Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 5 of 8

break, continue & goto

`break` exits the innermost loop or switch. `continue` skips the rest of the current loop iteration and jumps to the next condition check. `goto` unconditionally jumps to a labelled statement - controversial but legitimate for error-cleanup paths in C, where multi-level return cleanup is otherwise verbose and error-prone. The Linux kernel uses `goto` extensively for this purpose.

11 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

break, continue, goto - With Real Use Cases

break, continue, goto - With Real Use Cases
#include <stdio.h>
#include <stdlib.h>

/* ── break - exit innermost loop or switch ──────────────── */
void find_first_negative(void) {
    int arr[] = {3, 7, 1, -5, 8, -2};
    int n = (int)(sizeof(arr) / sizeof(arr[0]));

    for (int i = 0; i < n; i++) {
        if (arr[i] < 0) {
            printf("First negative: %d at index %d
", arr[i], i);
            break;   /* exit loop immediately */
        }
    }
    /* break from nested loops: only breaks the INNER loop */
    for (int r = 0; r < 3; r++) {
        for (int c = 0; c < 3; c++) {
            if (r == 1 && c == 1) break;   /* breaks inner loop only */
            printf("(%d,%d) ", r, c);
        }
    }
    printf("
");
}

/* ── continue - skip to next iteration ──────────────────── */
void print_odds(void) {
    for (int i = 1; i <= 10; i++) {
        if (i % 2 == 0) continue;   /* skip even numbers */
        printf("%d ", i);
    }
    printf("
");   /* 1 3 5 7 9 */
}

/* ── goto - legitimate error-cleanup pattern ────────────── */
int process_file(const char *path) {
    FILE *fp     = NULL;
    char *buffer = NULL;
    int   result = -1;

    fp = fopen(path, "r");
    if (fp == NULL) {
        fprintf(stderr, "Cannot open %s
", path);
        goto cleanup;   /* jump to cleanup, skipping further setup */
    }

    buffer = malloc(1024);
    if (buffer == NULL) {
        fprintf(stderr, "Out of memory
");
        goto cleanup;  /* safe: fp was opened, will be closed below */
    }

    /* ... use fp and buffer ... */
    result = 0;   /* success */

cleanup:         /* label: everything opened so far is freed here */
    free(buffer);          /* free(NULL) is safe - no-op */
    if (fp) fclose(fp);
    return result;
}
/* This pattern avoids deeply nested if/else and guarantees
   cleanup regardless of which step fails.                  */

int main(void) {
    find_first_negative();
    print_odds();
    int r = process_file("/etc/hostname");
    printf("process_file returned %d
", r);
    return 0;
}

Common Mistakes

  • Using `goto` to jump forward AND backward - backward `goto` creates loops that are harder to follow than `while`. Use `goto` only for forward jumps to a cleanup/error label at the end of a function.
  • Using `break` expecting it to break outer loops - `break` only exits the innermost loop or switch. For multi-level exit, use a flag variable, restructure with a function, or (in rare cases) `goto`.
  • `continue` in `do-while` jumps to the condition - in `for` loops, `continue` jumps to the update expression then checks the condition. In `do-while`, it jumps directly to the condition. This is occasionally surprising.

Tip

Tip

Practice break continue goto in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Break exits loop immediately, continue skips to next iteration, goto provides clean error unwind

Practice Task

Note

Practice Task - (1) Write a working example of break continue goto from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with break continue goto is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.

Key Takeaways

  • `break` exits the innermost loop or switch.
  • Using `goto` to jump forward AND backward - backward `goto` creates loops that are harder to follow than `while`. Use `goto` only for forward jumps to a cleanup/error label at the end of a function.
  • Using `break` expecting it to break outer loops - `break` only exits the innermost loop or switch. For multi-level exit, use a flag variable, restructure with a function, or (in rare cases) `goto`.
  • `continue` in `do-while` jumps to the condition - in `for` loops, `continue` jumps to the update expression then checks the condition. In `do-while`, it jumps directly to the condition. This is occasionally surprising.

Topics in This Module

0/8 done