1 / 28

Increment Operator

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

kenton
Télécharger la présentation

Increment Operator

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. Example 6 #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=++x+4; //increment x by 1 and add 4 cout <<"y=" <<y <<"\t and \t x=" <<x <<endl; return 0; }

  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.

  5. 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; }

  6. Decrement Operator • To decrement a variable X by 1. X = X - 1 --X X-- -=X

  7. Example 8 #include<iostream.h> 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; }

  8. Relational Operators • < less than • <= less than equal • > greater than • >= greater than equal • == equal to • != not_eq not equal to

  9. Logical Operators • ! not logical NOT • && and logical AND • || or logical OR

  10. Bitwise Operators • Bitwise operators treat their operands as a series of individual bits rather than a numerical value. • They work with integer or constants.

  11. Bitwise Operators • ~ comp1 one’s complement • << left shift • >> right shift • & bitand bitwise And • ^ xor bitwise Exclusive-OR • | bitor bitwise OR

  12. Example 9 #include<iostream.h> 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; }

  13. Assignment Operators • = assignment • += addition update • -= subtraction update • *= multiplication update • /= division update

  14. Assignment Operators • %= modulus update • <<= left shift update • >>= right shift update • &= and_eq bitwise AND update • |= or_eq bitwise OR update • ^= xor_eq bitwise Exclusive-OR

  15. 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;

  16. 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. \n\n"; return 0; }

  17. 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;

  18. 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. \n\n"; return 0; }

  19. 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;

  20. Switch Statement • The switch statement enables you to select from multiple choice on a set of fixed values for a given expression.

  21. 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; }

  22. Loop • A loop executes a sequence of statements until a particular condition is true (or false). loop: statements if ( ) goto loop;

  23. 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; }

  24. for Loop • General form of the for loop is: for (initializing; test; increment) statements;

  25. 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; }

  26. 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; }

  27. 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; }

  28. 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; }

More Related