Break Statement in C
#Break statement are used for terminates any type of loop e.g, while loop, do while loop or for loop. The break statement terminates the loop body immediately and passes control to the next statement after the loop. In case of inner loops, it terminates the control of inner loop only.
Use break statement
Break statement are mainly used with loop and switch statement. often use the break statement with the if statement.
- with loop statement
- with switch case
- with if statement
Syntax
jump-statements (loop or switch case); break;
Example of break
#include<stdio.h> #include<conio.h> void main() { char key; printf("Press any key or E to exit:\n"); while(1) { scanf("%c", &key); if (key == 'E' || key == 'e') break; } printf("Good bye !\n"); }
Explanation:In the above code when user enter any character, if the user enters E or e, the break statement terminates the while loop and control is passed to the statement after the while loop that displays the Good bye !message.
Output
Press any key or E to exit Good bye !