Upcoming SlideShare
×

# Prefix Postfix

8,115 views

Published on

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
8,115
On SlideShare
0
From Embeds
0
Number of Embeds
137
Actions
Shares
0
147
0
Likes
1
Embeds 0
No embeds

No notes for slide
• ### 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 ?