Loop control Ready
main.c

                

Ready

Statements run 0 Lines printed 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

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
Statements that jump
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

  1. break stops the loop immediately; the condition is not checked again.
  2. Typical use: stop searching once the answer is found.
  3. With for (;;) or while (1), break is the way out.
  4. In a switch inside a loop, break leaves only the switch.
  5. break outside a loop or switch is a compile error.

continue

  1. continue skips the rest of the current round.
  2. In a for loop it jumps to the step part (i++), then to the condition.
  3. In a while loop it jumps straight to the condition — make sure the update has already happened!
  4. Typical use: ignore some values (skip even numbers, skip invalid input).
  5. Often an if-else around the rest of the body does the same.

Nested loops

  1. The inner loop runs COMPLETELY for each round of the outer loop.
  2. Total rounds = outer rounds × inner rounds (3 rows × 4 columns = 12).
  3. Use different variables for the two loops (r and c, or i and j).
  4. Patterns: the outer loop chooses the row, the inner loop prints its columns.
  5. The inner condition may depend on the outer variable (c <= r gives a triangle).
Walkthrough

Stop early with break, skip with continue

Every line, explained.

1 #include <stdio.h>
2
3 int main(void)
4 {
5 int n = 91; Is 91 a prime number?
6 int isPrime = 1; Assume yes until a divisor is found.
7 for (int d = 2; d * d <= n; d++) { Try divisors 2, 3, 4 … up to √91.
8 if (n % d == 0) { 91 % 7 == 0: 7 divides 91.
9 isPrime = 0;
10 printf("%d = %d x %d\n", n, d, n / d); Prints 91 = 7 x 13
11 break; One divisor is enough — stop searching.
12 }
13 }
14 printf("%d is %s\n", n, isPrime ? "prime" : "not prime"); Prints 91 is not prime
15 for (int i = 1; i <= 10; i++) {
16 if (i % 2 == 0) continue; Skip even numbers.
17 printf("%d ", i); Prints 1 3 5 7 9
18 }
19 printf("\n");
20 return 0;
21 }
Common mistakes

Spot the bug

Mistakes almost every beginner makes — and how to fix them.

continue before the update in a while loop

✗ Wrong
int i = 0;
while (i < 10) {
    if (i % 2) continue;
    i++;
}
✓ Right
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

✗ Wrong
for (i …)
    for (j …)
        if (found) break;   // only the inner loop!
✓ Right
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

✗ Wrong
while (1) {
    switch (c) {
        case 'q': break;   // leaves the switch only
    }
}
✓ Right
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

✗ Wrong
for (i = 0; i < 3; i++)
    for (i = 0; i < 4; i++)
        printf("*");
✓ Right
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

✗ Wrong
if (x < 0)
    break;
✓ Right
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".

Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 5 correct
Question 1

for (i = 1; i <= 5; i++) { if (i == 3) break; printf("%d", i); } — output?

Question 2

for (i = 1; i <= 5; i++) { if (i == 3) continue; printf("%d", i); } — output?

Question 3

How many stars? for (r = 0; r < 3; r++) for (c = 0; c < 4; c++) printf("*");

Question 4

A break inside the inner of two nested loops leaves…

Question 5

for (r = 1; r <= 3; r++) { for (c = 1; c <= r; c++) printf("*"); printf("\n"); } — how many stars in the last line?

Assessment

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.