The Ultimate Guide to Using the Continue Statement in C
continue statement in c is useful with the next repetition of a loop. When continue inside a loop is executed, the flow of execution goes back to the starting of the loop, and the next repetition will take place, When continue inside a loop is executed, the subsequent statements in the loop are not executed, but the statements preceding continue will be executed.
Syntax: continue;
for loop to display numbers from 10 to 1.
Example: for(i=10; i>=1;i++)
{
printf("\n before continue");
if(i>5) continue;
printf("%d",i);
}
In the above example, for i values 10, 9, 8, 7, and 6, the continue statement is executed, and hence the compiler jumps back to the beginning of the loop. Thus, the subsequent statement in the loop, i.e printf(), is not executed. so the numbers 10, 9, 8, 7, and 6 are not displayed. When the i value is 5 or less, the condition becomes false, and hence, continue will be executed. So, the compiler executes the next statement, i.e., printf(), and hence the numbers from 5 to 1 will be displayed.
Program to understand the use of continue.
#include<stdio.h>
void main()
{
int i;
for(i=10;i>=1'i++)
{
printf("Before continue");
if(i>5)
continue;/* this is infinite loop */
printf("%d",i);
}
}
Output:
Before continue:
Before continue:
Before continue:
Before continue:
Before continue:
Before continue:
Before continue: 5
Before continue: 4
Before continue: 3
Before continue: 2
Before continue: 1