C ยท Chapter 16 of 45

C Break and Continue

The break statement immediately exits the nearest enclosing loop or switch statement. The continue statement skips the rest of the current iteration and jumps to the next one.

Both are useful for controlling loop flow precisely, such as stopping early when a target is found, or skipping invalid values without stopping the whole loop.

Syntax
break;
continue;

break

break stops loop execution entirely and jumps to the code right after the loop. It's often used to exit a loop early once a condition is satisfied, like finding a target value.

continue

continue skips the remaining code in the current loop iteration and moves directly to the next one, without exiting the loop entirely.

Example 1 (c)
#include <stdio.h>

int main() {
  for (int i = 0; i < 10; i++) {
    if (i == 3) break;
    printf("%d\n", i);
  }
  return 0;
}
Output
0
1
2

The loop stops entirely as soon as i equals 3.

Example 2 (c)
#include <stdio.h>

int main() {
  for (int i = 0; i < 5; i++) {
    if (i % 2 == 0) continue;
    printf("%d\n", i);
  }
  return 0;
}
Output
1
3

Even numbers are skipped with continue, so only odd numbers print.

Key points

  • break exits the nearest loop or switch immediately.
  • continue skips to the next iteration of the loop.
  • Both work inside for, while and do...while loops.
  • break also works inside switch statements to prevent fall-through.
๐Ÿ’ก Note: Overusing break and continue can make loop logic harder to follow, so use them sparingly and clearly.

๐Ÿ“ Quick Quiz

1. What does break do inside a loop?

2. What does continue do inside a loop?

3. Can break be used inside a switch statement?