Node:Speeding loops with continue, Previous:Terminating loops with return, Up:Terminating and speeding loops



Speeding loops with continue

Instead of terminating a loop, you might want to speed it to its next pass, perhaps to avoid executing irrelevant statements. To do so, you should use the continue statement. When a continue statement is encountered, the program will skip the rest of the loop's code block and jump straight to the start of the next pass through the loop.

Here is an example that uses the continue statement to avoid division by zero (which causes a run-time error):

for (my_int = -10; my_int <= 10; my_int++)
{
  if (my_int == 0)
  {
    continue;
  }

  printf ("%d", 20/i);
}