Break Statement
Description
Break statement terminates the current execution of loop(block) like while, do-while, for loop etc. and passes the execution control to the next statement after the loop.
Source code
- //Using break statement
- #include <stdio.h>
- void main()
- {
- int a;
- while(1)
- {
- printf("\n\nEnter 1 to break the loop : ");
- scanf("%d",&a);
- if(a==1)
- break;
- else
- printf("Didn't break the while loop");
- }
- printf("\nWhile loop terminated");
- }
Program working steps
- Comment, declaration of preprocessor directive and void main function at the start.
- Variable int a is declared, while(1) statement is a repetative loop it will terminate only when break statement is executed.
- Programs asks the user to enter 1 to break the loop, if user enters 1 it will break the while loop printing 'While loop terminated'. Else, if user enters any other integer value the loop will be repeated, printing 'Didn't break the while loop'.