+ 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; }

22nd Feb 2020, 11:03 AM
DESSIE BAHIRU KEBEDE
DESSIE BAHIRU KEBEDE - avatar
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
22nd Feb 2020, 1:34 PM
ycsvenom
ycsvenom - avatar
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. .
22nd Feb 2020, 4:49 PM
Rafik Abdelhak Nadir
Rafik Abdelhak Nadir - avatar