Break in C
break immediately exits the nearest loop or switch. Execution continues with the statement after that loop or switch.
Syntax / Basic Pattern
| for (…) { if (condition) break; } |
Easy points to remember
- break exits only the nearest enclosing loop/switch.
- In switch, break normally stops fall-through.
- Use it when continuing the loop is no longer necessary.
| ⭐ PRACTICE TIP: Type these programs yourself. Change the values and run them again. That is how control statements become easy. |
Program 1: Stop at 5
| #include <stdio.h> int main(){for(int i=1;i<=10;i++){if(i==5)break;printf(“%d “,i);}return 0;} |
| OUTPUT 1 2 3 4 |
Program 2: Find 7
| #include <stdio.h> int main(){for(int i=1;i<=10;i++){if(i==7){printf(“Found”);break;}}return 0;} |
| OUTPUT Found |
Program 3: First even
| #include <stdio.h> int main(){for(int i=1;i<=10;i++){if(i%2==0){printf(“%d”,i);break;}}return 0;} |
| OUTPUT 2 |
Program 4: First multiple of 9
| #include <stdio.h> int main(){int i=1;while(i<=20){if(i%9==0)break;i++;}printf(“%d”,i);return 0;} |
| OUTPUT 9 |
Program 5: Stop negative
| #include <stdio.h> int main(){int a[]={5,4,3,-1,2};for(int i=0;i<5;i++){if(a[i]<0)break;printf(“%d “,a[i]);}return 0;} |
| OUTPUT 5 4 3 |
Program 6: Switch break
| #include <stdio.h> int main(){int c=2;switch(c){case 1:printf(“One”);break;case 2:printf(“Two”);break;default:printf(“Other”);}return 0;} |
| OUTPUT Two |
Program 7: Factorial stop
| #include <stdio.h> int main(){int f=1;for(int i=1;i<=10;i++){f*=i;if(f>100)break;}printf(“%d”,f);return 0;} |
| OUTPUT 120 |
Program 8: Search character
| #include <stdio.h> int main(){char s[]=”HELLO”;for(int i=0;s[i];i++){if(s[i]==’L’){printf(“Index=%d”,i);break;}}return 0;} |
| OUTPUT Index=2 |
Program 9: Stop at 0
| #include <stdio.h> int main(){int a[]={2,4,0,8};for(int i=0;i<4;i++){if(a[i]==0)break;printf(“%d “,a[i]);}return 0;} |
| OUTPUT 2 4 |
Program 10: Table stop
| #include <stdio.h> int main(){for(int i=1;i<=10;i++){printf(“%d “,6*i);if(i==5)break;}return 0;} |
| OUTPUT 6 12 18 24 30 |
Program 11: Prime factor
| #include <stdio.h> int main(){int n=35;for(int i=2;i<=n;i++){if(n%i==0){printf(“%d”,i);break;}}return 0;} |
| OUTPUT 5 |
Program 12: First vowel
| #include <stdio.h> int main(){char s[]=”rhythmA”;for(int i=0;s[i];i++){if(s[i]==’A’||s[i]==’E’||s[i]==’I’||s[i]==’O’||s[i]==’U’||s[i]==’a’||s[i]==’e’||s[i]==’i’||s[i]==’o’||s[i]==’u’){printf(“%c”,s[i]);break;}}return 0;} |
| OUTPUT A |
Program 13: Sum until 20
| #include <stdio.h> int main(){int s=0;for(int i=1;i<=100;i++){s+=i;if(s>=20)break;}printf(“%d”,s);return 0;} |
| OUTPUT 21 |
Program 14: First square >50
| #include <stdio.h> int main(){for(int i=1;;i++){if(i*i>50){printf(“%d”,i*i);break;}}return 0;} |
| OUTPUT 64 |
Program 15: Nested break
| #include <stdio.h> int main(){for(int i=1;i<=3;i++){for(int j=1;j<=3;j++){if(j==2)break;printf(“%d%d “,i,j);}}return 0;} |
| OUTPUT 11 21 31 |
