+ 2
Please explain how it happens, Sir or Madam.
2 odpowiedzi
+ 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
+ 1
Thanks a lot Maz