The continue Statement

In this tutorial, we will understand the continue statement in C. This statement gives us more control over our loops and gives more flexibility to perform difficult tasks with ease. So, let’s dive in.


Understanding the Continue Statement

The continue statement is a control statement which can be used within a loop. When a continue statement is encountered, the control shifts to the beginning of the loop, skipping the execution of any subsequent statements in the current loop iteration. Note that the break statement can be used with loops and switch, but the continue statement can only be used within a loop.

Purpose

The primary purpose of the continue statement is to skip the current iteration of the loop without terminating the loop. This is particularly useful when for some reason, you want to continue to the next iteration early without completely implementing the current iteration.

Example

The continue statement can be used with for, while, and do-while loops. Here is a simple example demonstrating the usefulness of the continue statement:

// C program to display odd numbers from 1 to 10
#include <stdio.h>

int main()
{
    for (int i = 1; i <= 10; i++) {
        /* if the number is even, skip the current iteration
           and move to the next iteration. At this point of time, 
           the control shifts to i++.
        */
        if (i % 2 == 0) { 
            continue;
        }
        // print the odd numbers
        printf("%d ", i);
    }
    return 0;
}

Output:

1 3 5 7 9

Use Case

The continue statement is quite useful to filter out certain elements within a list. Maybe this list represents your financial transactions in the last month including positive integers representing the income you have generated and the negative integers representing your expenses. Now, you want to write a program to calculate the total income by filtering out the negative numbers from the list and by adding only the positive numbers representing the income. Here is the program:

// C program to calculate total income
#include <stdio.h>

int main()
{
    int transactions[] = {1000, 1500, -200, -600, 10000, 1200, -1500};
    int size = sizeof(transactions) / sizeof(transactions[0]);
    int total;
    
    
    for (int i = 0; i < size; i++) {
        // Skip the iteration if the number is negative or zero
        if (transactions[i] <= 0) 
            continue;
        // otherwise, add the current transaction to the total.     
        total += transactions[i];
    }
    
    printf("Your total income in the last month is $%d/-", total);
    return 0;
}

Output:

Your total income in the last month is $13700/-

Level-Up Your Understanding

Question 1: How would you use a continue statement within a while loop to print numbers from 1 to 10 except 5?

Answer: The continue statement with a while loop to print numbers from 1 to 10 except 5 looks like the following:

// The continue statement within while loop
#include <stdio.h>

int main()
{
    int i = 0;
    while (++i <= 10) {
        if (i == 5)
            continue;
        printf("%d ", i);
    }
    return 0;
}

Output:

1 2 3 4 6 7 8 9 10

Question 2: How continue statement can be used with a switch statement inside a loop?

Answer: The following example shows how the continue statement can be used with switch within a for loop.

#include <stdio.h>

int main()
{
    for (int i = 0; i <= 10; i++) {
        switch (i) {
            // When value is 0 or 2, skip the iteration.
            case 0:
            case 2: 
                continue;
            default:
                printf("%d ", i); // print values other than 0 and 2.   
        }
    }
    return 0;
}

Output:

1 3 4 5 6 7 8 9 10

Question 3:  Determine the output of the following C program:

int main()
{
    for (int i = 0; ; ) {
        printf("%d ", i);
        if (++i >= 5) break;
        else continue;
    }
    return 0;
}

Answer:  The output of the above program is 0 1 2 3 4. The loop will start from value 0 and it prints all values till 4 because when it reaches 5, the condition ++i >= 5 becomes true, and hence the loop will terminate. 

Question 4:  Consider the following code snippet:

for (int i = 0; i < 5; i++) {
    (i % 2 == 0) ? continue : printf("%d ", i);
}

What will be the output of the above program?

Answer:  Error. Compiler will generate an error as the continue statement cannot be used with the ternary conditional operator. 

Question 5:  Determine the output of the following C program:

#include <stdio.h>

int main()
{
    int i = 0;
    dothis: for (int j = 10; j > 5; j--) printf(" %d", j);
    while (i++ < 5) {
        if (i == 5)
            continue dothis;
        printf("%d ", i);    
    }
    return 0;
}

Answer:  Error. A label cannot be used with the continue statement. 


Conclusion

The continue statement is a powerful tool in C that allows us to have more control over the flow of execution of the loops. It allows you to skip an iteration based on some condition which is quite useful in various situations as observed in the “Use Case” section of this tutorial. Having this tool in your toolkit is a must if you want to handle different situations with elegance through your programs. 



Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.