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.
Learning objectives
- Exit a loop early with break
- Skip iterations with continue
- Know the one accepted use of goto: centralised cleanup
Definition
breakterminates the innermost enclosing loop orswitch;continueends the current iteration of the innermost enclosing loop and proceeds to its next condition check (and, in afor, 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
- A label is a name followed by a colon and must precede a statement.
gotocannot jump into another function.continueis not valid inside aswitchunless the switch is inside a loop.
Code examples
Linear search with break
31 found at index 3
Skipping with continue
1 2 4 5 7 8 10
More examples
Leaving nested loops with a flag
first zero at row 1, column 1 sum of odd numbers 1..6 = 9
Step-by-step explanation
- The inner
breakexits the column loop as soon as a zero is found. - The outer loop's condition
!foundthen stops it too, so the search ends at the first zero. - In the
whileloop,i++comes beforecontinue, so even numbers are skipped without skipping the update. - 1 + 3 + 5 = 9.
Expected output
Running “Leaving nested loops with a flag” prints exactly:
Practical applications
- Search routines stop at the first match with
break. - Log processors use
continueto skip blank or comment lines. - The Linux kernel uses
goto out;style cleanup in thousands of functions.
Best practices
- Use
breakfor early exit from searches; it is clearer than a complicated condition. - Put the loop update before any
continueinwhileloops. - Use
gotoonly for forward jumps to cleanup code.
Common mistakes
continue skipping the while-loop update
Avoid
Prefer
When i becomes 1, continue jumps back before i++ ever runs again — an infinite loop.
Expecting break to exit all loops
Avoid
Prefer
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
gotofor control flow. - Many style guides (MISRA C) restrict
goto; the Linux kernel style guide explicitly allows it for error handling.
Key takeaways
breakleaves the innermost loop or switch.continuestarts the next iteration; inwhileloops make sure the update still happens.- Use flags or functions to leave nested loops.
Try it yourself
- 1Read up to 10 numbers and stop early with
breakif the user enters 0; print the sum. - 2Print 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?