Скачать презентацию LECTURE 2 Operators An operator is a Скачать презентацию LECTURE 2 Operators An operator is a

lect2.pptx(С++)

  • Количество слайдов: 33

LECTURE #2 LECTURE #2

Operators An operator is a symbol that tells the compiler to perform specific mathematical Operators An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C++ is rich in built-in operators and provides following type of operators: Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Assignment Operators

Arithmetic Operators A =10 and B = 20 Operator Description Example + Adds two Arithmetic Operators A =10 and B = 20 Operator Description Example + Adds two operands A + B will give 30 - Subtracts second operand from the first A - B will give -10 * Multiply both operands A * B will give 200 / Divide numerator by de-numerator B / A will give 2 % Modulus Operator and remainder of after an integer division B % A will give 0 ++ Increment operator, increases integer value A++ will give 11 by one -- Decrement operator, decreases integer value by one A-- will give 9

Relational Operators A =10 and B = 20 Operator Description Example == Checks if Relational Operators A =10 and B = 20 Operator Description Example == Checks if the value of two operands is equal or not (A == B) is not true. != Checks if the value of two operands is not equal (A != B) is true. > Checks if the value is greater (A > B) is not true. < Checks if the value is less (A < B) is true. >= Checks if the value is greater than or (A >= B) is not true. equal <= Checks if the value is less than or equal to (A <= B) is true.

Logical Operators A =10 and B = 20 Operator Description Example && and Logical Logical Operators A =10 and B = 20 Operator Description Example && and Logical AND If both the comparisons are true then condition becomes true. (A>20 && B<30) is false. || or Logical OR If any of the two (A>20 || B<30) is true. comparisons is true then condition becomes true. ! Logical NOT Use to reverses the !(A>20) is true. logical state. If a condition is true then Logical NOT operator will make false.

Bitwise Operators The truth tables for &, |, and ^ p q p&q p|q Bitwise Operators The truth tables for &, |, and ^ p q p&q p|q p^q 0 0 0 1 0 1 1 1 0 1 0 0 1 1

 if A = 60; and B = 13; Now in binary format they if A = 60; and B = 13; Now in binary format they will be as follows: A = B = A&B = A|B = A^B = ~A = 0011 1100 0000 1101 --------0000 1100 0011 1101 0011 0001 1100 0011 12 61 49 -60

Binary-Shift Operators Binary Left Shift Operator << value is moved left by the number Binary-Shift Operators Binary Left Shift Operator << value is moved left by the number of bits specified by number. A = 60; Now in binary format A = 0011 1100 ------------A << 2 will give 240 which is 1111 0000 multiplication Binary Right Shift Operator >> value is moved right by the number of bits specified by number. A = 60; Now in binary format A = 0011 1100 ------------A >> 2 will give 15 which is 0000 1111 division

Assignment Operators A =10 B = 20 C = 25 Operator Description Example = Assignment Operators A =10 B = 20 C = 25 Operator Description Example = Simple assignment operator C=A+B C = 30 += Add AND assignment operator C += A same C = C + A C = 35 -= Subtract AND assignment operator C -= A same C = C – A C = 15 *= Multiply AND assignment operator C *= A same C = C * A C = 250 /= Divide AND assignment operator, C /= A same C = C / A C=2 %= Modulus AND assignment operator, C %= A same C = C % A C=5

A =10 B = 20 C = 25 <<= Left shift AND assignment operator A =10 B = 20 C = 25 <<= Left shift AND assignment operator C <<= 1 same C = C << 1 C = 50 >>= Right shift AND assignment operator C >>= 1 same C = C >> 1 C = 12 &= Bitwise AND assignment operator C &= 1 same C = C & 1 C=1 ^= bitwise exclusive OR and assignment operator C ^= 1 same C = C ^ 1 C = 24 |= bitwise inclusive OR and assignment operator C |= 1 same C = C | 1 C = 25

Operators Precedence in C++ Operator precedence determines the grouping of terms in an expression. Operators Precedence in C++ Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others. for example, the multiplication operator has higher precedence than the addition operator x = 7 + 3 * 2; Here x is assigned 13, not 20 because operator * has higher precedence than + so it first get multiplied with 3*2 and then adds into 7.

Category Operator Associativity Postfix () [] ->. ++ - - Left to right Unary Category Operator Associativity Postfix () [] ->. ++ - - Left to right Unary + - ! ~ ++ - - (type)* & sizeof Right to left * / % Left to right + - Left to right << >> Left to right < <= > >= Left to right == != Left to right Bitwise AND & Left to right Bitwise XOR ^ Left to right Bitwise OR | Left to right && Left to right Logical OR || Left to right Conditional ? : Right to left Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left , Left to right Multiplicative Additive Shift Relational Equality Logical AND Comma

