Bird
Raised Fist0
C++programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a loop in C++ programming?
easy
A. To repeat a block of code multiple times
B. To store multiple values in a variable
C. To create a new data type
D. To stop the program execution immediately

Solution

  1. Step 1: Understand the role of loops

    Loops are used to repeat a set of instructions multiple times without writing the code again and again.
  2. Step 2: Identify the correct purpose

    Among the options, only repeating code matches the loop's purpose.
  3. Final Answer:

    To repeat a block of code multiple times -> Option A
  4. Quick Check:

    Loop purpose = repeat code [OK]
Hint: Loops repeat code blocks to save effort [OK]
Common Mistakes:
  • Confusing loops with variables
  • Thinking loops create new data types
  • Believing loops stop program immediately
2. Which of the following is the correct syntax for a for loop in C++?
easy
A. for (int i = 0; i < 5; i++) /* code */
B. for int i = 0; i < 5; i++ { /* code */ }
C. for (int i = 0; i < 5; i++) { /* code */ }
D. for (int i = 0; i < 5; i++) [] { /* code */ }

Solution

  1. Step 1: Recall the correct for loop syntax

    The for loop syntax requires parentheses around initialization, condition, and increment, followed by a block in braces.
  2. Step 2: Check each option

    for (int i = 0; i < 5; i++) { /* code */ } matches the correct syntax exactly. Others miss parentheses, braces, or use wrong symbols.
  3. Final Answer:

    for (int i = 0; i < 5; i++) { /* code */ } -> Option C
  4. Quick Check:

    Correct for loop syntax = for (int i = 0; i < 5; i++) { /* code */ } [OK]
Hint: For loops need parentheses and braces [OK]
Common Mistakes:
  • Omitting parentheses around conditions
  • Missing braces for loop body
  • Using wrong symbols like [] instead of {}
3. What will be the output of the following code?
int count = 0;
for (int i = 1; i <= 3; i++) {
  count += i;
}
std::cout << count;
medium
A. Error
B. 6
C. 0
D. 3

Solution

  1. Step 1: Trace the loop iterations

    The loop runs with i = 1, 2, 3. Each time, count adds i: 0+1=1, 1+2=3, 3+3=6.
  2. Step 2: Determine final count value

    After the loop, count is 6, which is printed.
  3. Final Answer:

    6 -> Option B
  4. Quick Check:

    Sum 1+2+3 = 6 [OK]
Hint: Add loop variable each iteration to sum quickly [OK]
Common Mistakes:
  • Confusing loop end condition
  • Forgetting to add inside loop
  • Assuming count resets inside loop
4. Identify the error in this loop code:
int i = 0;
while (i < 3) {
  std::cout << i << " ";
}
medium
A. Variable i should be declared inside the loop
B. Incorrect while loop syntax
C. Missing semicolon after std::cout
D. Missing increment of i inside the loop

Solution

  1. Step 1: Analyze the while loop behavior

    The loop condition is i < 3, but i never changes inside the loop, causing an infinite loop.
  2. Step 2: Identify the missing increment

    To progress, i must increase inside the loop, e.g., i++.
  3. Final Answer:

    Missing increment of i inside the loop -> Option D
  4. Quick Check:

    Loop needs variable update to avoid infinite loop [OK]
Hint: Always update loop variable inside while loops [OK]
Common Mistakes:
  • Forgetting to increment loop variable
  • Thinking syntax error causes infinite loop
  • Declaring variable inside loop unnecessarily
5. Consider this code snippet:
int sum = 0;
for (int i = 1; i <= 5; i++) {
  if (i % 2 == 0) continue;
  sum += i;
}
std::cout << sum;

What is the output and why?
hard
A. 9, because only odd numbers 1, 3, 5 are added
B. 15, because all numbers 1 to 5 are added
C. 10, because even numbers are added
D. 0, because continue skips all additions

Solution

  1. Step 1: Understand the loop and condition

    The loop runs i from 1 to 5. If i is even, continue skips adding it to sum.
  2. Step 2: Calculate sum of odd numbers

    Odd numbers are 1, 3, 5. Their sum is 1+3+5 = 9.
  3. Final Answer:

    9, because only odd numbers 1, 3, 5 are added -> Option A
  4. Quick Check:

    Sum odd 1+3+5 = 9 [OK]
Hint: Continue skips even numbers, sum only odds [OK]
Common Mistakes:
  • Adding even numbers by mistake
  • Ignoring continue statement effect
  • Summing all numbers instead of filtered ones
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard