Code of the Day
BeginnerControl flow

break, continue, and loop patterns

Control loop execution with break and continue, and recognise common loop patterns like search, accumulation, and early exit.

CBeginner9 min read
Recommended first
By the end of this lesson you will be able to:
  • Use break to exit a loop early
  • Use continue to skip the rest of an iteration
  • Distinguish search loops from accumulation loops
  • Recognise and avoid infinite loops that are missing an exit condition

The for and while loops you have learned so far exit when their condition becomes false. But sometimes you need to exit based on something that happens inside the loop body, or skip a particular iteration without stopping the whole loop. break and continue give you that control.

break — exit the loop immediately

break exits the innermost enclosing loop unconditionally:

#include <stdio.h>

int main(void) {
    for (int i = 0; i < 10; i++) {
        if (i == 5) {
            break; /* exit the loop when i reaches 5 */
        }
        printf("%d ", i);
    }
    printf("\n");
    /* Prints: 0 1 2 3 4 */
    return 0;
}

When i reaches 5, break executes and the loop is done. The remaining values (5 through 9) are never printed.

Search pattern. The most common use of break is to stop searching once you have found what you need:

#include <stdio.h>

int main(void) {
    int arr[] = {3, 7, 2, 9, 4, 1, 8};
    int target = 9;
    int n = 7;
    int found = -1; /* -1 means "not found" */

    for (int i = 0; i < n; i++) {
        if (arr[i] == target) {
            found = i;
            break;
        }
    }

    if (found >= 0) {
        printf("Found %d at index %d\n", target, found);
    } else {
        printf("%d not found\n", target);
    }

    return 0;
}

continue — skip to the next iteration

continue skips the rest of the current loop body and jumps to the next iteration (updating the loop variable in a for loop, re-evaluating the condition in a while loop):

#include <stdio.h>

int main(void) {
    for (int i = 0; i < 10; i++) {
        if (i % 2 == 0) {
            continue; /* skip even numbers */
        }
        printf("%d ", i);
    }
    printf("\n");
    /* Prints: 1 3 5 7 9 */
    return 0;
}

continue is most useful when you want to skip some items but process others — for example, skipping blank lines when reading a file, or ignoring zero values when computing an average.

break and continue only affect the innermost loop. In nested loops, break exits only the loop it is directly inside, not all outer loops. To exit multiple nested loops, use a flag variable or restructure the code into a function (where return exits the whole function).

Common loop patterns

Accumulation (sum, product, count):

int sum = 0;
for (int i = 1; i <= n; i++) {
    sum += i;
}

Accumulate a running total. The key is to initialise the accumulator before the loop and update it inside.

Linear search with break:

int pos = -1;
for (int i = 0; i < n; i++) {
    if (arr[i] == target) { pos = i; break; }
}

The pos = -1 sentinel distinguishes "not found" from "found at index 0."

Filtering (process only matching items):

int count = 0;
for (int i = 0; i < n; i++) {
    if (arr[i] < 0) { continue; } /* skip negatives */
    count++;
}

Maximum or minimum:

int max = arr[0];
for (int i = 1; i < n; i++) {
    if (arr[i] > max) {
        max = arr[i];
    }
}

Initialise with the first element (never with a magic number like -9999), then update when you find something larger.

A practical example: counting words

#include <stdio.h>
#include <ctype.h>

int main(void) {
    const char *text = "Hello world this is C";
    int words = 0;
    int in_word = 0;

    for (int i = 0; text[i] != '\0'; i++) {
        if (text[i] == ' ') {
            in_word = 0;
        } else if (!in_word) {
            in_word = 1;
            words++;
        }
    }

    printf("Words: %d\n", words); /* 5 */
    return 0;
}

The loop walks each character, tracking whether the previous character was a space. When it transitions from space to non-space, a new word starts.

goto exists in C but avoid it. C has a goto statement that jumps to a labelled line. It can exit nested loops in one step, but it makes control flow hard to follow. The Linux kernel uses it for cleanup in error paths (a legitimate idiom), but for everything else — especially as a beginner — restructure your code instead of using goto.

Where to go next

Next: the FizzBuzz lab — you will use if/else, for loops, break, and continue together to write the classic FizzBuzz program in C.

Finished reading? Mark it complete to track your progress.

On this page