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 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 the Break statement and Continue statements in this 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. It means the break statement will exit the controller from the loop completely. Otherwise, it will run all the statements.
C break statement Syntax
The basic syntax behind this Break statement is as shown below:
break;
Break Statement in C Examples
The break statement is the most crucial statement in the 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 Break statement in both the For loop and the While loop.
C Break Statement inside For Loop
In this program, We are going to use the break statement inside for loop to exit from the loop iteration.
/* Break statement 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);
}
}
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);
C break Statement inside While Loop
In this program, We are going to use the break statement inside the While loop to exit from the loop iteration.
/* Break statement 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;
}

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 the While Loop in C, Do While loop, and Continue Statement articles in C Programming.
Inside the While loop, we placed an 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.
Does a break statement exit all nested loops in C?
No. When the compiler encounters a break statement, it will exit the current (innermost) loop where the break exits. The break statement is subjected to a particular loop block.
In the following example, we used two for loops (nested). Within the nested for loop, we used the break statement where j == 3. It means, on each row (i) iteration, when j becomes 3, it exits the inner loop and increments the i value by 1.
#include <stdio.h>
int main()
{
int i, j;
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= 10; j++)
{
if (j == 3)
{
break;
}
printf("i = %d, j = %d\n", i, j);
}
}
}
i = 1, j = 1
i = 1, j = 2
i = 2, j = 1
i = 2, j = 2
i = 3, j = 1
i = 3, j = 2
C break statement in nested loops solution
If your task is to exit the compiler from all loops, we must use the flag variable. First, set the flag value to false, and once j becomes 3, set the flag value to 1. The break statement exits the inner loop. The outer for loop has the flag value, and the condition fails.
#include <stdio.h>
int main()
{
int i, j;
int flag = 0;
for (i = 1; i <= 3 && !flag; i++)
{
for (j = 1; j <= 10; j++)
{
if (j == 3)
{
flag = 1;
break;
}
printf("i = %d, j = %d\n", i, j);
}
}
}
i = 1, j = 1
i = 1, j = 2
C break statement inside while(1) or infinite loop
One common scenario for using a break statement is to exit a while loop from entering into an infinite loop. In general, when working with gaming applications or any other user entries, we set the while loop to an infinite loop by using while(1) as the expression.
In the following example, we allow the user to enter the password using a while loop. Here, the program allows users to enter the password an infinite number of times until the correct password is given. Once the user enters the correct password, the strcmp() function compares with the original one, and if true, the break statement will exit the compiler from loop.
#include <stdio.h>
#include <string.h>
int main()
{
char password[20];
while (1)
{
printf("Enter Your Password: ");
scanf("%s", password);
if (strcmp(password, "User1234") == 0)
{
printf("Login Successful\n");
break;
}
printf("Wrong Password, try again\n");
}
printf("Welcome to the Coding World!");
}
Enter Your Password: new
Wrong Password, try again
Enter Your Password: name
Wrong Password, try again
Enter Your Password: User1234
Login Successful
Welcome to the Coding World!
Comments are closed.