- Asked 3/16
- 2070 Asar · 4 marks
- 2069 Chaitra · 3 marks
- 2072 Kartik · 2 marks
Break versus continue
Short answer
break terminates the loop entirely, transferring control after the loop; continue skips the rest of the current iteration and proceeds to the next. Example: break in a loop for i=1..10 when i==5 yields 1 2 3 4; continue skips just 5, yielding 1 2 3 4 6 7 8 9 10.
Distinguish between break and continue statement with example.
break | continue |
|---|---|
Terminates the loop (or switch) completely | Skips the rest of the current iteration only |
| Control goes to the statement after the loop | Control goes to the next iteration (loop condition or update) |
Used in loops and switch | Used only in loops |
Needed to end each case of a switch | Cannot be used to end a switch case |
#include <stdio.h>
int main(void)
{
int i;
printf("break : ");
for (i = 1; i <= 10; i++)
{
if (i == 5)
break; /* leaves the loop */
printf("%d ", i);
}
printf("\ncontinue: ");
for (i = 1; i <= 10; i++)
{
if (i == 5)
continue; /* skips only i = 5 */
printf("%d ", i);
}
printf("\n");
return 0;
}
Output:
break : 1 2 3 4
continue: 1 2 3 4 6 7 8 9 10
With break, the loop stops at 5; with continue, only 5 is skipped and the loop goes on up to 10.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Purpose of continue statement · Computer Programming, chapter 5