Lecture - Operators in C++ (Book: Tony Gaddis).pptx

arshadfarhad08 21 views 22 slides Oct 09, 2024
Slide 1
Slide 1 of 22
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22

About This Presentation

Operators in C++


Slide Content

Lecture-4: Operators Computer Programming 10/9/24 1

Lecture Contents Arithmetic & Sizeof Operators Logical & Relational Operators Assignment Operator Compound Assignment Logical Operators. 10/9/24 2

Arithmetic operators Arithmetic calculations * Multiplication / Division Integer division truncates remainder 7 / 5 evaluates to 1 % Modulus operator returns remainder 7 % 5 evaluates to 2 + and – Addition and Subtraction 10/9/24 3

Class Activity Write a program that has all the mathematical operators. +, -, /, % 10/9/24 4

Arithmetic operators Rules of operator precedence 10/9/24 5

Arithmetic operators Priority of operators a = 5 + 7 % 2; we may doubt if it really means: a = 5 + (7 % 2) with result 6 or a = (5 + 7) % 2 with result 0 Parentheses are included when one is not sure 10/9/24 6

Arithmetic operators Given integer variables a, b, c, d, where a = 1, b = 2, c = 3, d = 4, Evaluate the following expressions: 10/9/24 7 a + b - c + d a * b / c 1 + a * b % c a + d % b - c

Compound/ Combined Assignment Arithmetic Assignment Operators a = a + b; a += b; 10/9/24 8

Arithmetic operators Increment Operators (Unary Operator) count = count + 1; count +=1; count++; OR ++count; 10/9/24 9 int a = 5; int b = 10; int c = a * b++; int a = 5; int b = 10; int c = a * ++b; 50 55

Arithmetic operators Decrement Operators (Unary Operator) count = count - 1; count -=1; count--; OR --count; 10/9/24 10 postfix prefix

Arithmetic operators 10/9/24 11 int main() { int count = 10; cout << count << endl ; cout << ++count << endl ; cout << count << endl ; cout << count++ << endl ; cout << count << endl ; } 10 11 11 11 12

Sizeof () operator 10/9/24 12 Data Type No. of Bytes No. of Bits char 1 8 short 2 16 int 4 32 long 4 32 float 4 32 double 8 64 long double 10 80 sizeof ( ) returns the size in bytes a = sizeof (char); cout << a; Or cout << sizeof ( int ); It Will return the integer size in bytes on the screen

Sizeof () operator 10/9/24 13

Releational operators To evaluate comparison between two expressions Result : True / False 10/9/24 14 Assume variable x holds 10 and variable y holds 20, then:

Relational operators Examples (7 == 5) would return (3 != 2) would return (6 >= 6) would return If a=2, b=3 and c=6 (a*b >= c) would return true since it is (2*3 >= 6) (b+4 > a*c) would return false since it is (3+4 > 2*6) ((b=2) == a) would return true 10/9/24 15 false true true

Logical operators Logical expressions - expressions that use conditional statements and logical operators. && (And) A && B is true if and only if both A and B are true || (Or) A || B is true if either A or B are true ! (Not) !(condition) is true if condition is false, and false if condition is true This is called the logical complement or negation Examples (salary < 10000) || (dependants > 5) (temperature > 40.0) && (humidity > 90) !(temperature > 90.0) 10/9/24 16

Logical operators - Exercises NOT, AND, OR : ( !, &&, || ) Operator ! is equivalent to Boolean operation NOT ! (5 == 5) returns ! (6 <= 4) returns ! true returns ! false returns ((5 == 5) && (3 > 6)) returns false (true && false) ((5 == 5) || (3 > 6)) returns true ( true || false ). 10/9/24 17 false true false true

Remember && operator yields a true result only when both its operands are true. || operator yields a false result only when both its operands are false. 10/9/24 18

Expressions A sequence of operators and operands that specifies a computation Operand On which computation is performed E.g : c = a + b; //a & b are operands & + is an operator C = 7 + 8; // C=15 10/9/24 19

Escape Sequence 10/9/24 20

Escape Sequence Example 10/9/24 21

The setprecision Manipulator 10/9/24 22