Tuesday, November 17, 2009

Using Continue

In Programming languages such as C/C++, JAVA, PHP, Python.. etc the keyword continue is a jump statement like break and goto.
This is used to skip inside an iteration.
An example for this is the following for loop statement that prints all even numbers from 0 - 10.

for(int num = 0;num <= 10; num++){
if ((num % 2) > 0)
continue;
printf("%d ", num);
}


The code is written in C Language (actually it is more like an algotithm)
so even JAVA programmers can easily translate them.
Another example, for more sophisticated programming, continue may be also used for deleting entries in file manipulation.

3 comments:

  1. actually, in C/C++... you can write the code this way

    for(int num = 0;num <= 10; num++){
    if (num % 2)
    continue;
    printf("%d ", num);
    }

    since (num % 2) returns a non-zero value

    ReplyDelete
  2. The continue statement works similar to the break statement. Instead of forcing termination, the continue statement forces the next iteration of the loop to take place skipping any code in between.

    ReplyDelete
  3. yes,,, although most programmers often neglect the use of continue

    ReplyDelete