break statement in C programming –
C – break statement
- It is used to quickly come out of a loop. When a break statement is encountered inside the loop, control directly comes out of the loop and the loop terminates. It is used with if statement whenever it is used inside a loop.
- It can also be used in switch case control structure. Whenever it is encountered in a switch-case block, the control comes out of the switch-case (see example below).
Read Also – Explain GoTo Statement in C ?
Flow diagram of break statement
![](https://devopslover.com/wp-content/uploads/2022/12/image-2.png)
Syntax:
break;
Example – Use of break in a while loop
#include <stdio.h> int main() { int num =0; while(num<=100) { printf("value of variable num is: %d\n", num); if (num==2) { break; } num++; } printf("Out of while-loop"); return 0; }
value of variable num is: 0 value of variable num is: 1 value of variable num is: 2 Out of while-loop
Example – Use of break in a for loop
#include <stdio.h> int main() { int var; for (var =100; var>=10; var --) { printf("var: %d\n", var); if (var==99) { break; } } printf("Out of for-loop"); return 0; }
Output:
var: 100 var: 99 Out of for - loop
Example – Use of break statement in switch-case
#include <stdio.h> int main() { int num; printf("Enter value of num:"); scanf("%d",&num); switch (num) { case 1: printf("You have entered value 1\n"); break; case 2: printf("You have entered value 2\n"); break; case 3: printf("You have entered value 3\n"); break; default: printf("Input value is other than 1,2 & 3 "); } return 0; }
Output:
Enter value of num:2 You have entered value 2
Tag – break statement in C programming
Break statement in C programming
![break statement in C programming](https://devopslover.com/wp-content/uploads/2024/01/image-60-e1706019199337.png)
Latest posts by Mahesh Wabale (see all)
- Sending slack notification via pipeline job - August 25, 2024
- SonarQube integration with Jenkins - August 24, 2024
- Deployment of 2 application java and python using k8s : slack alerting Using Jenkins pipeline liberary - August 10, 2024