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

  1. //Using break statement
  2. #include <stdio.h>
  3. void main()
  4. {
  5. int a;
  6. while(1)
  7. {
  8. printf("\n\nEnter 1 to break the loop : ");
  9. scanf("%d",&a);
  10. if(a==1)
  11. break;
  12. else
  13. printf("Didn't break the while loop");
  14. }
  15. printf("\nWhile loop terminated");
  16. }

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'.