C Programs Tutorials | IT Developer
IT Developer

C Programming - C break statement



Share with a Friend

C Programming - C break statement

C break Statement

The break statement in C is used to exit a loop or switch statement prematurely, bypassing the remaining iterations or cases. It can be used inside any loop (such as for, while, or do-while loops) or inside a switch statement to stop execution.

Syntax of break Statement:

C

break;

  • When the break statement is executed, control is transferred out of the innermost loop or switch statement in which it is present.

Use Cases of break Statement:

  1. Exiting a loop: It is often used to exit a loop early when a certain condition is met.
  2. Exiting a switch statement: The break statement is used to exit a switch case, preventing the execution of subsequent cases.

Example 1: Using break in a for Loop

C

#include <stdio.h>

int main() {

    // Use break to exit the loop when i equals 5

    for (int i = 1; i <= 10; i++) {

        if (i == 5) {

            break;  // Exit the loop when i is 5

        }

        printf("%d ", i);  // Print values of i

    }

    return 0;

}

Explanation:

  • The for loop runs from 1 to 10, but when i reaches 5, the break statement is executed.
  • The loop exits early, and the program only prints 1 2 3 4.

Output:

1 2 3 4

Example 2: Using break in a while Loop

C

#include <stdio.h>

int main() {

    int i = 1;

    // Use break to exit the loop when i equals 7

    while (i <= 10) {

        if (i == 7) {

            break;  // Exit the loop when i is 7

        }

        printf("%d ", i);  // Print values of i

        i++;

    }

    return 0;

}

Explanation:

  • The while loop runs until i is greater than 10, but when i reaches 7, the break statement is executed, terminating the loop early.

Output:

1 2 3 4 5 6

Example 3: Using break in a switch Statement

C

#include <stdio.h>

int main() {

    int num = 3;

    switch (num) {

        case 1:

            printf("One\n");

            break;  // Break prevents the next cases from executing

        case 2:

            printf("Two\n");

            break;

        case 3:

            printf("Three\n");

            break;

        default:

            printf("Default case\n");

    }

    return 0;

}

Explanation:

  • The switch statement checks the value of num. When it matches case 3, the corresponding code is executed.
  • The break statement ensures that the program does not execute the default case or any cases after the matching one.

Output:

Three

Example 4: Using break in Nested Loops

C

#include <stdio.h>

int main() {

    // Use break to exit the inner loop when i equals 3

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

        for (int j = 1; j <= 5; j++) {

            if (j == 3) {

                break;  // Exit the inner loop when j equals 3

            }

            printf("%d-%d ", i, j);  // Print i and j

        }

        printf("\n");

    }

    return 0;

}

Explanation:

  • The inner loop runs for each value of i, but when j equals 3, the break statement is executed, causing the inner loop to exit early for that particular iteration of i.

Output:

1-1 1-2

2-1 2-2

3-1 3-2

4-1 4-2

5-1 5-2

Summary of break Statement:

  • The break statement is used to exit a loop or switch statement before its condition is fully met or its body is completely executed.
  • In loops: It helps in terminating a loop early based on some condition.
  • In switch statements: It prevents "fall-through" by exiting the switch after executing the matched case.
  • It only breaks out of the innermost loop or switch: In case of nested loops or switch statements, it exits the innermost structure where it is used.