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 code and immediately exit from the loop.

The C break and continue 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 code 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 and continue statements in C programming.

break Statement in C

For example, we have five lines of code 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 this one inside the If condition. If the condition is True, the compiler will execute the break statement in C. It means this statement will exit the controller from the loop completely. Otherwise, it will run all the lines of code.

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

break;

break Statement in C Examples

This statement is the most crucial one in Switch Case. Without using this, 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 iteration.

#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);
 }

}

break in c output

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

First, 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 code, please visit our article: For C Programming

Inside the For iterator, we placed the If condition to test whether i is equal to 6. If the condition is false, it skips the Break line 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 : printf(” %d “, i);

Break Statement inside While Loop

In this program, We are going to use the break in c While loop to exit from the iteration.

#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 example, We initialized the value of i as: i = 0 at the beginning of the code. In the While loop, we check for the condition whether i is less than or equal to 10 or not. Please visit WhileDo While and Continue articles in C Programming.

Inside it, we placed if condition 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 function.

Comments are closed.