Skip to main content

break statement in C

/*
The break is a keyword in C which is used to bring the program control out of the loop.
 The break statement is used inside loops or switch statement. The break statement 
 breaks the loop one by one, i.e., in the case of nested loops, it breaks the inner
 loop first and then proceeds to outer loops.
 */
//example of break statement in loop statement
#include<stdio.h>  
int main ()  
{  
    int i;  
    for(i = 0i<10i++)  
    {  
        printf("%d ",i);  
        if(i == 5)  
        break;  
    }  
    printf("came outside of loop i = %d",i);  
      
}  

//examples of break statement in switch statement
#include<stdio.h>  
int main(){    
int number=0;     
printf("enter a number:\n");    
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");    
}    
return 0;  
}


Comments