Challenge - 5 Problems
Break Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of break in a for loop
What is the output of this C++ code snippet?
C++
#include <iostream> int main() { for (int i = 0; i < 5; i++) { if (i == 3) { break; } std::cout << i << " "; } return 0; }
Attempts:
2 left
💡 Hint
The break statement stops the loop when i equals 3.
✗ Incorrect
The loop prints numbers starting from 0. When i reaches 3, break stops the loop, so only 0, 1, and 2 are printed.
❓ Predict Output
intermediate2:00remaining
Break inside nested loops
What is the output of this C++ code?
C++
#include <iostream> int main() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (j == 1) { break; } std::cout << i << j << " "; } } return 0; }
Attempts:
2 left
💡 Hint
Break stops the inner loop when j equals 1.
✗ Incorrect
The inner loop prints j=0 then breaks when j=1, so for each i, only i0 is printed.
❓ Predict Output
advanced2:00remaining
Break in while loop with condition
What is the output of this C++ program?
C++
#include <iostream> int main() { int count = 0; while (true) { if (count == 4) { break; } std::cout << count << " "; count++; } return 0; }
Attempts:
2 left
💡 Hint
Break stops the loop when count reaches 4, so 4 is not printed.
✗ Incorrect
The loop prints count starting at 0. When count equals 4, break stops the loop before printing 4.
❓ Predict Output
advanced2:00remaining
Break effect on switch-case
What is the output of this C++ code?
C++
#include <iostream> int main() { int x = 2; switch (x) { case 1: std::cout << "One "; break; case 2: std::cout << "Two "; break; case 3: std::cout << "Three "; default: std::cout << "Default "; } return 0; }
Attempts:
2 left
💡 Hint
Break stops execution after printing "Two ".
✗ Incorrect
Switch starts at case 2, prints "Two ", then break stops further cases from running.
🧠 Conceptual
expert3:00remaining
Break statement behavior in nested loops
Consider this C++ code snippet. Which statement is true about the effect of the break statement inside the inner loop?
C++
#include <iostream> int main() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i == j) { break; } std::cout << i << j << " "; } } return 0; }
Attempts:
2 left
💡 Hint
Break affects only the loop it is directly inside.
✗ Incorrect
Break stops the closest enclosing loop, here the inner loop. The outer loop continues normally.