Break Statement C Programming

The break statement in C programming language is used to terminate loop and takes control out of the loop. We sometimes want to terminate the loop immediately without checking the condition expression of loop. The break statement is used with conditional if statement.

Use of break statement, change the normal sequence of execution of statements inside loop.

Syntax of break Statement

break;
Control Flow of break Statement

C-Break-Statement

Use of break Statement

  • We can use break statement inside any loop(for, while and do-while). It terminates the loop immediately and program control resumes at the next statement following the loop.
  • We can use break statement to terminate a case in the switch statement.

If we are using break statement inside nested loop, then it will only terminate the inner loop enclosing the break statement.

C Program to show the use of break statement

Break Statement C Programming

#include <stdio.h>
#include <conio.h>
 
int main(){
    /* 
     * Using break statement to terminate for loop
     */
    int N, counter, sum=0;
    printf("Enter a positive number\n");
    scanf("%d", &N);
     
    for(counter=1; counter <= N; counter++){
        sum+= counter;
        /* If sum is greater than 50 break
           statement will terminate the loop */
        if(sum > 50){
            printf("Sum is > 50, terminating loop\n");
            break;
        }
    }
    if(counter > N)
        printf("Sum of Integers from 1 to %d = %d", N, sum);
     
    getch();
    return(0);
}

Above program find sum of all integers from 1 to N, using for loop. It sum is greater than 50, break statement will terminate for loop.

Output

Enter a positive number
9
Sum of Integers from 1 to 9 = 45
Enter a positive number
10
Sum is > 50, terminating loop