0

Please explain the output.

int main() { int a=20; ++a; int b=a; ++b; if(b>a){ cout <<++a; } else{ cout <<--a; }

7th Jul 2017, 6:35 AM
Sanjeev Kumar
Sanjeev Kumar - avatar
5 Réponses
+ 2
note that if it is : cout << a++; output : 21 because in this case the compiler will print 'a' value then increase its value by 1. good luck!
7th Jul 2017, 12:00 PM
Hany
Hany - avatar
+ 1
I will explain line by line what happen a=20 ++a : a==21 int b=a : b==21 ++b : b==22 b>a : true cout<<++a is the same as ++a : a==22 cout<<a : display a as it is pre incrementation
7th Jul 2017, 6:57 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
++a means a is now 21, b = a means b is also 21 now, ++b means b is 22 and 22 > 21 so it enters the if statement and outputs 22 (because a which is 21 gets incremented again)
7th Jul 2017, 6:58 AM
‎ɐısıօլɐ
‎ɐısıօլɐ - avatar
+ 1
Thanks for the answer both of u 👍👍
7th Jul 2017, 7:06 AM
Sanjeev Kumar
Sanjeev Kumar - avatar
+ 1
You're welcome :)
7th Jul 2017, 7:06 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar