C Break and Continue

break and continue are jump statements that change the normal path through a loop. break leaves a loop immediately, while continue skips the rest of the current iteration and starts the next one. They matter because real loops often need exceptions: stop after finding a match, ignore invalid data, or skip work that does not apply.

Overview: How It Works

Most loops run in a predictable cycle: test a condition, run the body, update loop state, then test again. break and continue interrupt that cycle from inside the loop body. They do not create variables, allocate memory, or return a value. They only change where execution continues next.

When C executes break inside a for, while, or do...while loop, control jumps to the first statement after that loop. The loop condition is not checked again, and the rest of the loop body is skipped. In a switch, break also exits the nearest switch statement; this is why switch cases commonly end with break.

When C executes continue, it does not leave the whole loop. It abandons only the current iteration. In a for loop, control jumps to the update expression, such as i++, and then the loop condition is tested. In a while or do...while loop, control jumps directly to the next condition test. This difference is important because code skipped by continue may include a counter update in a while loop.

Both statements affect only the nearest enclosing loop or switch. If a break appears in an inner loop, it exits that inner loop, not an outer loop. If a continue appears inside an inner loop, it continues the inner loop. To stop multiple levels, restructure the code with a flag, a function and return, or a clearer loop condition.

Syntax

break;
continue;
Statement Where it can appear Effect
break; Inside a loop or switch Exits the nearest loop or switch immediately.
continue; Inside a loop Skips the rest of the current loop iteration.

The semicolon is part of the statement. These statements are usually placed inside an if so they run only for a specific condition.

Examples

Example 1: Stop a Search With break

#include <stdio.h>

int main(void)
{
    for (int number = 1; number <= 6; number++) {
        printf("Checking %d\n", number);

        if (number == 4) {
            printf("Found 4\n");
            break;
        }
    }

    printf("Search ended\n");

    return 0;
}

Output:

Checking 1
Checking 2
Checking 3
Checking 4
Found 4
Search ended

The loop could count through 6, but it stops as soon as number == 4. After break, execution continues after the loop, so 5 and 6 are never checked.

Example 2: Skip Values With continue

#include <stdio.h>

int main(void)
{
    for (int number = 1; number <= 8; number++) {
        if (number % 2 == 0) {
            continue;
        }

        printf("%d is odd\n", number);
    }

    return 0;
}

Output:

1 is odd
3 is odd
5 is odd
7 is odd

For even numbers, the condition is true and continue skips the printf. In a for loop, the update expression still runs, so number++ happens before the next condition check.

Example 3: Combine Both Statements in a Data Loop

#include <stdio.h>

int main(void)
{
    int readings[] = {18, 21, -1, 24, 0, 19};
    int length = 6;
    int total = 0;
    int used = 0;

    for (int i = 0; i < length; i++) {
        if (readings[i] < 0) {
            printf("Skipped invalid reading: %d\n", readings[i]);
            continue;
        }

        if (readings[i] == 0) {
            printf("Stop marker found\n");
            break;
        }

        total += readings[i];
        used++;
    }

    printf("Readings used: %d\n", used);
    printf("Total: %d\n", total);

    return 0;
}

Output:

Skipped invalid reading: -1
Stop marker found
Readings used: 3
Total: 63

The negative reading is invalid, so continue skips the addition and moves to the next reading. The 0 reading is a stop marker, so break ends the loop completely. The value 19 after the stop marker is not processed.

Example 4: break Only Exits the Nearest Loop

#include <stdio.h>

int main(void)
{
    for (int row = 1; row <= 3; row++) {
        for (int col = 1; col <= 4; col++) {
            if (col == 3) {
                break;
            }

            printf("row %d col %d\n", row, col);
        }
    }

    return 0;
}

Output:

row 1 col 1
row 1 col 2
row 2 col 1
row 2 col 2
row 3 col 1
row 3 col 2

When col becomes 3, break exits only the inner col loop. The outer row loop continues with the next row.

How It Works Step by Step

  1. Execution enters a loop normally.
  2. Statements run in order until C reaches an if containing break or continue.
  3. If the condition is false, the jump statement is skipped and the loop continues normally.
  4. If break runs, C jumps to the statement after the nearest loop or switch.
  5. If continue runs, C skips the remaining statements in the current iteration.
  6. For a for loop, continue then runs the update expression before retesting the condition. For a while loop, it retests the condition immediately.

A useful way to read loop code is to ask whether a line must happen for every iteration. If yes, be careful not to put continue before it.

Common Mistakes

Using continue Before a Required Update

In a while loop, a continue can skip the counter update and cause an infinite loop. A wrong pattern is if (number == 3) continue; before number++. Put the update before the possible continue, or use a for loop when the update is part of the loop header.

#include <stdio.h>

int main(void)
{
    int number = 0;

    while (number < 5) {
        number++;

        if (number == 3) {
            continue;
        }

        printf("Processed %d\n", number);
    }

    return 0;
}

Output:

Processed 1
Processed 2
Processed 4
Processed 5

Using break When You Meant continue

If one bad item should be ignored but later items are still useful, use continue. Use break only when the whole loop should end. Confusing the two can silently drop valid work after the first skipped item.

Expecting break to Leave Every Nested Loop

A break inside an inner loop exits only that inner loop. If an outer search should end too, consider moving the search into a function and using return when the answer is found.

#include <stdio.h>

int main(void)
{
    int scores[] = {72, 88, 91, 67, 95};
    int length = 5;
    int first_high = -1;

    for (int i = 0; i < length; i++) {
        if (scores[i] < 90) {
            continue;
        }

        first_high = i;
        break;
    }

    if (first_high >= 0) {
        printf("First score above 90 is %d at index %d\n", scores[first_high], first_high);
    } else {
        printf("No high score found\n");
    }

    return 0;
}

Output:

First score above 90 is 91 at index 2

This version uses continue for scores that do not qualify and break after the first qualifying score is saved.

Best Practices

  • Use break when continuing the loop would be wasteful or incorrect, such as after finding a target.
  • Use continue to handle guard conditions at the top of a loop: invalid input, disabled items, or values outside a range.
  • Keep the normal path easy to see. Too many break and continue statements can make a loop harder to trace.
  • In while loops, update counters or input state before any continue that might skip them.
  • Remember that break in a switch exits the switch, not necessarily an enclosing loop.
  • Prefer clear loop conditions for ordinary stopping rules and reserve break for early exits that improve clarity.
  • For nested loops, choose a deliberate strategy if more than one level must stop.

Practice Exercises

  1. Write a program that loops through numbers 1 to 20, skips multiples of 3, and prints the rest.
  2. Create an array of test scores. Stop at the first score below 0, but skip scores greater than 100 as invalid data.
  3. Write a nested-loop program that prints coordinates, but stops each row when the column reaches a chosen limit. Then modify it so the whole search stops after one target coordinate is found.

Summary

  • break exits the nearest loop or switch immediately.
  • continue skips the rest of the current loop iteration.
  • In a for loop, continue still runs the update expression before the next condition test.
  • In a while loop, continue can skip important update code if you place it too early.
  • Both statements affect only the nearest enclosing loop, so nested loops require extra care.
  • Used well, these statements make loops clearer by separating special cases from the main work.