Skip to main content

What Is Difference Between Continue And Break In C

by
Last updated on 7 min read

What is the difference between continue and break in C?

Use break to exit a loop immediately and continue to skip to the next iteration.

When you're debugging or writing C code, understanding how these two keywords control loop execution is crucial. They serve completely different purposes, and mixing them up can lead to logic errors, infinite loops, or even unexpected program behavior. Below, you'll find a practical guide to using each correctly in for, while, and do...while loops.

Quick Fix Summary:
Use break to exit a loop immediately. Use continue to skip to the next iteration. Both work in for, while, and do...while loops in C as of 2026.

What's happening when you use break and continue?

The break statement exits the loop entirely, while continue skips the rest of the current iteration.

The break statement immediately terminates the innermost enclosing loop or switch block. After it runs, control transfers to the statement right after the loop or block—no more iterations happen.

Meanwhile, continue skips whatever's left in the current loop iteration and jumps straight to the next one. It doesn't exit the loop entirely, just the current pass. (Think of it like hitting the "next track" button on a playlist when you don't like the current song.)

How do I use break in a loop?

Use break when you need to exit a loop completely before it finishes naturally.

Here's a simple example. Imagine you're searching through an array for a specific value:

int numbers[] = {3, 7, 12, 5, 9};
int target = 5;
int found = 0;

for (int i = 0; i < 5; i++) {
    if (numbers[i] == target) {
        found = 1;
        break; // Exit the loop as soon as we find the target
    }
    printf("Checking %d\n", numbers[i]);
}

if (found) {
    printf("Found the target!\n");
}

Without that break, the loop would keep running even after finding what it's looking for. That's usually not what you want.

How do I use continue in a loop?

Use continue when you want to skip specific iterations without exiting the loop entirely.

Let's say you're processing a list of numbers but want to ignore the even ones:

for (int i = 0; i < 10; i++) {
    if (i % 2 == 0) {
        continue; // Skip even numbers entirely
    }
    printf("%d ", i); // Only prints odd numbers: 1 3 5 7 9
}

In this case, continue prevents the rest of the loop body from running for those even values. The loop itself keeps going until it finishes all iterations.

Can I use break in a while loop?

Yes, break works the same way in while loops as it does in for loops.

Here's a quick example:

int count = 0;
while (count < 10) {
    printf("%d ", count);
    if (count == 5) {
        break; // Exits the loop when count reaches 5
    }
    count++;
}

This prints numbers from 0 to 5, then stops completely. The break works exactly the same way it does in a for loop.

Can I use continue in a while loop?

Yes, continue works in while loops, but be careful with loop counters.

Here's a working example:

int num = 0;
while (num < 5) {
    num++;
    if (num == 3) {
        continue; // Skips printing 3
    }
    printf("%d ", num); // Output: 1 2 4 5
}

Now, here's where people often get tripped up. If you forget to increment the counter before the continue, you'll create an infinite loop:

int x = 0;
while (x < 10) {
    if (x == 5) {
        continue; // Uh-oh! x never increments past 5
    }
    x++;
    printf("%d ", x);
}

Always make sure your loop variable changes with each iteration, even when using continue.

What happens if I use break outside a loop?

You'll get a compiler error in C23 (2026 standard).

Both break and continue must be inside a loop or switch block. If you try to use them elsewhere, the compiler will flag it as an error. This is a safety feature to prevent accidental misuse.

(Honestly, this is one of those cases where the compiler is doing you a favor.)

What happens if I use continue in a do...while loop?

Continue works the same way in do...while loops as in other loop types.

Here's an example:

int i = 0;
do {
    i++;
    if (i == 3) {
        continue; // Skips printing 3
    }
    printf("%d ", i); // Output: 1 2 4 5
} while (i < 5);

The behavior is identical to for and while loops. The continue skips the rest of the current iteration and moves to the condition check, then the next iteration.

How do I exit an outer loop from inside a nested loop?

Use a labeled break or restructure your logic—break alone only exits the innermost loop.

By default, break only exits the innermost loop. If you need to exit an outer loop from inside a nested one, you have a few options:

First, you could use a labeled break (though this is generally discouraged):

int outer = 0;
outerLoop: // Label for the outer loop
while (outer < 3) {
    int inner = 0;
    while (inner < 3) {
        printf("(%d,%d) ", outer, inner);
        if (outer == 1 && inner == 1) {
            break outerLoop; // Exits both loops
        }
        inner++;
    }
    outer++;
}

But honestly, labeled breaks make code harder to read. A cleaner approach is to restructure your logic:

int found = 0;
int outer = 0;
while (outer < 3 && !found) {
    int inner = 0;
    while (inner < 3) {
        printf("(%d,%d) ", outer, inner);
        if (outer == 1 && inner == 1) {
            found = 1;
            break; // Exits inner loop
        }
        inner++;
    }
    outer++;
}

This keeps your code cleaner and easier to maintain.

Why would I get an infinite loop with continue?

It happens when you use continue without advancing the loop variable in a while loop.

Here's the classic mistake:

int x = 0;
while (x < 10) {
    if (x == 5) {
        continue; // x never changes, loop runs forever
    }
    x++;
}

When x hits 5, the continue skips the x++ line. The condition x < 10 never becomes false, so the loop never ends.

This is why it's so important to ensure your loop variable changes with every iteration, even when using continue.

Can I use break in a switch statement?

Yes, break is commonly used in switch statements to prevent fall-through.

In a switch statement, execution "falls through" from one case to the next unless you use break:

int choice = 2;
switch (choice) {
    case 1:
        printf("You chose option 1\n");
        break;
    case 2:
        printf("You chose option 2\n");
        break;
    case 3:
        printf("You chose option 3\n");
        break;
    default:
        printf("Invalid choice\n");
}

Without those break statements, executing case 2 would also run case 3's code. That's usually not what you want.

What's the difference between break and return?

Break exits a loop, while return exits the entire function.

This is a common point of confusion. break only exits the current loop or switch block. The function keeps running after the loop ends.

Meanwhile, return exits the entire function immediately, returning control to wherever the function was called. It doesn't just exit a loop—it exits the whole function.

Here's a quick comparison:

void example() {
    for (int i = 0; i < 10; i++) {
        if (i == 5) {
            break; // Exits the loop, function continues
            // More code here would still run
        }
    }
    printf("Loop ended, function continues\n");

    for (int i = 0; i < 10; i++) {
        if (i == 5) {
            return; // Exits the entire function
            // This code never runs
        }
    }
    printf("This never prints\n");
}

When should I avoid using break or continue?

Avoid them when they make your code harder to read or maintain.

These keywords can sometimes obscure your code's intent. If using break or continue makes your logic harder to follow, consider restructuring your loop instead.

For example, this:

for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break;
    }
    printf("%d ", i);
}

