Why we use break statement in C language?

Break statement in C is used to break a loop or a statement of switch case after checking a condition. Break is a keyword in C which can break a loop depending on the condition. It can break a loop step by step from its inner loop to outer loop. Previously we have already seen the use of break statement with switch case in C and here we will again see the use of this statement with switch and loops.

Fields of using C break statement

We can use break statement to control the flow of our program. Generally we use break statement with the following C control statement.

  • With Switch Statement
  • With Loops

The syntax of break statement is as follows;

// Condition when needs to break
break;

Program using break statement with switch case

We can use break statement with switch case to control the flow of our program and when we need to break the program. See the following program where we have used this break keyword with switch case.

// break statement in C
#include <stdio.h>
int main(){
    int d;
    printf("Enter a digit to spell it : ");
    scanf("%d", &d);

    switch(d){
    case 0:
        printf("You have entered ZERO\n");
        break;

    case 1:
        printf("You have entered ONE\n");
        break;

    case 2:
        printf("You have entered TWO\n");
        break;

    case 3:
        printf("You have entered THREE\n");
        break;

    case 4:
        printf("You have entered FOUR\n");
        break;

    case 5:
        printf("You have entered FIVE\n");
        break;

    case 6:
        printf("You have entered SIX\n");
        break;
    case 7:
        printf("You have entered SEVEN\n");
        break;

    case 8:
        printf("You have entered EIGHT\n");
        break;
    case 9:
        printf("You have entered NINE\n");
        break;

    default:
        printf("%d is not a digit.\n", d);
    }
return 0;
}

Output of the program:

use of c break statement

Try this program without using break statement. Then you will see that the program will print all the output bellow the expected case like as follows.

Use of break statement with for loop in C

Here we will use break statement to break the loop for a specific condition. The compiler will execute the code outside the loop by breaking the loop when it finds the statement inside a loop and the condition will be true. Here is a demo program to use break keyword with for loop.

// break statement with for loop in C
#include <stdio.h>
int main(){
    int j, num;
    printf("Enter a number here : ");
    scanf("%d", &num);

    for(j = 1; j <= num; j++){
        if(j == 5){
            break;
        }else{
            printf("%d\n", j);
        }
    }
    printf("\nOutside the loop.\n");
return 0;
}

This program can not print any value greater than 4 because when the value of variable j will be 5, the loop will break. Then the code bellow the loop will be executed. for this the output of this program will be as following if you give the input any number greater than 4.

c break statement, break statement in c

Using break statement with while loop in C

As like as for loop we can use break statement with while loop too. Here are an example of using this statement with while loop in C.

// break statement with while loop in C
#include <stdio.h>
int main(){
    int k = 1, num;
    printf("Enter a number : ");
    scanf("%d", &num);

    while(k <= num){
        if(k == 5){
            break;
        }else{
            printf("%d\n", k);
        }
        k++;
    }
    printf("\nOutside of the loop.\n");
return 0;
}

Output of program

Enter a number : 10
1
2
3
4

Outside of the loop.

Using break statement with do while loop

Using break statement with do while loop in C is almost the same process as other loops. See the use of break keyword with do while loop bellow.

// break statement with do while loop in C
#include <stdio.h>
int main(){
    int i = 1, num;
    printf("Enter a number : ");
    scanf("%d", &num);

    do{
        if(i == 5){
            break;
        }else{
            printf("%d\n", i);
        }
      i++;
    }while(i <= num);

    printf("\nOutside of the loop.\n");
return 0;
}

Output of this program:

Enter a number : 15
1
2
3
4

Outside of the loop.

Process returned 0 (0x0)   execution time : 1.553 s
Press any key to continue.