if we want to break the execution of loop in while, do while and for loop and switch case we use break satement.
Syntax:
jump-statement;
break;
below are examples to illustrate break statement:
Example1
Example of C break statement with loop
#include <stdio.h>
#include <conio.h>
void main(){
int i=1;
for(i=1;i<=10;i++){
printf("%d \n",i);
if(i==7){//if value of i is equal to 7, it will break the loop
break;
}
}
}
Output
1
2
3
4
5
6
7
Example2
C break statement with inner loop
#include <stdio.h>
#include <conio.h>
void main(){
int i=1,j=1;
for(i=1;i<=3;i++){
for(j=1;j<=3;j++){
printf("%d &d\n",i,j);
if(i==2 && j==2){
break;
}
}
}
Output
1 1
1 2
1 3
2 1
2 2
3 1
3 2
3 3
0 Comment(s)