Wednesday, February 4, 2015

C continue statement


C continue statement


The continue statement in C language is used to continue the execution of loop (while, do while and for). It is used with if condition within the loop.
In case of inner loops, it continues the control of inner loop only.

Syntax:

  1. jump-statement;  
  2. continue;  
The jump statement can be while, do while and for loop.

Example of continue statement in c

  1. #include <stdio.h>    
  2. #include <conio.h>    
  3. void main(){    
  4. int i=1;//initializing a local variable  
  5. clrscr();    
  6.   
  7. //starting a loop from 1 to 10  
  8. for(i=1;i<=10;i++){    
  9. if(i==5){//if value of i is equal to 5, it will continue the loop  
  10. continue;  
  11. }  
  12. printf("%d \n",i);  
  13. }//end of for loop  
  14.   
  15. getch();    
  16. }    

Output

1
2
4
3 6
9
7 8
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.
  1. #include <stdio.h>    
  2. #include <conio.h>    
  3. void main(){    
  4. int i=1,j=1;//initializing a local variable  
  5. clrscr();    
  6.   
  7. for(i=1;i<=3;i++){    
  8. for(j=1;j<=3;j++){  
  9. if(i==2 && j==2){  
  10. continue;//will continue loop of j only  
  11. }  
  12. printf("%d &d\n",i,j);  
  13. }  
  14. }//end of for loop  
  15.   
  16. getch();    
  17. }    

Output

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

No comments:

Post a Comment