10.21.2011

continue statement

when we write program, if we want to take control to the beginning of the loop, bypassing the statements inside the loop, which have not yet been executed, in this situation we uses continue. continue is c reserved keyword in C. When continue is encountered inside any loop, control automatically passes to the beginning to the loop.
A continue is usually associated with an if.
Let's understand continue with example:

/*demonstration of continue statement*/
 #include<stdio.h>
 #include<conio.h>
 int main()
 {
  int i,j;
  for(i=1; i<=2; i++)
  {
   for(j=1; j<=2; j++)
   {
    if(i==j)
     continue;
    printf("\n%d %d",i,j);
   }
  }
  getch();
  return 0;
 }

Output:
 1 2
 2 1

Explanation: When the value of i equals that of j, the continue statement takes the control to the for loop(inner) bypassing the rest of the statements execution in the for loop(inner).

Let's understand continue statement with another example:

/*demonstration of continue statement*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int i;
 for(i=1; i<=10; i++)
 {
   if(i==6)
     continue;
   printf("%d ",i);
 }
 printf("\nNow we are out of for loop");
 getch();
 return 0;
}


Output:-

 1 2 3 4 5 7 8 9 10
 Now we are out of for loop

Explanation: As when the value of i will becomes 6 it will move for the next iteration by skipping the iteration i=6.

No comments:

Post a Comment

Post a Comment