Break Statement in C Language
The break statement is a fundamental keyword in the C programming lang
break is encountered within a nested loop, it terminates the innermost loop first before progressing to the outer loops. The break statement in C finds utility in two main scenarios: within switch cases and loops.
Syntax:
// Inside a loop or switch case
break;
Flowchart of the break Statement in C:

Example of Using break in a Loop:
#include <stdio.h>
#include <stdlib.h>
void main()
{
int i;
for (i = 0; i < 10; i++)
{
printf("%d ", i);
if (i == 5)
break;
}
printf("came outside of loop i = %d", i);
}
Output:
0 1 2 3 4 5 came outside of loop i = 5
Using break with Nested Loops in C language:
In scenarios involving nested loops, the break statement selectively terminates only the innermost loop while allowing the outer loops to continue their execution.
#include <stdio.h>
int main()
{
int i = 1, j = 1; // Initializing local variables
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= 3; j++)
{
printf("%d %d\n", i, j);
if (i == 2 && j == 2)
break; // Will break only the inner loop
}
}
return 0;
}
Output:
1 1
1 2
1 3
2 1
2 2
3 1
3 2
3 3
As evident from the output, the numbers “2 3” are not printed since the break statement is encountered after printing “2 2.” However, “3 1,” “3 2,” and “3 3” are printed because the break statement influences only the inner loop’s execution.
Using break with a while Loop in C language:
Consider this example demonstrating the use of the break statement within a while loop:
#include <stdio.h>
void main()
{
int i = 0;
while (1)
{
printf("%d ", i);
i++;
if (i == 10)
break;
}
printf("came out of while loop");
}
Output:
0 1 2 3 4 5 6 7 8 9 came out of while loop
Using break with a do-while Loop in C language:
This example illustrates the use of the break statement in conjunction with a do-while loop:
#include <stdio.h>
void main()
{
int n = 2, i, choice;
do
{
i = 1;
while (i <= 10)
{
printf("%d X %d = %d\n", n, i, n * i);
i++;
}
printf("Do you want to continue with the table of %d? Enter any non-zero value to continue.", n + 1);
scanf("%d", &choice);
if (choice == 0)
{
break;
}
n++;
} while (1);
}
Output:
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20
Do you want to continue with the table of 3? Enter any non-zero value to continue: 1
3 X 1 = 3
3 X 2 = 6
3 X 3 = 9
3 X 4 = 12
3 X 5 = 15
3 X 6 = 18
3 X 7 = 21
3 X 8 = 24
3 X 9 = 27
3 X 10 = 30
Do you want to continue with the table of 4? Enter any non-zero value to continue: 0
In the final example, the break statement is employed within a do-while loop to control the continuation of table generation based on user input.
Discover more from PiEmbSysTech - Embedded Systems & VLSI Lab
Subscribe to get the latest posts sent to your email.




