Your SlideShare is downloading. ×
0
Increment & Decrement operators (Postfix and Prefix) Ms.Nita Arora KHMS
Increment operator (++) <ul><li>Increases the value stored in a variable by 1  </li></ul><ul><ul><li>a++ </li></ul></ul><u...
Increment operator (++) can be used in PREFIX and POSTFIX form. int B=3; int A=++B; //output B=4 //  A=4 int B=3; int A=B+...
Decrement operator (--) <ul><li>Decreases the value stored in a variable by 1  </li></ul><ul><ul><li>a-- </li></ul></ul><u...
Decrement operator (--) can be used in  PREFIX  and  POSTFIX  form. int B=3; int A=--B; //output B=2 //  A=2 int B=3; int ...
Priority of Operators and Associativity Left Comma , 11 Right Assignment = += -= *= /= %= 10 Right Conditional ?: 9 Left L...
Associativity  <ul><li>In case there are several operators of same priority, which one must be evaluated before- </li></ul...
<ul><li>Evaluation of expressions involving ++ and -- operators in postfix and prefix form </li></ul>PROBLEM AREA
Examples
<ul><li>#include <iostream.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{  clrs...
Example 2 <ul><li>#include <iostream.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>void main() </li></ul><ul><...
<ul><li>#include <iostream.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{clrscr...
<ul><li>#include <iostream.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{clrscr...
6.  What is the screen output?   int  a=20; a += a++  +  ++a ; cout << a; 5.  What is the screen output?    int i, j, k, m...
Evaluate the following c++ expressions where a, b, c are integers and d and f are floating point numbers. The values are a...
Do you have any QUESTIONS ?
Upcoming SlideShare
Loading in...5
×

Prefix Postfix

7,204

Published on

