Continue Statement in C

The Continue statement in C Programming is another one that controls the flow of loops. This C Continue statement is 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.

In the Continue Statement in C example, we have 10 statements inside the loop. And we want to skip executing the second 5 lines (statement6 —statement10) when a certain condition is True, or else it has to execute all the 10 lines inside the loop. In these situations, we place the condition after the 5th statement, followed by this continue statement. If the condition is True, then it will stop executing statements 6 to 10. Otherwise, it will execute lines 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 Continue statement in both For loop and the While loop.

C Continue Statement inside For Loop Example

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

#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 C continue Statement in for loop program example,

  • Here we are not going to explain the for loop. If you don’t understand for loop, please visit our article For Loop in C Programming.
  • Inside the for loop, we placed the If condition 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 function: 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 C continue Statement program is the same as the above example. However, we replaced the for loop with a while loop.

#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 the 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 the If condition 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: printf(“Values are: %d “, i);.

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