Can someone explain this. How it works . | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone explain this. How it works .

int main() { int x=3; while(x++<10){ x+=2; } cout<<x; //Output 13 }

30th Apr 2019, 11:00 AM
San Anemos
San Anemos - avatar
5 Answers
+ 8
3 3<10 true // first check then increment 3++ => 4 4+2 => 6 6<10 True 6++ => 7 7+2 => 9 9<10 True 9++ => 10 10+2 => 12 12 < 10 False 12++ => 13 hence x is 13
30th Apr 2019, 11:51 AM
Rstar
Rstar - avatar
+ 7
Ani Jona 🕊 thanks for the correction
30th Apr 2019, 12:00 PM
Rstar
Rstar - avatar
+ 2
In the while loop, you are adding 3 on each loop, the x++<10 means that x will be checked and get incremented by 1. When x = 9, it will be true, then x will become 10, then add 2 (x=12). In the last loop, x get incremented by 1(x=13), but the loop stops.
30th Apr 2019, 11:13 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
ah so... Thanks
30th Apr 2019, 11:55 AM
San Anemos
San Anemos - avatar
0
how x will become 10 ?
30th Apr 2019, 11:23 AM
San Anemos
San Anemos - avatar