C break statement
Break statement used inside in any loop or switch statements. Sometimes it is necessary to go outside from the loop even before the loop condition becomes false. in such conditions when terminates the loop break statement is used. This break statement cause an immediate exit from the loop in which break statement appear. example it is written as
break;
The break statement in C programming can be used in the following two scenarios:
1. With switch case
2. With loop
when break statement is encounter control is transfer immediately out from the loop. If break written a nested loop structure then it causes exit from the innermost loop.
Let take Example to understand the break statement uses
/* program to understand the use of break*/
main()
{
i;
(i=0;i<=10;i++)
{
(i==5)
printf("\n i=%d",i);
}
0;
}
|
in this example its normally executes 11 (0 to 10) times but here the value of i becomes 5 break is encounter and loop is terminated.
.
Output
C break statement with the nested loop
main() {
i=1,j=1;
for(i=1;i<=4;i++)
{
for(j=1;j<=4;j++)
{
printf("\n",i,j);
(i==3 && j==3)
{
}
}
}
0;
}
|
Output
1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4
3 1
3 2
3 3
4 1
4 2
4 3
4 4
|