Output is 5. Anyone knows how.? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Output is 5. Anyone knows how.?

int a = 1 while(a++<=1) while(a++ <= 2); cout<< a;

27th Nov 2020, 10:20 AM
shrijith
shrijith - avatar
2 Answers
+ 7
This is how the program executes: Outer loop: a++ <= 1 -> true a = 2 Inner loop: a++ <= 2 -> true a = 3 a++ <= 2 -> false a = 4, since it was still incremented during the condition Inner loop terminates. Outer loop: a++ <= 1 -> false a = 5, same as above Outer loop terminates. Keep in mind 'a' will be incremented once more even if the condition evaluates to false and the loop terminates.
27th Nov 2020, 10:30 AM
Shadow
Shadow - avatar
+ 3
Excellent explanation Shadow 👍👍👍
27th Nov 2020, 11:05 AM
Ipang