C Break / Continue


In C programming, loops are fundamental for repeating code, but sometimes you need more control over the loop’s execution. The break and continue statements provide this control, allowing a program to exit a loop early or skip certain iterations. Mastering these statements ensures loops are efficient, readable, and adaptable.

While loops, for loops, and do-while loops can all use break and continue to control flow more precisely, their use must be careful to avoid unexpected behavior or infinite loops.

The Break Statement

The break statement immediately terminates the loop in which it appears. Control passes to the first statement after the loop. It is commonly used in situations where continuing the loop is no longer necessary, such as when a condition is met, or an error occurs.

Syntax:

break;

Example 1: Exiting a For Loop Early

#include <stdio.h>

int main() {
    int i;

    for(i = 1; i <= 10; i++) {
        if(i == 6) {
            break; // exit the loop
        }
        printf("%d\n", i);
    }

    return 0;
}

Output:

1
2
3
4
5
  • The loop stops when i == 6, so the numbers 6 to 10 are not printed.

  • break can be used in any loop or switch statement.

Example 2: Searching in an Array

#include <stdio.h>

int main() {
    int arr[5] = {3, 7, 9, 2, 5};
    int i, target = 9;

    for(i = 0; i < 5; i++) {
        if(arr[i] == target) {
            printf("Found %d at index %d\n", target, i);
            break; // stop searching once found
        }
    }

    return 0;
}

Output:

Found 9 at index 2

Here, break prevents unnecessary iterations after the target is found, improving efficiency.

The Continue Statement

The continue statement skips the remaining code in the current iteration and moves to the next iteration of the loop. It is useful when certain conditions mean the rest of the loop should not be executed.

Syntax:

continue;

Example 1: Skipping a Number

#include <stdio.h>

int main() {
    int i;

    for(i = 1; i <= 10; i++) {
        if(i % 2 == 0) {
            continue; // skip even numbers
        }
        printf("%d\n", i);
    }

    return 0;
}

Output:

1
3
5
7
9
  • Even numbers are skipped because the continue statement moves control to the next iteration immediately.

Example 2: Skipping Invalid Input

#include <stdio.h>

int main() {
    int num;
    for(int i = 0; i < 5; i++) {
        printf("Enter a positive number: ");
        scanf("%d", &num);
        if(num <= 0) {
            printf("Invalid, try again.\n");
            continue; // skip the rest of this iteration
        }
        printf("You entered: %d\n", num);
    }
    return 0;
}
  • continue helps ignore invalid input without exiting the loop entirely.

Difference Between Break and Continue

Feature Break Continue
Effect Exits the loop completely Skips current iteration only
Usage To terminate early To skip unwanted iterations
Common Use Cases Found target, exit loop Ignore invalid input, skip conditions
Works In All loops, switch All loops

Nested Loops with Break and Continue

When using nested loops, break or continue affects only the loop in which it appears.

#include <stdio.h>

int main() {
    for(int i = 1; i <= 3; i++) {
        for(int j = 1; j <= 3; j++) {
            if(j == 2) {
                continue; // skip j=2 only
            }
            printf("i=%d, j=%d\n", i, j);
        }
    }
    return 0;
}

Output:

i=1, j=1
i=1, j=3
i=2, j=1
i=2, j=3
i=3, j=1
i=3, j=3
  • Only j=2 is skipped; outer loop continues normally.

Break in nested loops:

#include <stdio.h>

int main() {
    for(int i = 1; i <= 3; i++) {
        for(int j = 1; j <= 3; j++) {
            if(j == 2) {
                break; // exits inner loop only
            }
            printf("i=%d, j=%d\n", i, j);
        }
    }
    return 0;

Output:

i=1, j=1
i=2, j=1
i=3, j=1
  • break exits only the inner loop each time, not the outer loop.

Best Practices

  1. Use break to exit loops early when further iterations are unnecessary.

  2. Use continue to skip unwanted iterations instead of nesting multiple if statements.

  3. Avoid excessive use: Overusing break and continue can make code harder to read.

  4. Comment their use in complex loops, especially nested loops, to clarify logic.

  5. Ensure loop variables update correctly to prevent infinite loops when using continue.

Practical Example: User Menu

#include <stdio.h>

int main() {
    int choice;

    while(1) {
        printf("\nMenu:\n1. Add\n2. Subtract\n3. Exit\nEnter choice: ");
        scanf("%d", &choice);

        if(choice == 3) {
            printf("Exiting program.\n");
            break; // exit loop
        }
        if(choice < 1 || choice > 3) {
            printf("Invalid choice, try again.\n");
            continue; // skip rest and ask again
        }
        printf("You selected option %d\n", choice);
    }

    return 0;
}
  • break ends the loop when the user chooses to exit.

  • continue ignores invalid input without terminating the program.

Summary of the Tutorial

The break and continue statements in C provide fine-grained control over loops. Break is used to terminate loops early, while continue is used to skip iterations without ending the loop. When used wisely, these statements make your loops more efficient, readable, and flexible, especially in menus, input validation, and nested loops. Understanding their behavior is essential for mastering loop control and program flow in C.


Practice Questions

  1. Write a program that prints numbers from 1 to 10 but stops printing when it reaches 7 using break.

  2. Create a program to print numbers from 1 to 20, skipping multiples of 3 using continue.

  3. Develop a menu-driven program that exits the loop when the user chooses option 4 using break.

  4. Write a program that takes 5 numbers from the user but ignores negative numbers using continue.

  5. Create a program to find the first number divisible by 5 in an array and stop searching using break.

  6. Write a program to print numbers from 1 to 15 but skip all even numbers using continue.

  7. Develop a program that repeatedly asks the user for input until they enter 0 to exit using break.

  8. Write a program that prints a multiplication table from 1 to 10 but skips multiples of 4 using continue.

  9. Create a program that reads 10 numbers from the user and stops taking input if the number 99 is entered using break.

  10. Write a program to print all numbers from 1 to 20 but skip numbers divisible by 2 or 5 using continue.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top