break statement
break statement is used to come out of the loop instantly. As soon as the break statement is encountered from within a loop, the loop iterations stops there and control returns from the loop immediately to the first statement after the loop.
The syntax of the break statement is as follows:
break;
#include stdio.h;
#include conio.h;
void main()
{
int n;
for(n = 1; n5; n++)
{
printf("%d ",n);
if(n == 3)
break;
}
printf("last value of loop is = %d",n);
getch();
}
Output
1 2 3 last value of loop is n = 3
This can also be used in switch case control structure. Whenever it is encountered in switch-case block, the control comes out of the switch-case.
#include stdio.h
#include conio.h
void main()
{
int m;
printf("Enter value of m=");
scanf("%d",&m);
switch (m)
{
case 1:
printf("You have entered value 1\n");
break;
case 2:
printf("You have entered value 2\n");
break;
case 3:
printf("You have entered value 3\n");
break;
default:
printf("Input value is other than 1,2 & 3 ");
}
getch();
}
Output
Enter value of m=1
You have entered value 1