Table of Contents

    Understanding the continue Statement in C: Usage and Examples

    Understanding the continue Statement in C: Usage and Examples

    continue statement

    Sometimes it is useful to force an early iteration of a loop. That is, you might want to continue running the loop but stop processing the remainder of the code in its body for this particular iteration.

    The C continue statement is used to continue loop. It continues the current flow of the program and skips the remaining code at specified condition. In case of inner loop, it continues only inner loop.

    The continue statement skips some statements inside the loop. The continue statement is used with decision making statement such as if...else.

    Syntax of break statement

    
    continue;
    

    The simple code above is the syntax for break statement.

    Flowchart of break statement

    if statement in C

    How break statement works?

    if statement in C

    Example of continue statement

    In output 5 is missing for continue statement

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

    Output

    1
    2
    3
    4
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    Press any key to continue . . .

    Example of Continue Statement with Inner Loop

    It continues inner loop only if you use continue statement inside the inner loop.

    
    #include<stdio.h>
    void main()
    {
    	int i, j;
        for(i=1;i<=2;i++){
             for(j=1;j<=3;j++){
                     if(i==2&&j==2){
                       continue;
                      }
                  printf(" %d ",i);
                  printf(" %d \n",j);
               }
               
            }
    
    }
    
    

    Output

    1  1
     1  2
     1  3
     2  1
     2  3
    Press any key to continue . . .

    In a for loop, the continue keyword causes control to immediately jump to the update statement.

    In a while loop or do/while loop, control immediately jumps to the Boolean expression.

    Example of continue statement

    In the program, when the user enters positive number, the sum is calculated using sum += number; statement.
    When the user enters negative number, the continue statement is executed and skips the negative number from calculation.

    
    // Program to calculate sum of maximum of 10 numbers
    // Negative numbers are skipped from calculation
    
    # include<stdio.h>
    int main()
    {
        int i;
        double number, sum = 0.0;
    
        for(i=1; i <= 10; ++i)
        {
            printf("Enter a n%d: ",i);
            scanf("%lf",&number);
    
            // If user enters negative number, loop is terminated
            if(number < 0.0)
            {
                continue;
            }
    
            sum += number; // sum = sum + number;
        }
    
        printf("Sum = %.2lf \n",sum);
        
        return 0;
    }
    
    

    Output

    Enter a n1: 12
    Enter a n2: 3
    Enter a n3: -2
    Enter a n4: 2
    Enter a n5: -11
    Enter a n6: 5
    Enter a n7: 4
    Enter a n8: -4
    Enter a n9: 2
    Enter a n10: 1
    Sum = 29.00 
    Press any key to continue . . .