C - Continue Statement

Overview

continue skips to the next iteration of the nearest enclosing loop.

Learning Objectives

  • Use continue to skip work conditionally.

Prerequisites

Example

for (int i = 0; i < 5; i++) {
  if (i % 2 == 0) continue; // skip even
  printf("%d ", i);
}

Checks for Understanding

  1. Does continue exit the loop?
Show answer

No. It skips the remainder of the current iteration.

Expected Output

For the example that prints only odd numbers in [0,4]:

1 3 

Common Pitfalls

  • Using continue without updating loop variables can lead to infinite loops.
  • Be mindful of off-by-one errors when skipping iterations.

Exercises

  1. Print numbers from 1 to 20 skipping multiples of 3.
  2. Given an array, print only the positive values using continue to skip others.