break and continue in C
Sometimes you want to stop a loop early: "I found what I was looking for, no need to check further." Sometimes you want to skip just one round: "This number is negative, ignore it and move to the next."
C gives you two keywords for this: break and continue. In this lesson you will learn both and the difference between them, a very common exam question.
What You Will Learn
- What
breakdoes inside a loop. - What
continuedoes inside a loop. - The difference table between break and continue.
- How break works in nested loops.
- A real program using both.
break and continue in C Explained
Exam definition: break immediately terminates the nearest enclosing loop (or switch) and continues after it. continue skips the rest of the current iteration and jumps to the next iteration of the loop.
| Point | break | continue |
|---|---|---|
| What it does | Exits the loop completely | Skips to the next iteration |
| Loop after it | Ends | Keeps running |
| Used in | Loops and switch | Loops only (not switch) |
| In a for loop, goes to | The statement after the loop | The update part (i++), then the condition |
| Example use | Stop searching once found | Ignore invalid values |
Memory trick: Watching a series: break = switch off the TV (stop watching). continue = skip this episode and play the next one.
Syntax of break and continue
for (...)
{
if (condition1)
break; /* leave the loop now */
if (condition2)
continue; /* skip the rest, go to next iteration */
/* rest of the body */
}
Example 1: break vs continue Side by Side
#include <stdio.h>
int main(void)
{
printf("break at 6 : ");
for (int i = 1; i <= 10; i++)
{
if (i == 6)
break;
printf("%d ", i);
}
printf("\ncontinue at 3: ");
for (int i = 1; i <= 5; i++)
{
if (i == 3)
continue;
printf("%d ", i);
}
printf("\n");
return 0;
}
Output
break at 6 : 1 2 3 4 5 continue at 3: 1 2 4 5
Code Explanation
- break: when i becomes 6,
breakruns and the loop ends immediately. 6 to 10 are never printed. - continue: when i is 3,
continueskips the printf for that round only. The loop goes on with 4 and 5. - In a for loop,
continuestill runs the update (i++), so the loop doesn't get stuck.
How break and continue Work
- break jumps to the first statement after the loop.
- continue jumps to the end of the current iteration: in a for loop that is the update; in while and do-while it is the condition check.
- In a while loop, make sure the update happens before
continue, or you may create an infinite loop. - In nested loops, break and continue affect only the innermost loop they are in, not the outer one.
Example 2: Add Positive Numbers, Skip Negatives, Stop at 0
#include <stdio.h>
int main(void)
{
int n, sum = 0;
while (1)
{
printf("Enter a number (0 to stop): ");
scanf("%d", &n);
if (n == 0)
break; /* stop the loop */
if (n < 0)
{
printf("Skipping negative number\n");
continue; /* ignore this one */
}
sum += n;
}
printf("Sum of positive numbers = %d\n", sum);
return 0;
}
Output
Enter a number (0 to stop): 5 Enter a number (0 to stop): -2 Skipping negative number Enter a number (0 to stop): 8 Enter a number (0 to stop): 0 Sum of positive numbers = 13
while (1) would run forever, but break gives it an exit. A prime-number check uses the same idea: stop the loop with break as soon as a divisor is found. See our prime number program in C.
Important Points to Remember
- break = exit the loop. continue = skip to the next iteration.
- break works in loops and switch; continue only in loops.
- Both affect only the innermost loop.
- In a for loop, continue still runs the update.
while (1)+breakis a common pattern.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
continue before i++ in a while loop | Infinite loop | Update before continue |
| Expecting break to exit all nested loops | Only the inner loop ends | Use a flag variable (or goto, rarely) |
| Using continue inside a switch (not in a loop) | Compile error | continue only works in loops |
| break outside any loop or switch | Compile error | Use it only inside loops/switch |
break and continue Practice Questions
- Write three differences between break and continue.
- Can continue be used in a switch statement?
- What is the output of
for (i = 1; i <= 5; i++) { if (i == 4) break; printf("%d", i); }? - What is the output of
for (i = 1; i <= 5; i++) { if (i % 2 == 0) continue; printf("%d", i); }? - In nested loops, which loop does break exit?
Show answers
- break exits the loop, continue skips to the next iteration; break works in switch, continue doesn't; after break the loop ends, after continue it keeps running.
- No (only if the switch is itself inside a loop, and then continue affects the loop).
123135- Only the innermost loop that contains it.
Coding Exercise
Print numbers from 1 to 20 but skip multiples of 3, and stop completely when you reach a number greater than 14.
Show solution
#include <stdio.h>
int main(void)
{
for (int i = 1; i <= 20; i++)
{
if (i > 14)
break;
if (i % 3 == 0)
continue;
printf("%d ", i);
}
printf("\n");
return 0;
}
1 2 4 5 7 8 10 11 13 14
Quick Quiz: break vs continue (MCQ)
-
Which statement ends a loop immediately?
A) continue B) break C) return 1 D) goto only
-
Which statement skips only the current iteration?
A) continue B) break C) exit D) stop
-
Which can be used in a switch?
A) continue B) break C) Both D) Neither
-
Output:
for(i=1;i<=3;i++){ if(i==2) continue; printf("%d",i); }A) 123 B) 13 C) 1 D) 23
-
In nested loops, break exits:
A) All loops B) The outermost loop C) The innermost loop containing it D) The program
Show answers
- B: break.
- A: continue.
- B: break.
- B: 13.
- C: the innermost loop containing it.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| break | Exits the loop (or switch) |
|---|---|
| continue | Skips to the next iteration |
| break used in | Loops and switch |
| continue used in | Loops only |
| Nested loops | Affect only the innermost loop |
| continue in for | Goes to the update, then condition |
Frequently asked questions
- What is the difference between break and continue?
- break ends the loop; continue skips the rest of this iteration and goes to the next.
- Can break exit two loops at once?
- No. It exits only the innermost loop; use a flag to exit outer loops.
- Can continue be used without a loop?
- No, it is a compile error outside a loop.
Remember
- break = switch off the TV; continue = skip the episode.
- Both act on the innermost loop.
Next Topic
Next lesson: Infinite Loops in C. You will learn why loops sometimes never end, and how to fix them.