Challenge - 5 Problems
For Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple for loop with increment
What is the output of the following C++ code?
C++
for (int i = 0; i < 5; i++) { std::cout << i << " "; }
Attempts:
2 left
💡 Hint
Remember the loop starts at 0 and runs while i is less than 5.
✗ Incorrect
The loop starts at 0 and increments by 1 until it reaches 4. It stops before i equals 5, so it prints 0 1 2 3 4.
❓ Predict Output
intermediate2:00remaining
For loop with decrement
What will be printed by this C++ code?
C++
for (int i = 5; i > 0; i--) { std::cout << i << " "; }
Attempts:
2 left
💡 Hint
The loop starts at 5 and decreases until it is greater than 0.
✗ Incorrect
The loop prints from 5 down to 1. It stops before i reaches 0.
🧠 Conceptual
advanced2:00remaining
Understanding loop variable scope
What is the value of variable
i after this loop finishes?C++
int i = 10; for (int i = 0; i < 3; i++) { // loop body } // What is i here?
Attempts:
2 left
💡 Hint
Consider the scope of the variable declared inside the for loop.
✗ Incorrect
The
i declared inside the loop is a new variable local to the loop. The outer i remains unchanged at 10.❓ Predict Output
advanced2:00remaining
Nested for loops output
What is the output of this nested for loop code?
C++
for (int i = 1; i <= 2; i++) { for (int j = 1; j <= 3; j++) { std::cout << i * j << " "; } }
Attempts:
2 left
💡 Hint
Multiply i and j for each iteration and print in order.
✗ Incorrect
For i=1, j=1 to 3 prints 1 2 3. For i=2, j=1 to 3 prints 2 4 6. Combined output is 1 2 3 2 4 6.
❓ Predict Output
expert2:00remaining
For loop with continue and break
What is the output of this code?
C++
for (int i = 0; i < 6; i++) { if (i == 2) continue; if (i == 4) break; std::cout << i << " "; }
Attempts:
2 left
💡 Hint
Remember that continue skips the current iteration and break stops the loop.
✗ Incorrect
When i is 2, continue skips printing. When i is 4, break stops the loop. So printed values are 0,1,3.