Open In App

Break Statement in C

Last Updated : 28 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The break statement in C is a loop control statement that breaks out of the loop when encountered. It can be used inside loops or switch statements to bring the control out of the block. The break statement can only break out of a single loop at a time.

Example:

C
#include <stdio.h>

int main() {
    for (int i = 1; i <= 10; i++) {
      
      	// Exit the loop when i equals 5
        if (i == 5) {
            break;  
        }
        printf("%d ", i);
    }
    return 0;
}

Output
1 2 3 4 

Explanation: In this program, the break statement exits the loop when i == 5. As a result, the loop stops prematurely, and only the numbers 1 to 4 are printed.

Syntax of break in C

C
// in a block {
    break;
}

We just put the break wherever we want to terminate the execution of the loop.

How break in C Works?

BreakstatementinCforloop
Working of break stateemnt in C

The working of the break statement in C is described below:

  1. STEP 1: The loop execution starts after the test condition is evaluated.
  2. STEP 2: If the break condition is present the condition will be evaluated.
  3. STEP 3A: If the condition is true, the program control reaches the break statement and skips the further execution of the loop by jumping to the statements directly below the loop.
  4. STEP 3B: If the condition is false, the normal flow of the program control continues.

Flowchart of break Statement

flowchart-of-break-in-c
Flowchart for Break Statement in C

Examples of break in C

The following examples illustrate the use of break in C programming:

break with Nested Loops

C
#include <stdio.h>

int main() {
  
    // Nested for loops with break statement
    // at inner loop
    for (int i = 1; i <= 6; ++i) {
        for (int j = 1; j <= i; ++j) {
            if (i <= 4) {
                printf("%d ", j);
            }
            else {
              
                // If i > 4 then this innermost loop will
                // break
                break;
            }
        }
        printf("\n");
    }
    return 0;
}

Output
1 
1 2 
1 2 3 
1 2 3 4 

Explanation: In the above program the inner loop breaks when the value of i becomes equal to 4, which stops the printing of the value , although the outer loop continues to run but as the inner loop encounters a break statement for every iteration of i after the value of i becomes equal to 4, no values are printed after the fourth line.

break with Simple Loops

C
#include <stdio.h>
int main(){

    // Using break inside for loop to terminate
  	// after 2 iterations
    printf("break in for loop\n");
    for (int i = 1; i < 5; i++) {
        if (i == 3) {
            break;
        }
        else {
            printf("%d ", i);
        }
    }

    // using break inside while loop to terminate
  	// after 2 iterations
    printf("\nbreak in while loop\n");
    int i = 1;
    while (i < 20) {
        if (i == 3)
            break;
        else
            printf("%d ", i);
        i++;
    }
    return 0;
}

Output
break in for loop
1 2 
break in while loop
1 2 

Explanation: In this code, the break statement is used in both a for and while loop to terminate after two iterations. In both loops, numbers 1 and 2 are printed, and when i reaches 3, the break statement exits the loop, stopping further iterations. This demonstrates how break can be used to stop execution based on a specific condition.

Break in C switch case

In general, the Switch case statement evaluates an expression, and depending on the value of the expression, it executes the statement associated with the value. Not only that, all the cases after the matching case after the matching case will also be executed. To prevent that, we can use the break statement in the switch case as shown:

C
#include <stdio.h>
#include <stdlib.h>

int main() {
    char c;
    float x, y;

    while (1) {
        printf("Enter an operator (+, -), if want to exit "
               "press x: ");
        scanf(" %c", &c);
        // to exit
        if (c == 'x')
            exit(0);

        printf("Enter Two Values:\n ");
        scanf("%f %f", &x, &y);

        switch (c) {
            
        // For Addition
        case '+':
            printf("%.1f + %.1f = %.1f\n", x, y, x + y);
            break;
            
        // For Subtraction
        case '-':
            printf("%.1f - %.1f = %.1f\n", x, y, x - y);
            break;
        default:
            printf(
                "Error! please write a valid operator\n");
        }
    }
}


Output:

Enter an operator (+, -), if want to exit press x: +
Enter Two Values:
10
20
10.0 + 20.0 = 30.0

Explanation: This C program uses a while loop to repeatedly prompt the user for an operator and two numbers, performing addition or subtraction based on the operator entered. The break statement is used in the switch case to exit after executing the relevant operation, and the program exits when the user enters 'x'. If an invalid operator is provided, an error message is displayed.

break vs continue

The difference between the break and continue in C is listed in the below table:

breakcontinue
The break statement terminates the loop and brings the program control out of the loop.The continue statement terminates only the current iteration and continues with the next iterations.

The syntax is: break;

The syntax is: continue;

The break can also be used in switch case.Continue can only be used in loops.

Next Article
Article Tags :

Similar Reads