Break Statement in C Language

This “break statement C language Tutorial” post explains clearly what is break statement, how it breaks the loop execution, where we use break statement and much more with examples.


 

break :

  • Branching statement used to terminate execution of iterator(loop).
  • Must be placed either inside the loop or inside switch statement only.
  • break statement terminates only the execution of current loop and continues with remaining statements execution which are followed by loop.

 

#include<stdio.h>
main()
{
    if(1)
    {
        printf(“Hello\n”);
        break; //CE : break is not within the loop or switch
    }
}

#include<stdio.h>
main()
{
    while(-17)
    {
        break; 
        printf(“Hello\n”);
    }
    printf(“Bye\n”);
}

#include<stdio.h>
main()
{
    int i;
    for(i=1 ; i<=10 ; i++)
    {
        if(i==5)
        {
            break;
        }
        printf(“i : %d\n”,i);
    }
}

Note : if break statements executes inside the inner loop, it terminates the execution of inner loop only.
#include<stdio.h>
main()
{
    while(1)
    {
        while(1)
        {
            printf(“Outer loop\t”);
            break;
        }
        printf(“Inner loop\n”);
    }        
}

#include<stdio.h>
main()
{
    int i,j;
    for(i=1 ; i<=5 ; i++)
    {
        for(j=1 ; j<=5 ; j++)
        {
            if(i==j)
            {
                break;
            }
            printf(“i : %d \t j : %d \n”, i, j);
        }
    }
}

Share this