break, continue, and Nested Loops
Leaving early, skipping an iteration, and the nested loops behind matrix traversal and the pattern-printing questions on every paper.
Steering Inside the Loop
The three loops decide whether to repeat; two small statements decide what happens mid-pass. break leaves the loop entirely, continue abandons the current pass and moves to the next, and nesting puts loops inside loops, the machinery behind every pattern-printing question ever set. This lesson finishes your control-flow toolkit.
break: Leave Now
You met break escaping a switch; inside a loop it ends the innermost enclosing loop immediately, condition unconsulted. Its natural partner is the deliberate infinite loop, where the exit lives mid-body because that is where the answer becomes known:
Feed it a few numbers ending with 42. Two exits, both explicit, both visible: the input running dry, or the answer arriving. When a loop's natural end is mid-pass, while (1) plus break states it more honestly than a contorted condition.
continue: Skip This One
continue abandons the rest of the current pass and jumps to the loop's next test, in a for, the update still runs first, a detail exams check. Its job is filtering: step over the uninteresting cases and keep the interesting path unindented.
Multiples of 3 are skipped; everything else prints. The same logic writes as if (i % 3 != 0) { printf... }, and for one line the difference is taste. continue earns its keep when the skip conditions are several and the real work is long: guard clauses at the top, flat logic below. In a while, take care that continue does not jump over the statement that advances the loop, the update lives in the body there, and skipping it loops forever.
Nested Loops
A loop inside a loop: the outer picks a row, the inner walks its columns, and together they sweep a grid. This is the shape of matrix work in the next chapter and of the pattern questions on every C paper:
Row 1 prints one star, row 4 prints four: the inner limit depends on the outer counter, and that single idea, col <= row, generates the triangle. Every pyramid, diamond, and number-pattern variant is this program with different inner limits and characters. The counters must be distinct variables; reusing i for both loops is a bug the compiler cannot see.
Total work multiplies: 4 rows of up-to-4 columns is at most 16 inner passes. A grid of n by n does n squared, which is why nested loops are where programs first get slow, an observation the algorithms sections of later chapters build on.
break and Nesting
break exits one level only: from an inner loop, the outer continues its next pass. Escaping both levels at once has no dedicated statement in C, and the accepted tools are a flag variable tested by the outer condition, restructuring, or, per chapter 5's carve-out, a forward goto, the second and last situation where serious C code accepts one. Exams phrase it as "how do you break out of nested loops?", and that three-part answer is full marks.
Key Takeaways
breakends the innermost loop immediately;while (1)plusbreaksuits loops whose exit condition appears mid-pass.continueskips to the next pass; in aforthe update still runs, in awhilebeware skipping the advance statement.- Nested loops sweep grids: outer rows, inner columns, with the inner limit free to depend on the outer counter (
col <= rowmakes triangles). - Inner and outer counters must be distinct variables; work multiplies as the product of the loop lengths.
breakescapes one level only; nested escape uses a flag, restructuring, or the goto carve-out.
How did you find this lesson?
Your rating helps us improve the content.
Create an account to track your progress and access interactive exercises. Already have one? Sign in.
break, continue, and Nested Loops - Quiz
Test your understanding of the lesson.
Practice Exercises
The Star Triangle
Read a height and print a right triangle of stars with nested loops: row 1 has one star, row 2 has two, up to the given height. The inner loop's limit must depend on the outer counter. Guard the scanf and reject heights below 1.
Lesson Discussion
Share your thoughts and questions
No comments yet. Be the first to share your thoughts!