0
0
C++programming~20 mins

For loop in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
For Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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 << " ";
}
A0 1 2 3 4
B1 2 3 4 5
C0 1 2 3 4 5
D1 2 3 4
Attempts:
2 left
💡 Hint
Remember the loop starts at 0 and runs while i is less than 5.
Predict Output
intermediate
2:00remaining
For loop with decrement
What will be printed by this C++ code?
C++
for (int i = 5; i > 0; i--) {
    std::cout << i << " ";
}
A4 3 2 1 0
B5 4 3 2 1
C5 4 3 2 1 0
D6 5 4 3 2 1
Attempts:
2 left
💡 Hint
The loop starts at 5 and decreases until it is greater than 0.
🧠 Conceptual
advanced
2: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?
A3
B0
C10
DUndefined behavior
Attempts:
2 left
💡 Hint
Consider the scope of the variable declared inside the for loop.
Predict Output
advanced
2: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 << " ";
    }
}
A1 2 3 2 4 6
B1 2 3 1 2 3
C1 2 3 3 6 9
D2 4 6 4 8 12
Attempts:
2 left
💡 Hint
Multiply i and j for each iteration and print in order.
Predict Output
expert
2: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 << " ";
}
A0 1 2 3
B0 1 2 3 4
C0 1 3 4
D0 1 3
Attempts:
2 left
💡 Hint
Remember that continue skips the current iteration and break stops the loop.