Bird
Raised Fist0
C++programming~20 mins

Break statement in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Break Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of break in a for loop
What is the output of this C++ code snippet?
C++
#include <iostream>
int main() {
    for (int i = 0; i < 5; i++) {
        if (i == 3) {
            break;
        }
        std::cout << i << " ";
    }
    return 0;
}
A0 1 2 3
B0 1 2 3 4
C3 4
D0 1 2
Attempts:
2 left
💡 Hint
The break statement stops the loop when i equals 3.
❓ Predict Output
intermediate
2:00remaining
Break inside nested loops
What is the output of this C++ code?
C++
#include <iostream>
int main() {
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (j == 1) {
                break;
            }
            std::cout << i << j << " ";
        }
    }
    return 0;
}
A00 10 20
B00 01 10 11 20 21
C00 10 20 21 22
D00 01 02 10 11 12 20 21 22
Attempts:
2 left
💡 Hint
Break stops the inner loop when j equals 1.
❓ Predict Output
advanced
2:00remaining
Break in while loop with condition
What is the output of this C++ program?
C++
#include <iostream>
int main() {
    int count = 0;
    while (true) {
        if (count == 4) {
            break;
        }
        std::cout << count << " ";
        count++;
    }
    return 0;
}
A0 1 2 3 4 5
B0 1 2 3 4
C0 1 2 3
D1 2 3 4
Attempts:
2 left
💡 Hint
Break stops the loop when count reaches 4, so 4 is not printed.
❓ Predict Output
advanced
2:00remaining
Break effect on switch-case
What is the output of this C++ code?
C++
#include <iostream>
int main() {
    int x = 2;
    switch (x) {
        case 1:
            std::cout << "One ";
            break;
        case 2:
            std::cout << "Two ";
            break;
        case 3:
            std::cout << "Three ";
        default:
            std::cout << "Default ";
    }
    return 0;
}
AOne Two
BTwo
CTwo Default
DOne Two Default
Attempts:
2 left
💡 Hint
Break stops execution after printing "Two ".
🧠 Conceptual
expert
3:00remaining
Break statement behavior in nested loops
Consider this C++ code snippet. Which statement is true about the effect of the break statement inside the inner loop?
C++
#include <iostream>
int main() {
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (i == j) {
                break;
            }
            std::cout << i << j << " ";
        }
    }
    return 0;
}
AThe break stops only the inner loop when i equals j, outer loop continues.
BThe break stops both inner and outer loops immediately when i equals j.
CThe break causes a compile-time error because it is inside nested loops.
DThe break stops the outer loop only, inner loop continues.
Attempts:
2 left
💡 Hint
Break affects only the loop it is directly inside.

Practice

(1/5)
1.

What does the break statement do inside a loop in C++?

easy
A. It skips the current iteration and continues.
B. It pauses the loop until user input.
C. It restarts the loop from the beginning.
D. It immediately exits the loop.

Solution

  1. Step 1: Understand the role of break in loops

    The break statement is used to stop the loop immediately when executed.
  2. Step 2: Compare with other loop controls

    Unlike continue which skips to the next iteration, break exits the loop completely.
  3. Final Answer:

    It immediately exits the loop. -> Option D
  4. Quick Check:

    Break stops loop = exit loop [OK]
Hint: Break means stop loop now, not skip or pause [OK]
Common Mistakes:
  • Confusing break with continue
  • Thinking break pauses instead of exits
  • Assuming break restarts the loop
2.

Which of the following is the correct syntax to use break inside a for loop in C++?

for (int i = 0; i < 5; i++) {
    if (i == 3) {
        ?
    }    // other code
}
easy
A. stop;
B. break;
C. exit;
D. continue;

Solution

  1. Step 1: Recall the keyword to exit loops

    The keyword to exit a loop immediately in C++ is break;.
  2. Step 2: Check other options for correctness

    stop; and exit; are invalid here, and continue; skips iteration, not exit.
  3. Final Answer:

    break; -> Option B
  4. Quick Check:

    Use break; to exit loops [OK]
Hint: Break always ends loop, continue skips iteration [OK]
Common Mistakes:
  • Using continue instead of break
  • Writing stop or exit which are invalid here
  • Missing semicolon after break
3.

What is the output of the following C++ code?

for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        break;
    }
    std::cout << i << " ";
}
medium
A. 3 4 5
B. 1 2 3 4 5
C. 1 2
D. No output

Solution

  1. Step 1: Trace the loop iterations

    The loop starts with i=1, prints 1, then i=2, prints 2.
  2. Step 2: Check the break condition

    When i becomes 3, the if condition is true and break stops the loop immediately, so 3 is not printed.
  3. Final Answer:

    1 2 -> Option C
  4. Quick Check:

    Break stops loop before printing 3 [OK]
Hint: Break stops before printing the break condition value [OK]
Common Mistakes:
  • Including the break value in output
  • Ignoring break and printing all
  • Confusing break with continue
4.

Identify the error in this C++ code snippet using break:

int i = 0;
if (i == 0) {
    break;
}
medium
A. break cannot be used outside loops or switch.
B. Missing semicolon after break.
C. Variable i is not declared.
D. If condition syntax is wrong.

Solution

  1. Step 1: Check where break is used

    The break statement is inside an if but not inside a loop or switch.
  2. Step 2: Understand break usage rules

    In C++, break can only be used inside loops or switch statements, not standalone in if.
  3. Final Answer:

    break cannot be used outside loops or switch. -> Option A
  4. Quick Check:

    Break only inside loops/switch [OK]
Hint: Break only works inside loops or switch blocks [OK]
Common Mistakes:
  • Using break in if without loop
  • Forgetting break scope rules
  • Assuming break works like return
5.

Consider this nested loop code:

for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        if (j == 2) {
            break;
        }
        std::cout << i << j << " ";
    }
}

What is the output?

hard
A. 11 21 31
B. 11 12 13 21 22 23 31 32 33
C. 11 12 21 22 31 32
D. 11 21 22 31 32

Solution

  1. Step 1: Analyze inner loop behavior

    The inner loop runs j=1 to 3, but breaks when j==2, so it only prints when j=1.
  2. Step 2: Trace outer loop iterations

    For i=1, prints 11; i=2 prints 21; i=3 prints 31. No other j values print due to break.
  3. Final Answer:

    11 21 31 -> Option A
  4. Quick Check:

    Break stops inner loop at j=2, prints only j=1 [OK]
Hint: Break stops inner loop early, only first j prints per i [OK]
Common Mistakes:
  • Assuming all j values print
  • Confusing break effect on outer loop
  • Ignoring break inside nested loops
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