prefix-postfix-1220980481065867-9
-
Upload
mohsin-akram -
Category
Documents
-
view
217 -
download
0
Transcript of prefix-postfix-1220980481065867-9
![Page 1: prefix-postfix-1220980481065867-9](https://reader030.fdocuments.in/reader030/viewer/2022021123/577d20751a28ab4e1e92efa3/html5/thumbnails/1.jpg)
8/2/2019 prefix-postfix-1220980481065867-9
http://slidepdf.com/reader/full/prefix-postfix-1220980481065867-9 1/16
![Page 2: prefix-postfix-1220980481065867-9](https://reader030.fdocuments.in/reader030/viewer/2022021123/577d20751a28ab4e1e92efa3/html5/thumbnails/2.jpg)
8/2/2019 prefix-postfix-1220980481065867-9
http://slidepdf.com/reader/full/prefix-postfix-1220980481065867-9 2/16
3/15/2012 2
Increment operator (++)
Increases the value stored in a
variable by 1 a++ a+=1 a=a+1
are all equivalent in functionality.
![Page 3: prefix-postfix-1220980481065867-9](https://reader030.fdocuments.in/reader030/viewer/2022021123/577d20751a28ab4e1e92efa3/html5/thumbnails/3.jpg)
8/2/2019 prefix-postfix-1220980481065867-9
http://slidepdf.com/reader/full/prefix-postfix-1220980481065867-9 3/16
3/15/2012 3
Increment operator (++) can beused 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
![Page 4: prefix-postfix-1220980481065867-9](https://reader030.fdocuments.in/reader030/viewer/2022021123/577d20751a28ab4e1e92efa3/html5/thumbnails/4.jpg)
8/2/2019 prefix-postfix-1220980481065867-9
http://slidepdf.com/reader/full/prefix-postfix-1220980481065867-9 4/16
3/15/2012 4
Decrement operator (--)
Decreases the value stored in avariable by 1
a-- a - =1 a=a-1
are all equivalent in functionality.
![Page 5: prefix-postfix-1220980481065867-9](https://reader030.fdocuments.in/reader030/viewer/2022021123/577d20751a28ab4e1e92efa3/html5/thumbnails/5.jpg)
8/2/2019 prefix-postfix-1220980481065867-9
http://slidepdf.com/reader/full/prefix-postfix-1220980481065867-9 5/16
3/15/2012 5
Decrement operator (--) can beused 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
![Page 6: prefix-postfix-1220980481065867-9](https://reader030.fdocuments.in/reader030/viewer/2022021123/577d20751a28ab4e1e92efa3/html5/thumbnails/6.jpg)
8/2/2019 prefix-postfix-1220980481065867-9
http://slidepdf.com/reader/full/prefix-postfix-1220980481065867-9 6/16
3/15/2012 6
Priority of Operators and
AssociativityPriority Operator Description associativity
1 :: Scope Left
2 ()[]->sizeof Left
3 ++ -- ! & *
(type) + -
Right
4 * / % Arithmetic Op. Left
5 + - Arithmetic Op. Left
6 < <= >= > Relational Op. Left
7 == != Relational Op. Left
8 && || Logical Op. Left
9 ?: Conditional Right
10 = += -= *= /= %= Assignment Right
11 , Comma Left
![Page 7: prefix-postfix-1220980481065867-9](https://reader030.fdocuments.in/reader030/viewer/2022021123/577d20751a28ab4e1e92efa3/html5/thumbnails/7.jpg)
8/2/2019 prefix-postfix-1220980481065867-9
http://slidepdf.com/reader/full/prefix-postfix-1220980481065867-9 7/16
3/15/2012 7
Associativity
In case there are several operatorsof same priority, which one must beevaluated before-
Leftmost Rightmost
![Page 8: prefix-postfix-1220980481065867-9](https://reader030.fdocuments.in/reader030/viewer/2022021123/577d20751a28ab4e1e92efa3/html5/thumbnails/8.jpg)
8/2/2019 prefix-postfix-1220980481065867-9
http://slidepdf.com/reader/full/prefix-postfix-1220980481065867-9 8/16
3/15/2012 8
Evaluation of expressions involving
++ and -- operators in postfix and
prefix form
PROBLEM AREA
![Page 9: prefix-postfix-1220980481065867-9](https://reader030.fdocuments.in/reader030/viewer/2022021123/577d20751a28ab4e1e92efa3/html5/thumbnails/9.jpg)
8/2/2019 prefix-postfix-1220980481065867-9
http://slidepdf.com/reader/full/prefix-postfix-1220980481065867-9 9/16
3/15/2012 9
Examples
![Page 10: prefix-postfix-1220980481065867-9](https://reader030.fdocuments.in/reader030/viewer/2022021123/577d20751a28ab4e1e92efa3/html5/thumbnails/10.jpg)
8/2/2019 prefix-postfix-1220980481065867-9
http://slidepdf.com/reader/full/prefix-postfix-1220980481065867-9 10/16
#include <iostream.h>
#include <conio.h>
void main()
{ clrscr();cout <<"\n************* EVALUATION OF EXPRESSIONS*************** \n\n";
int a=10,b=20,c=30;
cout<<"a="<<a<<" b="<<b<<" c="<<c;
int z=(a++)*c/a+b;
cout <<"\nz=(a++ )*c/a+b = ";getch();
cout<<z;
}
Example 1
![Page 11: prefix-postfix-1220980481065867-9](https://reader030.fdocuments.in/reader030/viewer/2022021123/577d20751a28ab4e1e92efa3/html5/thumbnails/11.jpg)
8/2/2019 prefix-postfix-1220980481065867-9
http://slidepdf.com/reader/full/prefix-postfix-1220980481065867-9 11/16
Example 2
#include <iostream.h>
#include <conio.h>
void main()
{clrscr();
cout <<"\n** EVALUATION OF EXPRESSIONS ***\n\n";
int a=10,b=20,c=30;
cout<<"a="<<a<<" b="<<b<<" c="<<c;
z=++a*c/a+b;
cout <<"\nz=++a*c/a+b = "<<;getch();
cout<<z;
}
![Page 12: prefix-postfix-1220980481065867-9](https://reader030.fdocuments.in/reader030/viewer/2022021123/577d20751a28ab4e1e92efa3/html5/thumbnails/12.jpg)
8/2/2019 prefix-postfix-1220980481065867-9
http://slidepdf.com/reader/full/prefix-postfix-1220980481065867-9 12/16
#include <iostream.h>
#include <conio.h>void main()
{clrscr();
cout <<"\n************* EVALUATION OF EXPRESSIONS****************\n\n";
int a=1,b=2,c=3;
cout<<"a="<<a<<" b="<<b<<" c="<<c;
z= b+ c^a * ++b ;
cout <<"\nz=b+c^a * ++b; = ";
cout <<"\nAnswer is :(3+3)^(1*3) \n";
cout <<"\n :(6)^(3) \n";
cout <<"\nIn Binary :(110) XOR (011) \n";cout <<"\n :(101) \n";
cout <<"\nIn Decimal Answer is : \n";
getch();
cout<<z;
}
Example 3
![Page 13: prefix-postfix-1220980481065867-9](https://reader030.fdocuments.in/reader030/viewer/2022021123/577d20751a28ab4e1e92efa3/html5/thumbnails/13.jpg)
8/2/2019 prefix-postfix-1220980481065867-9
http://slidepdf.com/reader/full/prefix-postfix-1220980481065867-9 13/16
#include <iostream.h>
#include <conio.h>
void main()
{clrscr();
cout <<"\n* EVALUATION OF EXPRESSIONS *\n\n";z=45;
int q=45;
cout<<"\nq="<<q;
cout<<"\n-q--="<<-q--;
cout<<"q= " <<q;getch();
}
Example 4
![Page 14: prefix-postfix-1220980481065867-9](https://reader030.fdocuments.in/reader030/viewer/2022021123/577d20751a28ab4e1e92efa3/html5/thumbnails/14.jpg)
8/2/2019 prefix-postfix-1220980481065867-9
http://slidepdf.com/reader/full/prefix-postfix-1220980481065867-9 14/16
1. What is the screen output?
int a, b;
a = 6;
b = a++ -1;cout<<"a = " << a <<endl;
cout<<"b = " << b <<endl;
2. What is the screen output?
int a, b;
a = 6;
b = ++a -1;cout<<"a = " << a << endl;
cout<<"b = " << b << endl;
3. What is the screen output?
int i, j, k, ans;
i = 1;
j = 2;k = 3;
ans = i++ * j - --k;
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;
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;
6. What is the screen output?
int a=20;
a += a++ + ++a ;cout << a;
![Page 15: prefix-postfix-1220980481065867-9](https://reader030.fdocuments.in/reader030/viewer/2022021123/577d20751a28ab4e1e92efa3/html5/thumbnails/15.jpg)
8/2/2019 prefix-postfix-1220980481065867-9
http://slidepdf.com/reader/full/prefix-postfix-1220980481065867-9 15/16
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
a) f=a+b/a;
b) c=d*a+b;
c) c=a++ * d +a;
d) f=++b * b ± a;
e) c=a-(b++) * (--d);
![Page 16: prefix-postfix-1220980481065867-9](https://reader030.fdocuments.in/reader030/viewer/2022021123/577d20751a28ab4e1e92efa3/html5/thumbnails/16.jpg)
8/2/2019 prefix-postfix-1220980481065867-9
http://slidepdf.com/reader/full/prefix-postfix-1220980481065867-9 16/16
Do you have any