C break statement

The break statement in C language is used to break the execution of loop (while, do while and for) and switch case

In case of inner loops, it terminates the control of inner loop only.
There can be two usage of C break keyword:
  • With switch case
  • With loop
  • Syntax:
    jump-statement;  
    break;  
    
    The jump statement in c break syntax can be while loop, do while loop, for loop or switch case.

    Flowchart of break in c


    Example of C break statement with switch case

    #include <stdio.h>
    void main( ) {    
    int i = 2;    //initializing a local variable      
    //starting a loop from 2 to 12  
    for(i = 2; i<= 12; i++)  {    
    printf("%d ",i);  
    if(i == 6)  {    //if value of i is equal to 6, it will break the loop  
    break;  
    }  
    }    //end of for loop        
    }   
    
    Output:  2   3  4    5   6 
    
    As you can see on console output, loop from 2 to 12 is not printed after i==6.
    C break statement with inner loop
    In such case, it breaks only inner loop, but not outer loop.
    #include <stdio.h>
    void main( ) {    
    int i = 2,j = 2;     //initializing a local variable    
    for(i = 2; i <= 4; i++) {    
    for(j = 2; j<= 4; j++) {  
    printf("%d%d ", i, j);  
    if(i == 3 && j == 3)  {  
    break;   //will break loop of j only  
    }  
    }  
    }     //end of for loop  
    }
    Output:   22   23   24  32   33   42   43   44
    
    As you can see the output on console, 2 3 is not printed because there is break statement after printing i==3 and j==3. But 4 2, 4 3 and 4 4 is printed because break statement works for inner loop only.

    C continue statement

    The continue statement in C language is used to continue the execution of loop (while, do while and for). It is used with if condition within the loop.
    In case of inner loops, it continues the control of inner loop only.

    Syntax:
    jump-statement;  
    continue;   
    
    The jump statement can be while, do while and for loop.

    Example of continue statement in c

    #include <stdio.h>
    void main( )  {    
    int i = 2;   //initializing a local variable      
    for(i = 2; i <= 11; i++) {    
    if(i == 6) {    //if value of i is equal to 5, it will continue the loop  
    continue;  
    }  
    printf("%d ",i);  
    }    //end of for loop      
    } 
    Output:  2   3   4   5    7    8    9    10    11
    
    As you can see, 6 is not printed on the console because loop is continued at i==6.

    C continue statement with inner loop

    In such case, C continue statement continues only inner loop, but not outer loop.
    #include <stdio.h>
    void main( )  {    
    int i = 2, j = 2;     //initializing a local variable      
    for(i = 2; i<= 4; i++)  {    
    for(j = 2; j <= 4; j++)   {  
    if(i == 3 &&  j == 3)   {  
    continue;     //will continue loop of j only  
     }  
    printf("%d%d ",i,j);  
     }  
     }      //end of for loop       
      }   
    Output:  22   23   24   32   34    42    43    44   
    
    As you can see, 3 3 is not printed on the console because inner loop is continued at i==3 and j==3.