0
0
C++programming~20 mins

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

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