break, continue & Nested Loops
Loop controlSometimes a loop has to stop early or skip a round: break and continue do exactly that. And a loop can run inside another loop — that is how tables and patterns are drawn. In the lab below you see which rounds are skipped, watch nested loops draw a pattern cell by cell, and find out how many loops a break really leaves.
- breakleaves the loop now
- continueskips to the next round
- Nestedthe inner loop runs fully for each outer round
Ready
Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode
The ideas behind the lab
Exactly what you watched happen, in plain steps.
Jumping inside loops
break ends the loop immediately; the program continues after it. continue ends only the CURRENT round and jumps to the next one (in a for loop: to the step part). Both act on the innermost loop they are in. A nested loop is a loop inside a loop: for every round of the outer loop, the inner loop runs completely.
for (i = 1; i <= 10; i++) {
if (i % 3 == 0) continue; // skip this round → i++
if (i > 7) break; // leave the loop
printf("%d ", i); // prints 1 2 4 5 7
}
for (r = 1; r <= 3; r++) // outer: 3 rounds
for (c = 1; c <= 4; c++) // inner: 4 rounds EACH time
… // runs 3 × 4 = 12 times
| Statement | What it does | In nested loops |
|---|---|---|
| break | leaves the loop (or switch) at once | leaves only the innermost loop |
| continue | skips the rest of this round, goes on with the next | affects only the innermost loop |
| goto label | jumps to label: anywhere in the function | can leave several loops at once — avoid it otherwise |
| return | leaves the whole function (Functions part) | leaves every loop |
break
- break stops the loop immediately; the condition is not checked again.
- Typical use: stop searching once the answer is found.
- With for (;;) or while (1), break is the way out.
- In a switch inside a loop, break leaves only the switch.
- break outside a loop or switch is a compile error.
continue
- continue skips the rest of the current round.
- In a for loop it jumps to the step part (i++), then to the condition.
- In a while loop it jumps straight to the condition — make sure the update has already happened!
- Typical use: ignore some values (skip even numbers, skip invalid input).
- Often an if-else around the rest of the body does the same.
Nested loops
- The inner loop runs COMPLETELY for each round of the outer loop.
- Total rounds = outer rounds × inner rounds (3 rows × 4 columns = 12).
- Use different variables for the two loops (r and c, or i and j).
- Patterns: the outer loop chooses the row, the inner loop prints its columns.
- The inner condition may depend on the outer variable (c <= r gives a triangle).
Stop early with break, skip with continue
Every line, explained.
#include <stdio.h>
int main(void)
{
int n = 91;
Is 91 a prime number?
int isPrime = 1;
Assume yes until a divisor is found.
for (int d = 2; d * d <= n; d++) {
Try divisors 2, 3, 4 … up to √91.
if (n % d == 0) {
91 % 7 == 0: 7 divides 91.
isPrime = 0;
printf("%d = %d x %d\n", n, d, n / d);
Prints 91 = 7 x 13
break;
One divisor is enough — stop searching.
}
}
printf("%d is %s\n", n, isPrime ? "prime" : "not prime");
Prints 91 is not prime
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) continue;
Skip even numbers.
printf("%d ", i);
Prints 1 3 5 7 9
}
printf("\n");
return 0;
}
Spot the bug
Mistakes almost every beginner makes — and how to fix them.
continue before the update in a while loop
int i = 0;
while (i < 10) {
if (i % 2) continue;
i++;
}
int i = 0;
while (i < 10) {
i++;
if (i % 2) continue;
}
When i is 1, continue jumps back to the condition without i++: i stays 1 for ever.
Expecting break to leave both loops
for (i …)
for (j …)
if (found) break; // only the inner loop!
for (i …) {
for (j …)
if (found) break;
if (found) break; // leave the outer one too
}
break leaves only the innermost loop. Check a flag in the outer loop (or use a function and return).
break inside a switch inside a loop
while (1) {
switch (c) {
case 'q': break; // leaves the switch only
}
}
while (running) {
switch (c) {
case 'q': running = 0; break;
}
}
The break belongs to the switch, so the loop keeps going. Use a flag (or return) to stop the loop.
The same variable for both loops
for (i = 0; i < 3; i++)
for (i = 0; i < 4; i++)
printf("*");
for (i = 0; i < 3; i++)
for (j = 0; j < 4; j++)
printf("*");
The inner loop changes i, so the outer loop loses count — here it never ends.
break outside a loop
if (x < 0)
break;
if (x < 0)
return 0; // or restructure the code
break only works inside a loop or switch: "error: break statement not within loop or switch".
Check your understanding
Answer each question — you will see the explanation straight away.
for (i = 1; i <= 5; i++) { if (i == 3) break; printf("%d", i); } — output?
At i = 3 the loop ends before printing.
for (i = 1; i <= 5; i++) { if (i == 3) continue; printf("%d", i); } — output?
Only round 3 is skipped.
How many stars? for (r = 0; r < 3; r++) for (c = 0; c < 4; c++) printf("*");
The inner loop runs 4 times for each of the 3 outer rounds: 12.
A break inside the inner of two nested loops leaves…
break acts on the innermost loop it is in.
for (r = 1; r <= 3; r++) { for (c = 1; c <= r; c++) printf("*"); printf("\n"); } — how many stars in the last line?
The inner loop runs r times; the last row has r = 3.
break, continue & Nested Loops quiz
Test yourself: 10 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.