sizeof Operator The sizeof is a keyword, but it is a compile -time operator sizeof Operator The sizeof is a keyword, but it is a compile -time operator that determines the size, in bytes, of a variable or data type. The sizeof operator can be used to get the size of classes, structures, unions and any other user defined data type. sizeof (data type)

int A =10; double B = 20. 5; Size of char : 1 Size int A =10; double B = 20. 5; Size of char : 1 Size of int : 4 Size of short int : 2 Size of long int : 4 Size of float : 4 Size of double : 8 Size of A : 4 Size of B : 8

Q 1 Which of them are not operators ^ B) *= C) ? = Q 1 Which of them are not operators ^ B) *= C) ? = D) ! A)

Q 2 What will be output if A=1? A << 3 A) 2 B) Q 2 What will be output if A=1? A << 3 A) 2 B) 4 C) 8 D) 16

Q 3 Which operator is must be? p 0 0 0 1 1 1 Q 3 Which operator is must be? p 0 0 0 1 1 1 p&q B) p ^ q C) p ! q D) p | q ? 0 A) q 0 1

YES or NO YES or NO

If Structure If Structure

The if keyword is used to execute a statement or block only if a The if keyword is used to execute a statement or block only if a condition is fulfilled. if (condition) statement Where condition is the expression that is being evaluated. If this condition is true, statement is executed. If it is false, statement is ignored (not executed) and the program continues right after this conditional structure.

if (x == 100) cout << if (x == 100) cout << "x is 100"; if (x < 100) if (x > 10) cout << "x more 10 less 100"; ? if (x == 100) { cout << "x is "; cout << x; } ? if (x <100 && x > 10) { cout << "x more 10 less 100"; }

If Else Structure if the condition is not true, but there are more conditions If Else Structure if the condition is not true, but there are more conditions use keyword else. if (condition) statement 1 else statement 2 if (condition 1) statement 1 else if (condition 2) statement 2 else statement 3

1 2 if (x == 100) cout << 1 2 if (x == 100) cout << "x is 100"; else cout << "x is not 100"; if (x > 0) cout << "x is positive"; else if (x < 0) cout << "x is negative"; else cout << "x is 0";

Conditional ? : Operator The ? operator is called a conditional operator and has Conditional ? : Operator The ? operator is called a conditional operator and has the following general form: Exp 1 ? Exp 2 : Exp 3; where Exp 1, Exp 2, and Exp 3 are expressions. The value of a ? expression is determined like this: Exp 1 is evaluated. If it is true, then Exp 2 is evaluated and becomes the value of the entire ? expression. If Exp 1 is false, then Exp 3 is evaluated and its value becomes the value of the expression.

if(y < 10) { var = 30; } else { var = 40; } if(y < 10) { var = 30; } else { var = 40; } var = (y < 10) ? 30 : 40;

Comma Operator The purpose of comma operator is to string together several expressions. The Comma Operator The purpose of comma operator is to string together several expressions. The value of a comma-separated list of expressions is the value of the right-most expression. z = (i=19, j=10, i+1); z = 20

#include <iostream> using namespace std; int main() { int i, j; j = 10; #include using namespace std; int main() { int i, j; j = 10; i = (j++, j+100, 999+j); cout << i; return 0; } 1010 j starts with the value 10. j is then incremented to 11. Next, j is added to 100. Finally, j (still containing 11) is added to 999, which yields the result 1010.

Difference between = and == = means assign == means check for equality int Difference between = and == = means assign == means check for equality int a=1; if (a=5) cout << “YES”; else cout <<“NO” int a=1; if (a==5) cout << “YES”; else cout <<“NO” Answer YES Because there is no check there is assign Answer NO Because there is check

Q 4 What will be output? char ch=“A”; cout << (ch==‘A’)? “A” : “B”; Q 4 What will be output? char ch=“A”; cout << (ch==‘A’)? “A” : “B”; A B) B C) Nothing D) Compilation Error A)

Q 5 What will be value of y? int y = 4; int x Q 5 What will be value of y? int y = 4; int x = 7; x = (y++, ++x, y+=x, x+=y); A) 4 B) 7 C) 13 D) 20

Q 6 What will be output? int a=5; if (a>=5) cout << “ 1”; Q 6 What will be output? int a=5; if (a>=5) cout << “ 1”; else if (a<=5) cout <<“ 2”; else { if (a==5) cout << “ 3”; } A) 1 B) 1 2 C) 1 3 D) 1 2 3

It is easy to drink coffee, but it needs time to prepare it. It is easy to drink coffee, but it needs time to prepare it.

Thank you!!! Thank you!!!