0
0
Cprogramming~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>  printf("%d ", i);<br>}
It will print: 0 1 2 <br>The loop stops when i equals 3 because of the break.
Click to reveal answer
What does the break statement do inside a loop?
AStops the loop immediately and exits it
BSkips the current iteration and continues
CRestarts the loop from the beginning
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?
ANone of the loops
BThe outermost loop
CAll loops at once
DThe innermost loop where <code>break</code> is used
What will happen if break is used inside a switch statement?
AIt restarts the <code>switch</code>
BIt exits the entire program
CIt exits the <code>switch</code> and continues after it
DIt causes a syntax error
What will this code print?<br>
int i = 0;<br>while(i < 5) {<br>  if(i == 2) break;<br>  printf("%d ", i);<br>  i++;<br>}
A0 1 2 3 4
B0 1
C2 3 4
DNothing
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 in a list and stopping once you find it.
    You got /4 concepts.