Learn C
#include "controlflow/break_continue.h"

break & continue in C

#concept

break exits its nearest loop or switch immediately; continue skips the rest of the current iteration and starts the next. Together they express early exits and skips far more readably than flag variables — search loops break on found, filters continue on bad items.

Both affect only the innermost enclosing loop. Escaping outer loops needs a flag, a function return, or goto — which is exactly why goto survives in C for nested-loop exits and shared cleanup.

#how it works
1

break exits now

Control jumps past the loop's closing brace the moment break executes — remaining iterations never happen.

2

continue skips ahead

continue abandons only the current pass: for loops still run their step expression, while loops jump to the test.

3

Only the innermost loop

In nested loops, both affect the closest loop — outer exits need flags, returns, or goto.

4

Prefer over flags

Direct break on found beats found-flag plumbing — fewer variables, clearer exits, less state to reason about.

#example
#include <stdio.h>
 
int main(void) {
int v[] = {3, -1, 4, -2, 5};
int i;
for (i = 0; i < 5; ++i) {
if (v[i] < 0) continue;
if (v[i] == 4) break;
printf("%d ", v[i]);
}
printf("\n");
return 0;
}
Output
3
13 is non-negative and not 4, so it prints with a trailing space.
2-1 triggers continue, skipping its print and moving on.
34 triggers break, ending the loop — -2 and 5 are never examined, then a bare newline finishes the line.
#use cases
Linear search breaking the moment the target is found
Skipping invalid records in processing loops with continue
Replacing found-flag variables with direct exits
Early loop termination on errors and sentinel values

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.