Continue statement in C in C Language

In C programming, the continue statement is used to skip the current iteration of a loop and move to the next iteration. When a continue statement is encountered inside a loop, it skips the remaining code inside the loop for that particular iteration and proceeds with the next iteration of the loop.

Syntax of continue

continue;

Where continue is useful:

  • Filtering data in loops: When you want to skip certain elements based on specific conditions (e.g., skipping invalid input or certain values in an array).
  • Avoiding unnecessary computations: If certain conditions are met, skip the rest of the operations in that iteration (e.g., avoiding further checks when a condition is met early).
  • Improving performance: Reducing unnecessary processing in the loop, especially when complex calculations or checks are involved.

Key Points:

  • continue only affects the current iteration of the loop.
  • It does not terminate the loop (for that, you would use break).
  • It's typically used with an if statement to decide when to skip certain iterations.

Example:-

C program to skipping Even Numbers in a for Loop using continue statement

In C programming, you can use the continue statement to skip certain iterations in a loop. If you want to skip even numbers in a for loop, the continue statement can be used inside the loop to bypass the rest of the loop’s body whenever an even number is encountered.

How it Works:

  • The for loop runs from 1 to 10.
  • Inside the loop, there's an if statement that checks whether the current number i is even using the condition i % 2 == 0. The modulus operator (%) returns the remainder when i is divided by 2, and if the remainder is 0, the number is even.
  • If the number is even, the continue statement is executed. This causes the loop to skip the remaining part of the body and go directly to the next iteration, thus not printing the even numbers.
  • The odd numbers (when i % 2 != 0) are printed as they do not trigger the continue statement.
#include <stdio.h>

int main() {
    // Loop from 1 to 10
    for (int i = 1; i <= 10; i++) {
        // Check if the number is even
        if (i % 2 == 0) {
            continue;  // Skip the rest of the loop for even numbers
        }
        // Print odd numbers
        printf("%d\n", i);
    }
    
    return 0;
}

Output

1
3
5
7
9
Click here to download practice questions on Continue statement in C

List of Continue statement in C questions and answers

  1. C program to skipping Even Numbers in a for Loop using continue statement