break statement in c language


  • The break is a keyword in C which is used to bring the program control out of the loop. The break statement is used inside loops or switch statement. The break statement breaks the loop one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops.
  • The keyword break allows the programmers to terminate the loop. The break skips from the loop or block in which it is defined.
  • The control then automatically goes to the first statement after the loop or block. The break can be associated with all conditional statements.
  • We can also use break statements in the nested loops. If we use break statement in the innermost loop, then the control of the program is terminated only from the innermost loop.
  • The break statement causes an immediate exit from a loop. Because the code that follows it in the loop is not executed if the break is reached, you can also sometimes avoid nesting by including a break.
  • It is possible to force an immediate exit from a loop, bypassing the loop’s conditional test, by using the break statement.

Syntax:

    break;
    

Flow Diagram break statement:

    break statement in c

Example:

#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;
}

Output:

value of variable num is: 0
value of variable num is: 1
value of variable num is: 2
Out of while-loop