Break Statement in C

The Break statement in C Programming is very useful to exit from any loop such as For Loop, While Loop, and Do While. While executing these loops, if the C compiler finds the break statement inside them, then the loop will stop running the statements and immediately exit from the loop.

The C Break statement and Continue Statement statements are two important keywords used to alter the flow of a program in any programming language.

Loops are useful to execute a particular block of statements for n number of times until the test condition is false. There will be some situations where we have to terminate the loop without executing all the statements. In these situations, we can use Break statement and Continue statements in C programming.

Break Statement in C

For example, we have five statements inside the loop, and we want to exit from the loop when a certain condition is True; otherwise, it has to execute them. In these situations, we can place the Break statement inside the If condition. If the condition is True, the compiler will execute the break statement in C. It means the break statement will exit the controller from the loop completely. Otherwise, it will run all the statements.

Break statement Syntax

The basic syntax behind this Break statement is as shown below:

break;

Break Statement in C Examples

Break statement is the most crucial statement in Switch Case. Without using the Break, the compiler won’t exit from the switch cases. We would like to share two examples to display the working functionality of the C Break statement in both For loop and While loop

C Break Statement inside For Loop

In this program, We are going to use the C break statement inside for loop to exit from the loop iteration.

/* Break statement in C Programming example */

#include <stdio.h>

int main()
{
 int i;

 for(i=10;i>0; i--)
 {
  if(i==6)
   {
     printf("\n Coming out from for loop Where i = %d\n", i);
     break;
   }
  printf(" %d  ",i);
 }

}

C break output

 10   9   8   7  
 Coming out from for loop Where i = 6

Within the For loop, we initialized the value of i as: i =10. Next, we used the Decrement operator to decrement the value by 1. If you find it hard to understand the For loop, please visit our article: For Loop in C Programming

Inside the For loop, we placed the If statement to test whether i is equal to 6. If the condition is false, it skips the Break statement and prints that number as output (In Our case 10,9,8,7).

If this condition is True, the Break statement will execute, and the iteration will stop at that number without printing the other printf statement: printf(” %d “, i);

Break Statement inside While Loop

In this program, We are going to use the C break statement inside While loop to exit from the loop iteration.

/* Break statement in C Programming example */

#include <stdio.h>

int main()
{
 int i =0;

 while(i<=10)
  {
    printf("\n The Value of the Variable = %d \n", i);
    i++;
   
    if (i==4)
    {
      break;
    }
  }
 printf("\n This statement is from Outside the while Loop ");

 return 0;
}
Break statement in C Programming 2

Within this Break Statement example, We initialized the value of i as: i = 0 at the beginning of the code. Within the While loop, we check for the condition whether i is less than or equal to 10 or not. Please visit While Loop in CDo While loop and Continue Statement articles in C Programming.

Inside the While loop, we placed if statement to test whether i is equal to 4.

  • If the condition is false, it will skip the Break statement. Next, it prints that number as output (In Our case 0, 1, 2, 3).
  • If this condition is True, the Break statement will execute. It means the iteration will stop at that number without printing the other printf statement.

Comments are closed.