Chapter 14 of 37

Break and Continue

When working with loops, sometimes we don't want the loop to run normally until its condition becomes false.

For example, we may want to stop the loop completely or skip one particular iteration.

C provides two statements for this:

  • break

  • continue

break Statement

The break statement immediately terminates the loop or switch statement.

For example:

for (int i = 1; i <= 5; i++)
{
    if (i == 3)
        break;

    printf("%d\n", i);
}

Output:

1
2

When i becomes 3, break stops the entire loop.

Think of it as:

Loop
 ↓
1 → 2 → 3
         ↓
       STOP

continue Statement

The continue statement skips the current iteration and moves to the next iteration of the loop.

Example:

for (int i = 1; i <= 5; i++)
{
    if (i == 3)
        continue;

    printf("%d\n", i);
}

Output:

1
2
4
5

When i becomes 3, the current iteration is skipped, but the loop continues.

Think of it as:

1 → 2 → 3 → 4 → 5
        ↓
      SKIP

Break vs Continue

break

continue

Stops the loop completely

Skips only the current iteration

Execution continues after the loop

Execution continues with the next iteration

Can be used in loops and switch

Used to control loop iterations

Easy Way to Remember

break = Stop the loop

continue = Skip this iteration

For example, if you're processing 100 students and want to stop processing completely when you find a particular student, use break.

If you simply want to skip one student and continue processing the rest, use continue.