continue statement in c

Continue statement in c

The continue statement is used to skip some statements and go to next iteration of the loop. The continue statement can be written as

continue;

it is generally used with the condition. its force to the next iteration and skipping the code of current iteration or not executes the current statements when continue statement encounter.

The difference between break and continue is that when the break statement is encountered the loop terminates, And when the continue statement is encounter then the loop is not terminate but the next iteration of the loop starts.

/* program to understand the use of continue statement*/
#include<stdio.h>
void main()
{
int i;
for(i=0;i<=10;i++)
{
if(i==5)
continue;
printf("\n i=%d",i);
}
return 0;
}

In this example when iteration i value becomes 5 then continue statement encountered and skip the all code for the iteration 5 and go to the next iteration. .

Continue statement Example two

/* program to understand the use of continue statement*/
#include<stdio.h>
int main()
{
int i;//initializing i local variable
//starting a loop from 1 to 10
for(i=1;i<=10;i++)
{
if(i==4)
{//if value of i is equal to 4, it will continue the loop
continue;
}
printf("%d \n",i);
}
//end of for loop
return 0;
}

output

1
2
3
5
6
7
8
9
10

C continue statement with inner loop

/* program to understand the use of continue statement with inner loop*/
#include<stdio.h>
int main()
{
int i,j;//declare local variable
for(i=1;i<=4;i++)
{
for(j=1;j<=4;j++)
{
if(i==3 && j==3)
{
continue;//will continue loop of j only
}
printf("%d %d\n",i,j);
}
}//end of for loop
return 0;
}

output

4 2
4 3
4 4