Published in: Business, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
7,204
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
132
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • 1 1 The “operator” keyword Overloading Unary operators Overloading Binary operators Constructors as conversion routines Converting between basic and user-defined types LEAD IN: Overloaded Unary Operators
  • Transcript of "Prefix Postfix"

    1. 1. Increment & Decrement operators (Postfix and Prefix) Ms.Nita Arora KHMS
    2. 2. Increment operator (++) <ul><li>Increases the value stored in a variable by 1 </li></ul><ul><ul><li>a++ </li></ul></ul><ul><ul><li>a+=1 </li></ul></ul><ul><ul><li>a=a+1 </li></ul></ul><ul><li>are all equivalent in functionality. </li></ul>
    3. 3. Increment operator (++) can be used in PREFIX and POSTFIX form. int B=3; int A=++B; //output B=4 // A=4 int B=3; int A=B++; //output B=4 // A=3
    4. 4. Decrement operator (--) <ul><li>Decreases the value stored in a variable by 1 </li></ul><ul><ul><li>a-- </li></ul></ul><ul><ul><li>a - =1 </li></ul></ul><ul><ul><li>a=a-1 </li></ul></ul><ul><li>are all equivalent in functionality. </li></ul>
    5. 5. Decrement operator (--) can be used in PREFIX and POSTFIX form. int B=3; int A=--B; //output B=2 // A=2 int B=3; int A=B- -; //output B=2 // A=3
    6. 6. Priority of Operators and Associativity Left Comma , 11 Right Assignment = += -= *= /= %= 10 Right Conditional ?: 9 Left Logical Op. && || 8 Left Relational Op. == != 7 Left Relational Op. < <= >= > 6 Left Arithmetic Op. + - 5 Left Arithmetic Op. * / % 4 Right ++ -- ! & * (type) + - 3 Left ()[]->sizeof 2 Left Scope :: 1 associativity Description Operator Priority
    7. 7. Associativity <ul><li>In case there are several operators of same priority, which one must be evaluated before- </li></ul><ul><ul><ul><ul><ul><li>Leftmost </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Rightmost </li></ul></ul></ul></ul></ul>
    8. 8. <ul><li>Evaluation of expressions involving ++ and -- operators in postfix and prefix form </li></ul>PROBLEM AREA
    9. 9. Examples
    10. 10. <ul><li>#include <iostream.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ clrscr(); </li></ul><ul><li>cout <<&quot; ************* EVALUATION OF EXPRESSIONS *************** &quot;; </li></ul><ul><li>int a=10,b=20,c=30; </li></ul><ul><li>cout<<&quot;a=&quot;<<a<<&quot; b=&quot;<<b<<&quot; c=&quot;<<c; </li></ul><ul><li>int z=(a++)*c/a+b; </li></ul><ul><li>cout <<&quot; z=(a++ )*c/a+b = &quot;; </li></ul><ul><li>getch(); </li></ul><ul><li>cout<<z; </li></ul><ul><li>} </li></ul>Example 1
    11. 11. Example 2 <ul><li>#include <iostream.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{clrscr(); </li></ul><ul><li>cout <<&quot; ** EVALUATION OF EXPRESSIONS *** &quot;; </li></ul><ul><li>int a=10,b=20,c=30; </li></ul><ul><li>cout<<&quot;a=&quot;<<a<<&quot; b=&quot;<<b<<&quot; c=&quot;<<c; </li></ul><ul><li>z=++a*c/a+b; </li></ul><ul><li>cout <<&quot; z=++a*c/a+b = &quot;<<; </li></ul><ul><li>getch(); </li></ul><ul><li>cout<<z; </li></ul><ul><li>} </li></ul>
    12. 12. <ul><li>#include <iostream.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{clrscr(); </li></ul><ul><li>cout <<&quot; ************* EVALUATION OF EXPRESSIONS **************** &quot;; </li></ul><ul><li>int a=1,b=2,c=3; </li></ul><ul><li>cout<<&quot;a=&quot;<<a<<&quot; b=&quot;<<b<<&quot; c=&quot;<<c; </li></ul><ul><li>z= b+ c^a * ++b ; </li></ul><ul><li>cout <<&quot; z=b+c^a * ++b; = &quot;; </li></ul><ul><li>cout <<&quot; Answer is :(3+3)^(1*3) &quot;; </li></ul><ul><li>cout <<&quot; :(6)^(3) &quot;; </li></ul><ul><li>cout <<&quot; In Binary :(110) XOR (011) &quot;; </li></ul><ul><li>cout <<&quot; :(101) &quot;; </li></ul><ul><li>cout <<&quot; In Decimal Answer is : &quot;; </li></ul><ul><li>getch(); </li></ul><ul><li>cout<<z; </li></ul><ul><li>} </li></ul>Example 3
    13. 13. <ul><li>#include <iostream.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{clrscr(); </li></ul><ul><li>cout <<&quot; * EVALUATION OF EXPRESSIONS * &quot;; </li></ul><ul><li>z=45; </li></ul><ul><li>int q=45; </li></ul><ul><li>cout<<&quot; q=&quot;<<q; </li></ul><ul><li>cout<<&quot; -q--=&quot;<<-q--; </li></ul><ul><li>cout<<&quot;q= &quot; <<q; </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>Example 4
    14. 14. 6.  What is the screen output? int a=20; a += a++ + ++a ; cout << a; 5.  What is the screen output?    int i, j, k, m, ans;      i = 0;      j = -1;      k = 0;      m = 1;;      ans = i++  &&  ++j   ||  k  ||  m++;      cout<<ans; 4.  What is the screen output?      int i, j, k, ans;      i = 1;      j = 2;      k = 3;      ans = ++i  *   j  -   k--;      cout<<ans; 3.  What is the screen output?      int i, j, k, ans;      i = 1;      j = 2;      k = 3;      ans = i++  *   j  -   --k;      cout<<ans; 2.  What is the screen output?      int a, b;      a = 6;      b = ++a -1;      cout<<&quot;a =  &quot; << a << endl;      cout<<&quot;b =  &quot; << b << endl; 1.  What is the screen output?      int a, b;      a = 6;      b = a++ -1;      cout<<&quot;a =  &quot; << a <<endl;      cout<<&quot;b =  &quot; << b <<endl;  
    15. 15. Evaluate the following c++ expressions where a, b, c are integers and d and f are floating point numbers. The values are a = 5, b=3 and d=1.5 <ul><ul><li>f=a+b/a; </li></ul></ul><ul><ul><li>c=d*a+b; </li></ul></ul><ul><ul><li>c=a++ * d +a; </li></ul></ul><ul><ul><li>f=++b * b – a; </li></ul></ul><ul><ul><li>c=a-(b++) * (--d); </li></ul></ul>
    16. 16. Do you have any QUESTIONS ?
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×