Could be rewritten as:

int shouldRun = 1;
for (int i = 0; i < 10 && shouldRun; i++) {
    printf("%d ", i);
    if (i == 5) {
        shouldRun = 0;
    }
}

Which approach is clearer? That depends on your team's coding standards and the specific context. Sometimes a break is perfectly fine. Other times, restructuring leads to more maintainable code.

How do I debug issues with break and continue?

Add print statements to track loop execution and check your logic step by step.

Debugging these issues often comes down to understanding exactly when and why your loop is exiting or skipping iterations. Adding temporary print statements can help:

for (int i = 0; i < 5; i++) {
    printf("Before continue - i is %d\n", i);
    if (i == 2) {
        continue; // Skip this iteration
    }
    printf("After continue - i is %d\n", i);
}

This shows you exactly what's happening with each iteration. You can also use a debugger to step through your code line by line.

Common issues to check:

  • Are your loop variables being modified correctly?
  • Are your conditions evaluating as expected?
  • Are you accidentally nesting loops when you shouldn't?

Are there alternatives to break and continue?

Yes—restructuring your loops or using flags can often replace them.

Sometimes you can rewrite your loops to avoid these keywords entirely. For example, this:

int found = 0;
for (int i = 0; i < 10; i++) {
    if (array[i] == target) {
        found = 1;
        break;
    }
}

Could become:

int found = 0;
int i = 0;
while (i < 10 && !found) {
    if (array[i] == target) {
        found = 1;
    } else {
        i++;
    }
}

Both approaches achieve the same result. The second one might be clearer to some readers since it avoids the break entirely.

Similarly, continue can often be replaced by adjusting your loop conditions or using an else clause:

for (int i = 0; i < 10; i++) {
    if (i % 2 == 0) {
        // Skip even numbers
        continue;
    }
    printf("%d ", i);
}

Could become:

for (int i = 0; i < 10; i++) {
    if (i % 2 != 0) { // Only process odd numbers
        printf("%d ", i);
    }
}
David Okonkwo
Author

David Okonkwo holds a PhD in Computer Science and has been reviewing tech products and research tools for over 8 years. He's the person his entire department calls when their software breaks, and he's surprisingly okay with that.

How Does The Tombstone Perk Work?How Do You Left Align In Word?