The break statement is used for the termination of the loop or switch statement.
The syntax for a break statement in C is as follows :
break;
Example 1 :
#include<stdio.h>
int main()
{
int i=0;
while(i < 10)
{
i++;
if(i == 5 )
break;
}
printf("Loop stopped at i = %d",i);
return 0;
}
int main()
{
int i=0;
while(i < 10)
{
i++;
if(i == 5 )
break;
}
printf("Loop stopped at i = %d",i);
return 0;
}
Output :
Loop stopped at i = 5
Example 2 :
#include<stdio.h>
int main()
{
int number;
printf("Enter a number between 1 and 5 : ");
scanf("%d",&number);
switch(number)
{
case 1:
printf("You chose One");
break;
case 2:
printf("You chose Two");
break;
case 3:
printf("You chose Three");
break;
case 4:
printf("You chose Four");
break;
case 5:
printf("You chose Five.");
break;
default :
printf("Invalid Choice!");
break;
}
}
int main()
{
int number;
printf("Enter a number between 1 and 5 : ");
scanf("%d",&number);
switch(number)
{
case 1:
printf("You chose One");
break;
case 2:
printf("You chose Two");
break;
case 3:
printf("You chose Three");
break;
case 4:
printf("You chose Four");
break;
case 5:
printf("You chose Five.");
break;
default :
printf("Invalid Choice!");
break;
}
}
Output :
Enter a number between 1 and 5 : 2
You chose Two
You chose Two
0 Comments