C break statement


The break statement in C language is used to break the execution of loop (while, do while and for) and switch case

There can be two usage of break keyword in C

  • With switch case
  • With loop

Example of C break statement with switch case

 #include<stdio.h>
#include<conio.h>


void main(){ 
int number=0; 
clrscr(); 
printf("enter a number:"); 
scanf("%d",&number); 
switch(number){ 
case 10: 
printf("number is equals to 10"); 
break; 
case 50: 

printf("number is equal to 50"); 
break; 
case 100: 
printf("number is equal to 100"); 
break; 
default: 
printf("number is not equal to 10, 50 or 100"); 
} 
getch(); 

} 				

Output:

enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50
				 

Example of C break statement with loop

#include<stdio.h>
#include<conio.h>

void main(){ 
inti=1;//initializing a local variable
clrscr(); 

//starting a loop from 1 to 10
for(i=1;i<=10;i++){ 
printf("%d \n",i); 
if(i==5){//if value of i is equal to 5, it will break the loop
break; 
} 
}//end of for loop

getch(); 
} 
				

Output:

1
2
3
4
5