0
0
C++programming~5 mins

Break statement in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the break statement do in C++?
The break statement immediately exits the nearest enclosing loop or switch statement, skipping the rest of its body.
Click to reveal answer
beginner
In which control structures can you use the break statement?
You can use break inside for, while, do-while loops, and switch statements.
Click to reveal answer
intermediate
What happens if you use break inside nested loops?
The break statement only exits the innermost loop where it is used, not all loops.
Click to reveal answer
beginner
Can break be used outside loops or switch statements?
No, using break outside loops or switch causes a compile-time error.
Click to reveal answer
beginner
Example: What will this code print?<br>
for (int i = 0; i < 5; i++) {<br>  if (i == 3) break;<br>  std::cout << i << ' ';<br>}
It will print: 0 1 2 <br>Because when i becomes 3, the break stops the loop immediately.
Click to reveal answer
What does the break statement do inside a loop?
ARestarts the loop from the beginning
BSkips the current iteration and continues
CStops the loop immediately and exits it
DDoes nothing special
Where can you NOT use the break statement?
AOutside any loop or <code>switch</code>
BInside a <code>switch</code> statement
CInside a <code>while</code> loop
DInside a <code>for</code> loop
If you have nested loops, which loop does break exit?
AOnly the innermost loop where <code>break</code> is used
BAll loops at once
COnly the outermost loop
DNone of the loops
What will happen if break is used inside a switch statement?
AIt causes a syntax error
BIt exits the entire function
CIt restarts the <code>switch</code>
DIt exits the <code>switch</code> and continues after it
What is the output of this code?<br>
int i = 0;<br>while (i < 3) {<br>  if (i == 1) break;<br>  std::cout << i << ' ';<br>  i++;<br>}
A0 1 2
B0
C1 2
DNo output
Explain how the break statement affects loops and switch statements in C++.
Think about what happens when you want to stop a loop early.
You got /4 concepts.
    Describe a real-life situation where using a break statement in a loop would be helpful.
    Imagine looking for something and stopping once you find it.
    You got /4 concepts.