Challenge - 5 Problems
Loop Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested loops with break
What is the output of this C++ code snippet?
C++
#include <iostream> int main() { for (int i = 1; i <= 3; ++i) { for (int j = 1; j <= 3; ++j) { if (j == 2) break; std::cout << i << j << " "; } } return 0; }
Attempts:
2 left
💡 Hint
The inner loop breaks when j equals 2, so only j=1 prints per outer loop iteration.
✗ Incorrect
The inner loop prints when j=1, then breaks at j=2, so for each i (1 to 3), only i1 is printed.
❓ Predict Output
intermediate2:00remaining
While loop with continue behavior
What is the output of this C++ code?
C++
#include <iostream> int main() { int i = 0; while (i < 5) { i++; if (i == 3) continue; std::cout << i << " "; } return 0; }
Attempts:
2 left
💡 Hint
When i equals 3, the continue skips the print for that iteration.
✗ Incorrect
The loop increments i first, then skips printing when i==3, so output excludes 3.
❓ Predict Output
advanced2:00remaining
For loop with multiple increments
What is the output of this C++ code snippet?
C++
#include <iostream> int main() { for (int i = 0; i < 5; i += 2) { std::cout << i << " "; i++; } return 0; }
Attempts:
2 left
💡 Hint
The loop increments i twice each iteration: once in the body and once in the for statement.
✗ Incorrect
i starts at 0, prints 0, increments to 1 inside loop, then increments by 2 to 3 in for, prints 3, increments to 4 inside loop, then increments by 2 to 6 in for, loop ends.
❓ Predict Output
advanced2:00remaining
Do-while loop execution count
How many times does the loop body execute in this C++ code?
C++
#include <iostream> int main() { int count = 0; int i = 5; do { count++; i++; } while (i < 5); std::cout << count; return 0; }
Attempts:
2 left
💡 Hint
A do-while loop runs the body at least once before checking the condition.
✗ Incorrect
The loop runs once, increments i from 5 to 6, then checks condition i<5 which is false, so it stops after one iteration.
❓ Predict Output
expert3:00remaining
Complex nested loop with continue and break
What is the output of this C++ program?
C++
#include <iostream> int main() { for (int i = 1; i <= 3; ++i) { for (int j = 1; j <= 3; ++j) { if (i == j) continue; if (i + j == 4) break; std::cout << i << j << " "; } } return 0; }
Attempts:
2 left
💡 Hint
Pay attention to when continue skips printing and when break stops the inner loop.
✗ Incorrect
For i=1: j=1 skipped (continue), j=2 prints 12, j=3 prints 13; for i=2: j=1 prints 21, j=2 skipped (continue), j=3 breaks (2+3=5, no break, but condition is i+j==4, so break at j=2 since 2+2=4), so j=3 not reached; for i=3: j=1 prints 31, j=2 breaks (3+2=5 no), j=3 skipped (continue). So output is 12 13 21 31.