Loading...

C essentialsControl Flow

break, continue & goto

7 min lesson

Next
  • Jump statements change the normal flow of a loop or switch by leaving early or skipping ahead.
  • They matter because sometimes you want to stop searching once you find a match, or skip an item that does not apply.
  • Think of break as leaving the room immediately, continue as skipping to the next item on a list, and goto as teleporting to a labeled spot (powerful but easy to misuse).
  • break exits the innermost loop or switch right away and continues after it.
  • continue skips the rest of the current iteration and jumps to the next pass of the loop.
  • goto jumps to a named label anywhere in the same function. It is rarely needed and can make code hard to follow.
  • The computer simply changes the instruction pointer: break and continue move it out of or to the top of the loop, while goto moves it to the label.
  • The syntax is: break; continue; and goto label; with label: written as its own line.
  • A realistic use: break when a search finds its target, continue to skip invalid entries, and goto for cleanup in a function with many error exits.
  • A surprising detail: break only leaves one level; inside nested loops it exits only the innermost one.
  • The example program searches for a number in a small array with break, skips odd numbers with continue, and uses goto for a simple error exit.
Keep in mind
  • Use break to stop a loop as soon as the answer is found.
  • Use continue to skip the rest of one pass without leaving the loop.
  • Avoid goto in normal code; reserve it for rare cleanup paths.
  • Remember break exits only the innermost loop or switch.
  • You are mastering program flow. Nice work!
main.c
#include <stdio.h>

int main(void) {
    // A small list of numbers to search through
    int numbers[5] = {4, 8, 15, 16, 23};
    int target = 15;
    int foundIndex = -1;

    // Search until we find the target
    for (int i = 0; i < 5; i++) {
        if (numbers[i] == target) {
            // Stop the loop as soon as we find it
            foundIndex = i;
            break;
        }
    }
    // Print where the target was found (or -1 if not found)
    printf("Found at index: %d\n", foundIndex);

    // Print only the even numbers, skipping odd ones
    for (int i = 0; i < 5; i++) {
        if (numbers[i] % 2 != 0) {
            // Skip the rest of this pass for odd numbers
            continue;
        }
        // This line runs only for even numbers
        printf("Even: %d\n", numbers[i]);
    }

    // A goto used for a simple error exit
    int divisor = 0;
    if (divisor == 0) {
        // Jump to the cleanup label instead of nesting more ifs
        goto cleanup;
    }
    printf("Division result: %d\n", 10 / divisor);

cleanup:
    // Runs whether we took the error path or not
    printf("Done\n");

    return 0;
}

What happens: Found at index: 2 Even: 4 Even: 8 Even: 16 Done

Breaking it down

break in search

break;

Stops the for loop the moment the target is found. Without it, the loop keeps running even after the answer is known.

continue for odd numbers

continue;

Skips the printf for odd numbers and goes to the next iteration. The loop itself keeps running.

goto label

goto cleanup;

Jumps directly to the cleanup label. Used here to avoid deeply nested error handling.

cleanup label

cleanup:

A named spot in the function. Execution continues here after the goto, so the final message always prints.

Jump statements at a glance

StatementEffectUsed inCaution
breakExit loop or switchfor, while, do, switchOnly exits innermost
continueSkip to next iterationfor, while, doSkips rest of body
gotoJump to a labelAnywhere in functionCan hurt readability

A helpful analogy

break is like leaving a party as soon as you find your friend. continue is like skipping a song you do not like and moving to the next one. goto is like a teleport button that jumps you anywhere in the room, powerful but easy to misuse.

Nesting rules

  • 1.break only leaves the innermost loop or switch; to exit outer loops you need a flag or goto.
  • 2.continue skips the rest of the current iteration but still runs the update step of a for loop.
  • 3.A goto label must be in the same function and is written with a colon at the end.
  • 4.Avoid jumping into the middle of a block; it can skip variable initialization and cause bugs.

Common mistakes

Expecting break to exit nested loops

Avoid

for (...) { for (...) { break; // only inner } }

Use instead

int done = 0; for (...) { for (...) { if (found) { done = 1; break; } } if (done) break; }

break leaves only the innermost loop, so the outer loop keeps running. Use a flag or goto to exit both.

Using continue before updating a while counter

Avoid

while (i < 5) { if (i == 3) continue; i++; }

Use instead

while (i < 5) { i++; if (i == 3) continue; }

continue skips the i++ line, so i stays at 3 forever and the loop never ends.

Using goto to jump into a block

Avoid

goto inside; int x = 5; inside: printf("%d", x);

Use instead

int x = 5; printf("%d", x);

Jumping over the initialization of x leaves it uninitialized. Never jump into the middle of a block.

Quick tips

  • •Prefer break over a flag variable when you only need to stop one loop.
  • •Use continue to simplify loops that skip a subset of items.
  • •Reserve goto for cleanup in functions with many early exits.
  • •Try removing break from the search example and see that it keeps looping after finding the target.
  • •Read compiler warnings; some flag suspicious goto usage.
Previous
Next