Please explain how it happens, Sir or Madam. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Please explain how it happens, Sir or Madam.

https://code.sololearn.com/cQphBidxcGM9/?ref=app

7th Jul 2017, 10:48 AM
Phyoe Min Thant
Phyoe Min Thant - avatar
2 Answers
+ 14
int main() { int a = 2; int b = 3; while (b--) { a++; } cout << a; return 0; } This code is focused on the 4th line only, "while(b--)", the while loop will stop itself when b will be equal to "0" because "0" is equal to the boolean value "false". Inside the "while" loop we increase the value of the "a" variable. while (3--) { 2++ } while (2--) { 3++ } while (1--) { 4++ } 1-- will be equal to "0" and this loop will stop his execution! 4++ will be equal to 5. cout << a // Output 5 With the "--" as prefix ("--b" rather than "b--") the output would be "4" because the "b" variable takes immediately the "0" value in the last step. Let me know if i said something wrong. :3
7th Jul 2017, 11:22 AM
Maz
Maz - avatar
+ 1
Thanks a lot Maz
7th Jul 2017, 9:46 PM
Phyoe Min Thant
Phyoe Min Thant - avatar