Continue Statement in C Language

Continue Statement in C Language

In this article, I am going to discuss Continue Statement in C Language with examples. Please read our previous articles, where we discussed Break Statement in C Program. At the end of this article, you will understand what is Continue Statement in C and when and how to use continue statement in C with examples.

Continue Statement in C:

Continue is a keyword. By using continue we can skip the statement from the loop body. Using continue is always optional but it should be placed within the loop body only. In an implementation where we know the maximum number of repetition but some condition is there we need to skip the statement from loop body then we need to go for continue statement.

The continue statement provides a convenient way to immediately start the next iteration of the enclosing FOR, WHILE, or REPEAT loop. Whereas the BREAK statement exits from a loop, the continue statement exits only from the current loop iteration, proceeding immediately to the next iteration. The continue statement is almost always used with the if…else statement. For the for loop, the continue statement causes the conditional test and increment portions of the loop to execute.

For the while and do-while loops, continue statement causes the program control to pass to the conditional tests.

Continue Statement in C

How continue statement works in C?

How continue statement works in C?

Syntax: continue;

Program to understand Continue statement in c:
#include <stdio.h>
int main ()
{
    int counter = 10;
    while (counter >= 0)
    {
      if (counter == 7)
   {
      counter--;
      continue;
   }
      printf ("%d ", counter);
      counter--;
    }
    return 0;
}

Output:

if we do not place the counter– statement in the body of “if” then the value of counter would remain 7 indefinitely. The print statement is skipped when the counter value was 7.

Note: When the continue statement is executed within the loop body then control will pass back to the condition without executing the remaining statement.

Some tricky questions related to C Continue statement.
Question1: What will be the output in the below program?
#include <stdio.h>
int main()
{
    int a = 10;
    while(a <= 25)
    {
        a += 2;
        if(a >15 && a < 20)
                continue;
        printf("%d ", a);
    }
    return 0;
}

Output: 12 14 20 22 24 26

Question2: What will be the output in the below program?
#include <stdio.h>
int main()
{
    int a = 1;
    while(a <= 50)
    {
        printf("%d ", a);
        if(a >= 5 && a <= 35)
                continue;
        a = a+ 2; 
    }
    return 0;
}

Output: infinite loop

Question3: What will be the output in the below program?
#include <stdio.h>
int main()
{
    int a = 8;
    while(a <= 42);
    {
        a += 2;
        if(a >= 20 && a <= 30)
                continue;
        printf("%d", a);
    }
    return 0;
}

Output:

Note: When the semicolon (;) is placed after the while then it becomes a dummy loop. When the dummy loop is created then the compiler will create an empty body without any statement and the current body becomes outside. Then automatically continue is placing outside then it becomes an error.

In the next article, I am going to discuss Return Statement in C with examples. Here, in this article, I try to explain Continue Statement in C Language with examples. I hope you enjoy this article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

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