C continue statement

The continue statement can only appear in a loop body. It does not operate on switch. It causes the rest of the statement body in the loop to be skipped.

For while and do-while it causes transfer of program-control immediately to the conditional test, which is reevaluated for the next iteration. The same action occurs for a for-loop after first executing the increment expression (i.e., expression 3). Note that, as with break, continue acts on the inner-most enclosing block of a nested loop.

Syntax
//loop statements
continue;
//some lines of the code which is to be skipped

Continue statement example 1

snippet
#include<stdio.h>
void main ()
{
	int i = 0; 
	while(i!=10)
	{
		printf("%d", i); 
		continue; 
		i++;
	}
}
Output
infinite loop

Continue statement example 2

snippet
#include
int main(){
int i=1;//initializing a local variable     
//starting a loop from 1 to 10  
for(i=1;i<=10;i++){    
if(i==5){//if value of i is equal to 5, it will continue the loop  
continue;  
}  
printf("%d \n",i);  
}//end of for loop  
return 0;
}
Output
1 2 3 4 6 7 8 9 10

As you can see, 5 is not printed on the console because loop is continued at i==5.

C continue statement with inner loop

In such case, C continue statement continues only inner loop, but not outer loop.

snippet
#include
int main(){
int i=1,j=1;//initializing a local variable  
for(i=1;i<=3;i++){    
for(j=1;j<=3;j++){  
if(i==2 && j==2){  
continue;//will continue loop of j only  
}  
printf("%d %d\n",i,j);  
}  
}//end of for loop  
return 0;
}
Output
1 1 1 2 1 3 2 1 2 3 3 1 3 2 3 3

As you can see, 2 2 is not printed on the console because inner loop is continued at i==2 and j==2.

Note
Both break and continue have no effect on an if-statement.

You cannot use break to jump out of an if compound statement. For example, given a nested construct of the form,

while (expression) {
    statements
    if (expression) {
        statements
        if (expression)
            break;
        statements
    }
}
statements after if statements after loop

In the above example the break will not transfer control to the statements after if, whereas it will actually transfer control to the statements after loop.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +