Increment & Decrement operators (Postfix and Prefix) Ms.Nita Arora KHMS
Increment operator (++) Increases the value stored in a variable by 1  a++ a+=1 a=a+1 are all equivalent in functionality.
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
Decrement operator (--) Decreases the value stored in a variable by 1  a-- a - =1 a=a-1 are all equivalent in functionality.
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
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
Associativity  In case there are several operators of same priority, which one must be evaluated before- Leftmost Rightmost
Evaluation of expressions involving ++ and -- operators in postfix and prefix form PROBLEM AREA
Examples
#include <iostream.h> #include <conio.h> void main() {  clrscr(); cout <<&quot;\n************* EVALUATION OF EXPRESSIONS *************** \n\n&quot;; int a=10,b=20,c=30; cout<<&quot;a=&quot;<<a<<&quot;  b=&quot;<<b<<&quot;  c=&quot;<<c; int z=(a++)*c/a+b; cout <<&quot;\nz=(a++ )*c/a+b  =  &quot;; getch(); cout<<z; } Example 1
Example 2 #include <iostream.h> #include <conio.h> void main() {clrscr(); cout <<&quot;\n** EVALUATION OF EXPRESSIONS ***\n\n&quot;; int  a=10,b=20,c=30; cout<<&quot;a=&quot;<<a<<&quot;  b=&quot;<<b<<&quot;  c=&quot;<<c; z=++a*c/a+b; cout <<&quot;\nz=++a*c/a+b  =  &quot;<<; getch(); cout<<z; }
#include <iostream.h> #include <conio.h> void main() {clrscr(); cout <<&quot;\n************* EVALUATION OF EXPRESSIONS ****************\n\n&quot;; int  a=1,b=2,c=3; cout<<&quot;a=&quot;<<a<<&quot;  b=&quot;<<b<<&quot;  c=&quot;<<c; z= b+ c^a * ++b ; cout <<&quot;\nz=b+c^a * ++b;  =  &quot;; cout <<&quot;\nAnswer is :(3+3)^(1*3)  \n&quot;; cout <<&quot;\n  :(6)^(3)  \n&quot;; cout <<&quot;\nIn Binary :(110) XOR (011)  \n&quot;; cout <<&quot;\n  :(101)  \n&quot;; cout <<&quot;\nIn Decimal Answer is :  \n&quot;; getch(); cout<<z; } Example 3
#include <iostream.h> #include <conio.h> void main() {clrscr(); cout <<&quot;\n* EVALUATION OF EXPRESSIONS *\n\n&quot;; z=45; int q=45; cout<<&quot;\nq=&quot;<<q; cout<<&quot;\n-q--=&quot;<<-q--; cout<<&quot;q=  &quot; <<q; getch(); } Example 4
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;  
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 f=a+b/a; c=d*a+b; c=a++ * d +a; f=++b * b – a; c=a-(b++) * (--d);
Do you have any QUESTIONS ?

Prefix Postfix

  • 1.
    Increment & Decrementoperators (Postfix and Prefix) Ms.Nita Arora KHMS
  • 2.
    Increment operator (++)Increases the value stored in a variable by 1 a++ a+=1 a=a+1 are all equivalent in functionality.
  • 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.
    Decrement operator (--)Decreases the value stored in a variable by 1 a-- a - =1 a=a-1 are all equivalent in functionality.
  • 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.
    Priority of Operatorsand 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.
    Associativity Incase there are several operators of same priority, which one must be evaluated before- Leftmost Rightmost
  • 8.
    Evaluation of expressionsinvolving ++ and -- operators in postfix and prefix form PROBLEM AREA
  • 9.
  • 10.
    #include <iostream.h> #include<conio.h> void main() { clrscr(); cout <<&quot;\n************* EVALUATION OF EXPRESSIONS *************** \n\n&quot;; int a=10,b=20,c=30; cout<<&quot;a=&quot;<<a<<&quot; b=&quot;<<b<<&quot; c=&quot;<<c; int z=(a++)*c/a+b; cout <<&quot;\nz=(a++ )*c/a+b = &quot;; getch(); cout<<z; } Example 1
  • 11.
    Example 2 #include<iostream.h> #include <conio.h> void main() {clrscr(); cout <<&quot;\n** EVALUATION OF EXPRESSIONS ***\n\n&quot;; int a=10,b=20,c=30; cout<<&quot;a=&quot;<<a<<&quot; b=&quot;<<b<<&quot; c=&quot;<<c; z=++a*c/a+b; cout <<&quot;\nz=++a*c/a+b = &quot;<<; getch(); cout<<z; }
  • 12.
    #include <iostream.h> #include<conio.h> void main() {clrscr(); cout <<&quot;\n************* EVALUATION OF EXPRESSIONS ****************\n\n&quot;; int a=1,b=2,c=3; cout<<&quot;a=&quot;<<a<<&quot; b=&quot;<<b<<&quot; c=&quot;<<c; z= b+ c^a * ++b ; cout <<&quot;\nz=b+c^a * ++b; = &quot;; cout <<&quot;\nAnswer is :(3+3)^(1*3) \n&quot;; cout <<&quot;\n :(6)^(3) \n&quot;; cout <<&quot;\nIn Binary :(110) XOR (011) \n&quot;; cout <<&quot;\n :(101) \n&quot;; cout <<&quot;\nIn Decimal Answer is : \n&quot;; getch(); cout<<z; } Example 3
  • 13.
    #include <iostream.h> #include<conio.h> void main() {clrscr(); cout <<&quot;\n* EVALUATION OF EXPRESSIONS *\n\n&quot;; z=45; int q=45; cout<<&quot;\nq=&quot;<<q; cout<<&quot;\n-q--=&quot;<<-q--; cout<<&quot;q= &quot; <<q; getch(); } Example 4
  • 14.
    6.  What isthe 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.
    Evaluate the followingc++ 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 f=a+b/a; c=d*a+b; c=a++ * d +a; f=++b * b – a; c=a-(b++) * (--d);
  • 16.
    Do you haveany QUESTIONS ?

Editor's Notes

  • #2 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