+ 1
Why the output of this C++ code is zero.đ€
#include <iostream> void main() { int m = 100; while (true) { if (m < 10) break; m = m - 10; } std::cout << "m is=" << m; }
3 Answers
+ 1
Because the last time when the value of m equal to 10 then if statement will be false because 10 is not less than 10 so it minus 10 and output zero after the break
0
Entering the loop
1st sequence:
m = 100; check if condition : 100 < 10 false ( no break) then m = 100 - 10 = 90.
2nd sequence:
m = 90; check if condition : 90 < 10 false ( no break) then m = 90 - 10 = 80.
3rd sequence:
m = 80; check if condition : 80 < 10 false ( no break) then m = 80 - 10 = 70.
4th sequence:
m = 70; check if condition : 70 < 10 false ( no break) then m = 70 - 10 = 60.
.
.
.
9th sequence:
m = 20; check if condition : 20 < 10 false ( no break) then m = 20 - 10 = 10.
10th sequence:
m = 10; check if condition : 10 < 10 false ( no break) then m = 10 - 10 = 0.
11th sequence:
m = 0; check if condition : 0 < 10 true then breck (end the loop).
m store the last value m = 0
cout << m; will put on screen 0.
.



