Break Statement in C

Break Statement in C. Break Statement in C Tutorial. Learn Break Statement in C
Break Statement in C.

What is break statement in C?

The break statement in C is a control statement used to terminate the execution of a loop or switch case block immediately. It allows the program to jump out of the loop or switch case block and continue the execution of the program from the next statement after the loop or switch case block.

Examples of break statement:

1. break statement in switch case

#include <stdio.h>

int main() {
    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");
    }
    
    return 0;
}

Output:

You chose option 2

Explanation:

The above program initializes the choice variable to 2 and enters the switch case block. Since choice is equal to 2, the second case is executed, which prints “You chose option 2” to the console. The break statement immediately exits the switch case block, and the program continues to execute the statement after the block.

2. break statement in for loop

#include <stdio.h>

int main() {
    int i;
    
    for (i = 0; i < 5; i++) {
        if (i == 3) {
            break;
        }
        printf("%d ", i);
    }
    
    printf("\n");
    return 0;
}

Output:

0 1 2

Explanation:

In above program, the for loop runs for values of i from 0 to 4. When i equals 3, the break statement is executed, and the loop immediately terminates. The program then continues to execute the printf statement after the loop.

3. break statement in while loop

#include <stdio.h>

int main() {
    int i = 0;
    
    while (i < 5) {
        if (i == 3) {
            break;
        }
        printf("%d ", i);
        i++;
    }
    
    printf("\n");
    return 0;
}

Output:

0 1 2

Explanation:

In above program, the while loop runs while i is less than 5. When i equals 3, the break statement is executed, and the loop immediately terminates. The program then continues to execute the printf statement after the loop.

4. break statement in do while loop

#include <stdio.h>

int main() {
    int i = 0;
    
    do {
        if (i == 3) {
            break;
        }
        printf("%d ", i);
        i++;
    } while (i < 5);
    
    printf("\n");
    return 0;
}

Output:

0 1 2

Explanation:

The do-while loop runs at least once, and then continues to run while i is less than 5. When i equals 3, the break statement is executed, andthe loop immediately terminates. The program then continues to execute the printf statement after the loop.

Rules to follow when using break statement:

  1. The break statement can only be used inside loops (for, while, do-while) and switch statements.
  2. The break statement is used to immediately terminate the current loop or switch statement and transfer control to the statement immediately following the loop or switch.
  3. If the break statement is used inside nested loops, it only terminates the innermost loop in which it is used.
  4. If the break statement is used inside a switch statement, it immediately exits the switch and transfers control to the statement following the switch.
  5. The break statement should be used carefully to avoid creating an infinite loop or an unreachable code.
  6. The break statement should not be used unnecessarily, as it can make the code difficult to read and understand.
  7. If the break statement is used in a loop, it should be accompanied by a condition that determines when the loop should terminate.

More: