In this tutorial you will learn about the C Break Statement and its application with practical example.
The c break statement inside any loop gives you a way to break or terminate the execution of the loop containing it and transfers the execution to the next statement following the loop. It is always used with a c if-else statement.
Break Statement Flowchart In C
Break Statement Inside Loops
Syntax Of Break Statement
Below is the general syntax of break statement in c programming:
Syntax:-
1 |
break; |
Break Statement Example
Below is a simple example to demonstrate the use of break statement in c programming:
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> int main() { int count = 0; printf("W3Adda - C Break Statement"); while(count <= 10){ count = count + 1; if(count == 5){ break; } printf("\nInside loop "); } printf("\nOut of while loop"); return 0; } |
In the above program, the variable count is initialized as 0. Then a while loop is executed as long as the variable count is less than 10. Inside the while loop, the count variable is incremented by 1 with each iteration (count = count + 1). Next, we have an if statement that checks the variable count is equal to 5, if it returns TRUE causes the loop to break or terminate. Within the loop, there is a printf statement that will execute with each iteration of the while loop until the loop breaks. Then, there is a final printf statement outside of the while loop. When we above c program, our output will be as follows –
Output:-