Int x=4; Int y=2; Cout<<x++*y--; | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 12

Int x=4; Int y=2; Cout<<x++*y--;

Can anyone tell me output of this code with explanation . I'm trying to understand but still confusing in question

1st Feb 2020, 1:13 AM
Manoj
Manoj - avatar
33 Answers
+ 10
x++: Increases x by 1, but returns the original value 4. y--: Decreases y by 1, but returns the original value 2. x++*y-- = 4*2 = 8
1st Feb 2020, 1:16 AM
Seb TheS
Seb TheS - avatar
+ 3
++x: Increases x by 1 and returns the new value 5. --y: Decreases y by 1 and returns the new value 1. ++x*--y = 5*1 = 5
1st Feb 2020, 1:21 AM
Seb TheS
Seb TheS - avatar
+ 3
Ok got it thank you sir
1st Feb 2020, 1:22 AM
Manoj
Manoj - avatar
+ 3
Oh sorry I read that by votes, I was answering your second question in the comments
1st Feb 2020, 1:33 AM
Gevork Bagratyan
+ 3
Ohk my mistake 😅
1st Feb 2020, 1:42 AM
Manoj
Manoj - avatar
+ 3
As both x and y are post incremented or decremented , so the value assigned first and then gets incremented or decremented, so in cout statement value of x (i.e. 4) and y (i.e. 2) is used and then gets incremented and decremented respectively, hence results in 8.
1st Feb 2020, 6:02 PM
Anmol Kumar
Anmol Kumar - avatar
+ 3
2*4=8
2nd Feb 2020, 7:21 AM
李域辰
+ 3
8 since it is postfix,use the original values before u increment x and decrement y .
2nd Feb 2020, 10:15 AM
THE_MASTERMIND
THE_MASTERMIND - avatar
+ 3
8 as you use post increment
2nd Feb 2020, 1:07 PM
Ashutosh Bichare
Ashutosh Bichare - avatar
+ 3
8
2nd Feb 2020, 2:38 PM
Gopal Agarwal
Gopal Agarwal - avatar
+ 3
It's answer is 8
2nd Feb 2020, 2:55 PM
Kaushal Kishore
Kaushal Kishore - avatar
+ 3
First you multiply 2 and 4 and print 8 The reason: In c++ it does the prefix then the math then the postfix so in this case it did the math then the postfix so if you tried to print out x value after that it will give you 5 and if you did the same for y it will give you 1.
2nd Feb 2020, 6:02 PM
ibrahim shaheen
ibrahim shaheen - avatar
+ 3
4*2=8. Post increment/decrement occurs after the expression is evaluated and output.
3rd Feb 2020, 12:52 AM
Sonic
Sonic - avatar
+ 3
The answer is 4*2=8
22nd Jan 2021, 12:54 PM
Kalindu Wanasinghe
Kalindu Wanasinghe - avatar
+ 2
X is incremented before multiplying to y and same goes for y. Answer is 5
1st Feb 2020, 1:30 AM
Gevork Bagratyan
+ 2
Wrong answer is 8
1st Feb 2020, 1:31 AM
Manoj
Manoj - avatar
+ 2
Answer = 8. causes, That's happen post increment and post decrement.
2nd Feb 2020, 12:45 AM
Md Sabbir Ahammed Rony
Md Sabbir Ahammed Rony - avatar
+ 2
x=5 y=2 case1: cout<<++x*--y; //6*1=6 after evaluation x=6,y=1 case2: cout<<x++*y--; //5*2=10 after evaluation x=6,y=1
2nd Feb 2020, 6:03 AM
Gnana phanideep Pothini
Gnana phanideep Pothini - avatar
+ 2
8
2nd Feb 2020, 12:25 PM
Никита
Никита  - avatar
+ 2
Becoz in this there is post increment and Decrement operator .so it take values increase and decrease of only x and y after the multiplication.
2nd Feb 2020, 2:58 PM
Kaushal Kishore
Kaushal Kishore - avatar