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 compiler finds the C 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. However, there will be situations where we have to terminate the loop without executing all the statements. We can use the break and continue statements in C programming in these situations.

A Scenario of 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 specific condition is True; otherwise, it has to execute them. In these situations, we can place this one inside the If condition. The C compiler will execute the break statement if the condition is True. It means this statement will exit the controller from the loop entirely. Otherwise, it will run all the lines of code.

C break statement syntax

The basic syntax behind this 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 want to share two examples to display the working functionality of the C break statement in both For loop and the While loop.

C break Statement inside For Loop

In this program, we will use this one inside the 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 Where i = %d\n", i);
     break;
   }
  printf(" %d  ",i);
 }

}

output

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

Analysis

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 Example.

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

C break Statement Inside a While Loop

In this program, we will use the break statement in the 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

Analysis

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

Inside it, we placed the if condition to test whether i equals 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. The iteration will stop at that number without printing the other printf function.

Comments are closed.