Recall & Review
beginner Click to reveal answer
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.beginner Click to reveal answer
In which control structures can you use the
break statement?You can use
break inside for, while, do-while loops, and switch statements.intermediate Click to reveal answer
What happens if you use
break inside nested loops?The
break statement only exits the innermost loop where it is used, not all loops.beginner Click to reveal answer
Can
break be used outside loops or switch statements?No, using
break outside loops or switch causes a compile-time error.beginner Click to reveal answer
Example: What will this code print?
for(int i = 0; i < 5; i++) {
if(i == 3) break;
printf("%d ", i);
}It will print:
The loop stops when
0 1 2 The loop stops when
i equals 3 because of the break.What does the
break statement do inside a loop?✗ Incorrect
The
break statement immediately stops the loop and exits it.Where can you NOT use the
break statement?✗ Incorrect
Using
break outside loops or switch causes a compile error.If you have nested loops, which loop does
break exit?✗ Incorrect
break exits only the innermost loop it is inside.What will happen if
break is used inside a switch statement?✗ Incorrect
break exits the switch statement and continues with the code after it.What will this code print?
int i = 0;
while(i < 5) {
if(i == 2) break;
printf("%d ", i);
i++;
}
✗ Incorrect
The loop stops when
i is 2 due to break, so it prints 0 and 1.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.
