16. Know when to use the continue statement


The continue statement is somewhat the opposite of the break statement. It forces the next iteration of the loop to take place, skipping any code in between itself and the test condition of the loop. For example, this program never display any output: 

#include<stdio.h>
#include<conio.h>
void main()
{
int x;
  for(x=0;x<100;++x)
  {
  continue;
  printf("%d",x);    /* this is never executed */
  }
}

In this program when the continue statement is reached, it causes the loop to repeat, skipping the printf() statement. In while and do-while loops, a continue statement will cause control to go directly to the test condition and then continue the looping process. In the case of for, the increment part of the loop is performed,the conditional test is executed, and the loop continues.
   Frankly, continue is seldom used, not because it is poor practice to use it, but simply because good applications for it are not common.

(post by arnob)

No comments:

Post a Comment

free counters