Presentation is loading. Please wait.

Presentation is loading. Please wait.

Www.hndit.com HNDIT11034 More Operators.

Similar presentations


Presentation on theme: "Www.hndit.com HNDIT11034 More Operators."— Presentation transcript:

1 HNDIT11034 More Operators

2 Increment and Decrement Operators
Increment and Decrement Operators Prefix and Postfix notations The increment ope rator, ++, can be used in two ways: As a prefix, in which the operator follows the variable. ++n ; As a postfix, in which the operator follows the variable. n++;

3 The following code segment differentiates the two notations: var1 =10;
The following code segment differentiates the two notations: var1 =10; var2 = ++var1; The equivalent of this code is: var1 = 10; var1 = var1 + 1; var2 = var1; In this case, both var1 and var2 are set to 11.

4 However, if the code is written as:
However, if the code is written as: var1 = 10; var2 = var1++; the equivalent code will be: var1 =10; var2 = var1; var1 = var1 +1; Here,var1 is set to 11,but the value of var2 is 10.

5 The decrement operator can also be used in both the prefix and postfix forms.

6 #include <iostream.h> void main() { int j; // declare j as int
#include <iostream.h> void main() { int j; // declare j as int j=1; // initialize j to 1 cout << “j=“ << j << ‘\n’; j++; //increment j j--; //decrement j }

7 Arithmetic Assignments
Longer form x = x + y x = x - y x = x * y x = x / y x = x % y Short hand x += y x -= y x *= y x /= y x %= y

8 Conditional Operators
Consider the following statements that find the maximum of two given numbers: if (num1 > num2) { max = num1; } else max= num2;

9 above program code can be modified using the conditional operator as:
In the above program code, we are determining whether num1 is greater than num2. The variable, max is assigned the value, num1, if the expression, (num1 > num2), evaluates to true, and the value, num2,if the expression evaluates to false. The above program code can be modified using the conditional operator as: max = (num1 > num2)? num1 : num2;

10 cout << “Please enter a score for the student “;
The?: operator is called the ternary operator since it has three operands. The following example calculates the grade of a student based on his scores. int score =0; cout << “Please enter a score for the student “; cin >> score; char grade = (score > 75)? ‘A’:’B’;

11 cout << ( score > 50 ? “PASSED”: “FAILED”) << endl;
The following code will display either “PASSED” or “FAILED”, depending on the value in the variable, score. cout << ( score > 50 ? “PASSED”: “FAILED”) << endl;

12 Bitwise Operators

13 Assume if A = 60; and B = 13; now in binary format they will be as follows: A = B = A&B = A|B = A^B = ~A  =

14

15 cout << "Line 1 - Value of c is : " << c << endl ;
int a = 60; // 60 = int b = 13; // 13 = int c = 0; c = a & b; // 12 = cout << "Line 1 - Value of c is : " << c << endl ; c = a | b; // 61 = cout << "Line 2 - Value of c is: " << c << endl ; c = a ^ b; // 49 = cout << "Line 3 - Value of c is: " << c << endl ; c = ~a; // -61 = cout << "Line 4 - Value of c is: " << c << endl ; c = a << 2; // 240 = cout << "Line 5 - Value of c is: " << c << endl ; c = a >> 2; // 15 = cout << "Line 6 - Value of c is: " << c << endl ;

16 Q & A


Download ppt "Www.hndit.com HNDIT11034 More Operators."

Similar presentations


Ads by Google