0
0
C++programming~20 mins

Loop execution flow in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Loop Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A11 12 13 21 22 23 31 32 33
B11 12 21 22 31 32
C11 21 31
D11 21 22 31 32
Attempts:
2 left
💡 Hint
The inner loop breaks when j equals 2, so only j=1 prints per outer loop iteration.
Predict Output
intermediate
2: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;
}
A1 2 4 5
B1 2 3 4 5
C1 2 3 4
D2 3 4 5
Attempts:
2 left
💡 Hint
When i equals 3, the continue skips the print for that iteration.
Predict Output
advanced
2: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;
}
A0 2 3 4
B0 2 4
C0 1 3
D0 3
Attempts:
2 left
💡 Hint
The loop increments i twice each iteration: once in the body and once in the for statement.
Predict Output
advanced
2: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;
}
A0
B1
C5
DInfinite loop
Attempts:
2 left
💡 Hint
A do-while loop runs the body at least once before checking the condition.
Predict Output
expert
3: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;
}
A12 13 21 31
B12 21 31
C12 13 21 23 31
D11 22 33
Attempts:
2 left
💡 Hint
Pay attention to when continue skips printing and when break stops the inner loop.