Skip to content

Module 3 · Decisions and Loops — topic 5 of 5

break, continue and goto

break leaves a loop or switch early, continue skips to the next iteration, and goto jumps to a label. Learn when each is the clearest choice and how they interact with nested loops.

Beginner 12 min read·Lesson 15 of 37 in C Programming

Learning objectives

  • Exit a loop early with break
  • Skip iterations with continue
  • Know the one accepted use of goto: centralised cleanup

Definition

break terminates the innermost enclosing loop or switch; continue ends the current iteration of the innermost enclosing loop and proceeds to its next condition check (and, in a for, its update); goto label; transfers control unconditionally to a labelled statement in the same function.

Introduction

Sometimes you know the answer before a loop finishes: you found the item you were searching for, or the input is invalid. break stops the loop immediately. Other times you want to skip just one item and carry on: continue does that.

goto is rarely needed and is avoided in most code, but you will see it in real C projects for one specific pattern — jumping to cleanup code when an error occurs.

Why it matters

Searching loops become shorter and faster with break, and filtering loops become clearer with continue than with deeply nested if blocks.

Understanding exactly what continue does in a for versus a while loop prevents a subtle infinite-loop bug.

Detailed explanation

break affects only the innermost loop or switch. Inside a switch that is inside a loop, break leaves the switch, not the loop.

In a for loop, continue jumps to the update expression, then the condition. In a while loop it jumps straight to the condition — so if the update is at the bottom of the body, continue skips it and the loop may never end.

To exit nested loops, either set a flag that the outer loop checks, put the loops in a function and return, or (rarely) goto a label after the loops.

goto for cleanup: when a function acquires several resources (memory, files) and any step can fail, jumping forward to a single cleanup label avoids duplicating the release code. Never use goto to jump backward to build loops.

Syntax

C
for (...) {
if (found) break; /* leave the loop */
if (skip_this) continue; /* next iteration */
}
if (error) goto cleanup;
/* ... */
cleanup:
free(buffer);
  • A label is a name followed by a colon and must precede a statement.
  • goto cannot jump into another function.
  • continue is not valid inside a switch unless the switch is inside a loop.

Code examples

Linear search with break

C
#include <stdio.h>
int main(void) {
int roll[] = {14, 22, 9, 31, 7};
int target = 31, pos = -1;
for (int i = 0; i < 5; i++) {
if (roll[i] == target) {
pos = i;
break;
}
}
printf("%d found at index %d\n", target, pos);
return 0;
}
Output
31 found at index 3

Skipping with continue

C
#include <stdio.h>
int main(void) {
for (int i = 1; i <= 10; i++) {
if (i % 3 == 0) continue;
printf("%d ", i);
}
printf("\n");
return 0;
}
Output
1 2 4 5 7 8 10

More examples

Leaving nested loops with a flag

C
#include <stdio.h>
int main(void) {
int grid[3][4] = {
{5, 8, 1, 4},
{7, 0, 3, 2},
{6, 9, 0, 1},
};
int found = 0, r = -1, c = -1;
for (int i = 0; i < 3 && !found; i++) {
for (int j = 0; j < 4; j++) {
if (grid[i][j] == 0) {
found = 1; r = i; c = j;
break; /* leaves only the inner loop */
}
}
}
if (found) printf("first zero at row %d, column %d\n", r, c);
int i = 0, odd_sum = 0;
while (i < 6) {
i++; /* update BEFORE continue */
if (i % 2 == 0) continue;
odd_sum += i;
}
printf("sum of odd numbers 1..6 = %d\n", odd_sum);
return 0;
}
Output
first zero at row 1, column 1
sum of odd numbers 1..6 = 9

Step-by-step explanation

  • The inner break exits the column loop as soon as a zero is found.
  • The outer loop's condition !found then stops it too, so the search ends at the first zero.
  • In the while loop, i++ comes before continue, so even numbers are skipped without skipping the update.
  • 1 + 3 + 5 = 9.

Expected output

Running “Leaving nested loops with a flag” prints exactly:

TextOutput
first zero at row 1, column 1
sum of odd numbers 1..6 = 9

Practical applications

  • Search routines stop at the first match with break.
  • Log processors use continue to skip blank or comment lines.
  • The Linux kernel uses goto out; style cleanup in thousands of functions.

Best practices

  • Use break for early exit from searches; it is clearer than a complicated condition.
  • Put the loop update before any continue in while loops.
  • Use goto only for forward jumps to cleanup code.

Common mistakes

continue skipping the while-loop update

Avoid

int i = 0;
while (i < 10) {
if (i % 2) continue;
printf("%d\n", i);
i++;
}

Prefer

int i = 0;
while (i < 10) {
i++;
if (i % 2) continue;
printf("%d\n", i);
}

When i becomes 1, continue jumps back before i++ ever runs again — an infinite loop.

Expecting break to exit all loops

Avoid

for (...) for (...) if (done) break; /* outer keeps going */

Prefer

for (...; !done; ...) for (...) if (done) break;

break leaves only the innermost loop. Check a flag in the outer loop too.

Important notes

  • Dijkstra's 1968 letter “Go To Statement Considered Harmful” is why structured loops replaced goto for control flow.
  • Many style guides (MISRA C) restrict goto; the Linux kernel style guide explicitly allows it for error handling.

Key takeaways

  • break leaves the innermost loop or switch.
  • continue starts the next iteration; in while loops make sure the update still happens.
  • Use flags or functions to leave nested loops.

Try it yourself

  1. 1
    Read up to 10 numbers and stop early with break if the user enters 0; print the sum.
  2. 2
    Print all numbers from 1 to 50 that are not divisible by 2 or 5, using continue.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

5 questions test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
  • BeginnerWhat is printed?
  • BeginnerInside two nested for loops, what does `break` in the inner loop do?
Practise this topic