Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;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 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=++x+4;//increment x by 1 and add 4 cout <<"y=" <<y <<"\t and \t x=" <<x <<endl; return 0; }
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 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 X X-- -=X
Example 8 #include int main() { int x, y, z; x=10;//initial value for x x=x-1;//decrement x by 1 cout <<"x=" <<x <<endl; x-=1;//decrement x by 1 cout <<"x=" <<x <<endl; y=--x +4;//decrement x by 1 and add 4 cout <<"y=" <<y <<"\t and \t x=" <<x <<endl; z=4 + x--;//add 4 to x and decrement x by 1 cout <<"z=" <<z <<"\t and \t x=" <<x <<endl; return 0; }
Relational Operators <less than <=less than equal >greater than >=greater than equal ==equal to !=not_eqnot equal to
Logical Operators !notlogical NOT &&andlogical AND ||orlogical 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 ~comp1one’s complement <<left shift >>right shift &bitandbitwise And ^xorbitwise Exclusive-OR |bitorbitwise OR
Example 9 #include main() { long letter1 =0x41, letter2 =0x5A, x=0, y=0, z=0; cout <<"letter1=" <<letter1 <<"\t\t letter2=" <<letter2; cout <<endl; x= ~letter1; y=letter1 & letter2; z=letter1 | letter2; cout <<"x= " <<x <<"\t\t y=" <<y <<"\t\t z=" <<z; cout <<endl; return 0; }
Assignment Operators =assignment +=addition update -=subtraction update *=multiplication update /=division update
Assignment Operators %=modulus update <<=left shift update >>=right shift update &=and_eqbitwise AND update |=or_eqbitwise OR update ^=xor_eqbitwise 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 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. \n\n"; 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 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. \n\n"; return 0; }
Advanced if statement Any statement can be used in an if or else clause. if (expression1) { if (expression2) statement1; else { if (expression3) statement2; else statement3; } else statement4;
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 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 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 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 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 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 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; }