[go: up one dir, main page]

0% found this document useful (0 votes)
17 views26 pages

Fundamentals of Programming Lec-4.Pptx (1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views26 pages

Fundamentals of Programming Lec-4.Pptx (1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

CS1102

Fundamentals of Programming
Lecture 4 (Introduction to C)

2023/12/11

Bachelor of Information Technology


Department of Computing
Faculty of Computing and Technology
Saegis Campus
Nugegoda

Udaya Perera(MSc in IT(Reading),B.Eng SE, SQA 9 SE)

1
Arithmetic Operators in C
Arithmetic operators are used to perform basic arithmetic operations
on numeric data types.

Operations include addition, subtraction, multiplication, division, and


calculating the remainder after division.

2
3
Addition (+) Operator
The addition operator adds two operands together and returns their
sum.

#include <stdio.h>
int main(void) {
int a = 5;
int b = 3;
int sum = a + b;
printf("Sum: %i\n", sum); // Output: Sum: 8
}

4
Subtraction (-) Operator
The subtraction operator subtracts the second operand from the first
operand and returns their difference.
#include <stdio.h>
int main(void) {
int a = 10;
int b = 5;
int difference = a - b;
printf("Difference: %i\n", difference); // Output: Difference: 5
}

5
Multiplication (*) Operator
The multiplication operator multiplies two operands and returns their
product.
#include <stdio.h>

int main(void) {
int a = 4;
int b = 3;
int product = a * b;
printf("Product: %i\n", product); // Output: Product: 12
}

6
Division (/) Operator
The division operator divides the first operand by the second operand
and returns their quotient.
#include <stdio.h>

int main(void) {
int a = 10;
int b = 2;
int quotient = a / b;
printf("Quotient: %i\n", quotient); // Output: Quotient: 5
}
7
Modulo (%) Operator
The modulo operator returns the remainder of the first operand when
divided by the second operand.
#include <stdio.h>
int main(void) {
int a = 10;
int b = 3;
int remainder = a % b;
printf("Remainder: %i\n", remainder); // Output: Remainder: 1
}

8
Relational Operators in C
Relational operators are used to compare values and return a result.

The result is a Boolean value. A Boolean value is either true


(represented by 1) or false (represented by 0).

These operators are commonly used in decision-making statements


such as if statements, and while loops.

9
10
Equal to (==) Operator
• The equal to operator checks if two values are equal.
• It essentially asks the question, "Are these two values equal?"
• Note that you use the comparisson operator (two equal signs – ==)
and not the assignment operator (=) which is used for assigning a
value to a variable.
#include <stdio.h>
int main(void) {
int a = 5;
int b = 5;
int result = (a == b);
printf("Result: %i\n", result); // Output: Result: 1
}

11
Not equal to (!=) Operator
The not equal to operator checks if two values are NOT equal.

#include <stdio.h>

int main(void) {
int a = 5;
int b = 3;
int result = (a != b);
printf("Result: %i\n", result); // Output: Result: 1
}

12
Greater than (>) Operator
This operator compares two values to check if one is greater than the
other.
#include <stdio.h>

int main(void) {
int a = 10;
int b = 5;
int result = (a > b);
printf("Result: %i\n", result); // Output: Result: 1
}
13
Less than (<) Operator
This operator compares two values to check if one is less than the
other.
#include <stdio.h>

int main(void) {
int a = 10;
int b = 5;
int result = (a < b);
printf("Result: %i\n", result); // Output:
Result: 0
}
14
Logical Operators
Logical operators operate on Boolean values and return a Boolean
value.

15
AND (&&) Operator
• The logical AND (&&) operator checks whether all operands are
true.
• The result is true only when all operands are true.
• Here is the truth table for the AND (&&) operator when you are
working with two operands:

16
Example
• The result of (10 == 10) && (20 == 20) is true because both
operands are true.
• The result of (10 == 20) && (20 == 20) is false because one of the
operands is false.
• When the first operand is false, the second operand is not
evaluated

17
OR (||) Operator
• The logical OR (||) operator checks if at least one of the operands is
true.
• The result is true only when at least one of the operands is true.
• Here is the truth table for the OR (||) operator when you are
working with two operands:

18
Example
• The result of (10 == 20) || (20 == 20) is true because one of the
operands is true.
• The result of (20 == 20) || (10 == 20) is true because one of the
operands is true
• If the first operand is true, then the second operator is not
evaluated.

19
NOT (!) Operator
• The logical NOT (!) operator negates the operand.
• If the operand is true, it returns false.
• And if it is false, it returns true.

You may want to use the NOT operator when when you want to flip the
value of a condition and return the opposite of what the condition
evaluates to.

20
Example
• The result of !(10 == 10) is false.
• The condition 10 == 10 is true, but the ! operator negates it so the
result is false.

• The result of !(10 == 20) is true.


• The condition 10 == 20 is false, but the ! operator negates it.

21
Compound Assignment Operators
• Compound assignment operators are shorthand notations.
• They allow you to modify a variable by performing an operation on
it and then storing the result of the operation back into the same
variable in a single step.
• This can make your code more concise and easier to read.

22
Common compound assignment operators
• +=: Addition and assignment
• -=: Subtraction and assignment
• *=: Multiplication and assignment
• /=: Division and assignment
• %=: Modulo and assignment

23
how the += operator works
#include <stdio.h>

int main(void) {
int num = 10;
num += 5;
printf("Num: %i\n", num); // Num: 15
}
Note that the num += 5; line works exactly the same as doing num = num + 5,
which would mean num = 10 + 5, but with fewer lines of code.
24
Home Work
Try other compound assignment operators at home.

25
Thank you

26

You might also like