0

I wonder why output is 13?

int x = 3; while (x++<10) { x += 2; } cout <<x; return 0;

2nd Mar 2020, 5:59 PM
Sh.A.kH.
Sh.A.kH. - avatar
4 Answers
+ 2
1st iteration: x++<10 => 3<10 true, x++, x=x+1 x=4, x+=2=> x=6, 2nd: x<10 => 6<10 true, x++, x=7, X+=2 =>x=9 3rd: X<10 =>9<10, true, x++=>x=10 X+=2. =>x=12, 4th: X<10=> 12<10 false, loop stops, x++ =>x=13 X++, Post increment first uses the value then increments... So answer is 13 Hope this helps you....
2nd Mar 2020, 6:08 PM
Jayakrishna šŸ‡®šŸ‡³
+ 1
The condition will be ran 1 more time than the code block.
2nd Mar 2020, 6:04 PM
Seb TheS
Seb TheS - avatar
0
x=3, then x++ == x = x +1 so x=4 4 < 10 so 4 + 2 = 6 Why output is 13???
2nd Mar 2020, 6:05 PM
Sh.A.kH.
Sh.A.kH. - avatar
0
Thank you very much! Yeah! I get it!
2nd Mar 2020, 6:15 PM
Sh.A.kH.
Sh.A.kH. - avatar