Increment Operator To increment a variable X by
Increment Operator • To increment a variable X by 1. X+1; X+=; ++X; X = 10 Y = X + 1; X += 1; Y = ++X + 4; Y = 11 X = 11 Y = 15
Increment Operator • To increment a variable X by 1. X+1; X+=; ++X; • Operator before the variable are called prefix. X = 10 X = X + 1; X = 11 X += 1; X = 12 Y = ++X + 4; Y = 17
Example 6 #include<iostream. h> int main() { int x, y; x=10; x=x+1; cout <<"x=" <<x <<endl; x+=1; cout <<"x=" <<x <<endl; y=++x+4; cout <<"y=" <<y <<"t and t x=" <<x <<endl; return 0; } //initial value for x //increment x by 1 and add 4
Increment Operator • The operator can also be written after the variable to which it applies. • Operator before the variable are called postfix. • Effect of postfix in operation is slightly different. The incrementing of the variable to which it applies occurs after its value is used in context.
Example 7 #include<iostream. h> int main() { int x, y; x=10; //initial value for x x=x+1; //increment x by 1 cout <<"x=" <<x <<endl; x+=1; //increment x by 1 cout <<"x=" <<x <<endl; y=4 + x++ ; //add 4 to x and increment x by 1 cout <<"y=" <<y <<"t and t x=" <<x <<endl; return 0; }
Decrement Operator • To decrement a variable X by 1. X=X-1 --X X--=X
Example 8 #include<iostream. h> int main() { int x, y, z; x=10; x=x-1; cout <<"x=" <<x <<endl; x-=1; cout <<"x=" <<x <<endl; y=--x +4; cout <<"y=" <<y <<"t and t x=" <<x <<endl; z=4 + x--; cout <<"z=" <<z <<"t and t x=" <<x <<endl; return 0; } //initial value for x //decrement x by 1 and add 4 //add 4 to x and decrement x by 1
Relational Operators • • • < <= > >= == != not_eq less than equal greater than equal to not equal to
Logical Operators • ! • && • || not and or logical NOT logical AND logical OR
Bitwise Operators • Bitwise operators treat their operands as a series of individual bits rather than a numerical value. • They work with integer or constants.
Bitwise Operators • • • ~ << >> & ^ | comp 1 bitand xor bitor one’s complement left shift right shift bitwise And bitwise Exclusive-OR bitwise OR
Example 9 #include<iostream. h> main() { long letter 1 =0 x 41, letter 2 =0 x 5 A, x=0, y=0, z=0; cout <<"letter 1=" <<letter 1 <<"tt letter 2=" <<letter 2; cout <<endl; x= ~letter 1; y=letter 1 & letter 2; z=letter 1 | letter 2; cout <<"x= " <<x <<"tt y=" <<y <<"tt z=" <<z; cout <<endl; return 0; }
Assignment Operators • • • = += -= *= /= assignment addition update subtraction update multiplication update division update
Assignment Operators • • • %= <<= >>= &= |= ^= and_eq or_eq xor_eq modulus update left shift update right shift update bitwise AND update bitwise OR update bitwise Exclusive-OR
If Statement • The if statement enables the programmer to test for a condition and branch to different parts of the code depending on the result. If (expression) statement;
Example 10 #include<iostream. h> main() { int w, x, y, z; cout <<"enter a value for w" << endl <<"w="; cin>>w; cout <<"enter a value for x" << endl <<"x="; cin>>x; if (w>x) { y=w+x; cout <<"w is greater than x" <<endl <<"y=w+x=" <<y <<endl; } if (w<x) { z=w-x; cout <<"x is greater than w" <<endl <<"z=w-x=" <<z <<endl; } if (w==x) cout <<"A tie" <<endl; cout <<"Thanks for telling me. nn"; return 0; }
The else keyword • A program should take one branch if a condition is true, and another branch if the condition is false. if (expression) statement; else statement;
Example 11 #include<iostream. h> main() { int w, x, y, z; cout <<"enter a value for w" << endl <<"w="; cin>>w; cout <<"enter a value for x" << endl <<"x="; cin>>x; if (w>x) { y=w+x; cout <<"w is greater than x" <<endl <<"y=w+x=" <<y <<endl; } else { z=w-x; cout <<"x is greater than w" <<endl <<"z=w-x=" <<z <<endl; } cout <<"Thanks for telling me. nn"; return 0; }
Advanced if statement • Any statement can be used in an if or else clause. if (expression 1) { if (expression 2) statement 1; else { if (expression 3) statement 2; else statement 3; } } else statement 4;
Switch Statement • The switch statement enables you to select from multiple choice on a set of fixed values for a given expression.
Example 12 #include<iostream. h> main() { int x=0; cout <<"Please select one of the following delicious dishes: " << endl <<"1 t Hamburger" <<endl <<"2 t Hamburger and coke" <<endl <<"3 t Coke" <<endl; cout <<"Enter your choice t"; cin>>x; switch(x) { case 1: cout << endl <<"Hamburger" <<endl; break; case 2: cout << endl << "Hamburger and coke" <<endl; break; case 3: cout << endl << "Coke" <<endl; break; default: cout <<"You entered a wrong number" <<endl; } return 0; }
Loop • A loop executes a sequence of statements until a particular condition is true (or false). loop: statements if ( ) goto loop;
Example 13 #include<iostream. h> int main() { int i=1, y=20, x; loop: x=y+i; cout <<"x=" <<x <<"t i=" <<i <<"t y=" <<y <<endl; if (++i <= y) goto loop; cout <<"I love C++" <<endl; return 0; }
for Loop • General form of the for loop is: for (initializing; test; increment) statements;
Example 14 #include<iostream. h> int main() { int i=1, y=20, x; for(i; i<=y; i++) { x=y+i; cout <<"x=" <<x <<"t i=" <<i <<"t y=" <<y <<endl; } cout <<"I love C++" <<endl; return 0; }
Example 15 #include<iostream. h> int main() { int y=20, x; for(int i=1; i<=y; i++) { x=y+i; cout <<"x=" <<x <<"t i=" <<i <<"t y=" <<y <<endl; } cout <<"I love C++" <<endl; return 0; }
Example 16 #include<iostream. h> int main() { int i=1, y=20, x; for(; i<=y; i++) { x=y+i; cout <<"x=" <<x <<"t i=" <<i <<"t y=" <<y <<endl; } cout <<"I love C++" <<endl; return 0; }
Example 17 #include<iostream. h> int main() { int i=1, y=20, x; for(; i<=y; x=y+i++, cout <<"x=" <<x <<"t i=" <<i <<"t y=" <<y <<endl); cout <<"I love C++" <<endl; return 0; }
- Slides: 28