Continue Statement in C

The Continue statement in C Programming is another one that controls the flow of loops. This C Continue statement used inside For Loop, While Loop and Do While Loops. While executing these loops, if the compiler finds the continue statement inside them, then the loop will stop the current iteration and starts the new iteration from the beginning.

For example, we have 10 statements inside the loop. And we want to skip executing the second 5 statements (statement 6 —statement 10) when a certain condition is True or else it has to execute all the 10 statements inside the loop. In these situations, we place the condition after the 5th statement, followed by this C continue statement. If the condition is True, then it will stop executing statements 6 to 10. Otherwise, it will execute statements 1 to 10. The syntax of the Continue Statement in C Programming is as follows:

continue;

Continue statement in C Example

In this article, We would like to share two examples to display the working functionality of the C Continue statement in both For loop and While loop

C Continue Statement inside For Loop Example

In this example, we will show you how to use C Continue Statement inside the For Loop with example. This program allows the user to enter any integer values. Then it will display the Even and Odd numbers inside the integer value.

/* Continue Statement in C Programming example */
#include <stdio.h>

int main()
{
 int i, number;

 printf("\n Please Enter any integer\n");
 scanf("%d", &number);

 for(i=1;i<= number; i++)
  {
   if(i%2 != 0)
   {
     printf("\n Odd Numbers = %d(Skipped By Continue)\n",i);
     continue;
   }
   printf("\n Even numbers = %d\n",i);
 }

}
Continue Statement in C Programming 1

Within this continue Statement in for loop program example,

  • Here we are not going to explain the for loop. If you don’t understand for loop, then please visit our article For Loop in C Programming.
  • Inside the for loop, we placed If Statement to test whether (i%2 != 0). If this condition is True, the continue statement will execute, and the iteration will stop at that number without printing the other printf statement: printf(“\n Even numbers = %d\n”, i).
  • If the condition is false, it skips the continue statement and prints that number as output (In Our case Even Number)

C Continue Statement inside While Loop Example

The continue Statement program is the same as the above example. However, we replaced the for loop with a while loop.

/* Continue Statement in C Programming example */

#include<stdio.h>

int main()
{
 int i=0;

 while (i<= 10)
  {
   if (i== 5 || i == 9)
    {
      printf("Skipped Values = %d\n", i);
      i++;
      continue;
    }
   printf("Values = %d\n", i);
   i++;
  }

return 0;
}
Values = 0
Values = 1
Values = 2
Values = 3
Values = 4
Skipped Values = 5
Values = 6
Values = 7
Values = 8
Skipped Values = 9
Values = 10

Within this C continue Statement in while loop example, Here we are not going to explain the While loop. Please visit our While Loop in C and Do While loop articles in C Programming.

Inside the While loop, we placed If Statement to test whether i is equal to 5 or 9. If this condition is True, the continue statement will run, and the iteration will stop at that number without printing the other printf statement: printf(“Values are: %d “, i);.

For better understanding, we placed printf(“Skipped Values = %d\n “, i); inside the If condition. So, whenever the iteration break, then that value be printed from this printf statement. If the condition is false, then it will skip the continue statement and prints that number as output (In Our case 0,1,2,3,4,6,7,8,10)