CJump StatementsContinue Statement

Understanding the continue Statement in C: A Beginner’s Guide

The continue statement in C is used to skip the current iteration of a loop and move directly to the next iteration. Unlike the break statement, which terminates the loop entirely, the continue statement simply bypasses the remaining code in the current iteration while keeping the loop active.

In this blog, we will explore the continue statement with examples ranging from basic to complex, along with detailed explanations for absolute beginners.


1. What is the continue Statement?

The continue statement is primarily used when certain iterations of a loop need to be skipped based on a specific condition.

Syntax:

continue;
  • When the continue statement is executed, all code following it in the loop is ignored for that iteration.
  • The control goes directly to the next iteration.

2. Simple Example: Using continue in a for Loop

This program demonstrates how the continue statement works in a simple loop.

main.c
#include <stdio.h>
 
int main() {
    for (int i = 1; i <= 10; i++) {
        if (i == 5) {
            continue;  // Skip the iteration when i is 5
        }
        printf("%d ", i);
    }
 
    printf("\nLoop completed.\n");
    return 0;
}

Output:

1 2 3 4 6 7 8 9 10
Loop completed.

Explanation:

  1. The for loop starts at i = 1 and increments up to 10.
  2. When i == 5, the continue statement skips the rest of the code for that iteration and moves to the next value of i.
  3. As a result, 5 is not printed.

3. Practical Applications of continue

1. Skipping Even Numbers in a Loop

This program demonstrates skipping specific values using the continue statement.

main.c
#include <stdio.h>
 
int main() {
    printf("Odd numbers between 1 and 10:\n");
 
    for (int i = 1; i <= 10; i++) {
        if (i % 2 == 0) {
            continue;  // Skip even numbers
        }
        printf("%d ", i);
    }
 
    printf("\nDone!\n");
    return 0;
}

Output:

Odd numbers between 1 and 10:
1 3 5 7 9
Done!

Explanation:

  1. The if condition checks whether i is even (i % 2 == 0).
  2. If true, the continue statement skips printing i.
  3. This results in only odd numbers being printed.

2. Filtering Specific Characters in a String

This program demonstrates skipping specific characters when processing a string.

main.c
#include <stdio.h>
 
int main() {
    char str[] = "hello world";
    printf("String without vowels: ");
 
    for (int i = 0; str[i] != '\0'; i++) {
        if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') {
            continue;  // Skip vowels
        }
        printf("%c", str[i]);
    }
 
    printf("\n");
    return 0;
}

Output:

String without vowels: hll wrld

Explanation:

  1. The loop iterates through each character in the string.
  2. The if condition checks if the character is a vowel.
  3. If true, the continue statement skips printing the vowel.

3. Nested Loops with continue

When used in nested loops, the continue statement affects only the innermost loop.

Example: Skipping Values in the Inner Loop

main.c
#include <stdio.h>
 
int main() {
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 3; j++) {
            if (j == 2) {
                continue;  // Skip the iteration when j == 2
            }
            printf("i = %d, j = %d\n", i, j);
        }
    }
 
    return 0;
}

Output:

i = 1, j = 1
i = 1, j = 3
i = 2, j = 1
i = 2, j = 3
i = 3, j = 1
i = 3, j = 3

Explanation:

  • The continue statement skips the iteration in the inner loop (j loop) when j == 2.
  • The outer loop (i loop) remains unaffected.

4. Skipping Specific Rows in a Multiplication Table

This program skips rows where the multiplier is 5.

main.c
#include <stdio.h>
 
int main() {
    for (int i = 1; i <= 10; i++) {
        if (i == 5) {
            continue;  // Skip the 5th row
        }
 
        for (int j = 1; j <= 10; j++) {
            printf("%2d ", i * j);
        }
 
        printf("\n");
    }
 
    return 0;
}

Output (Partial):

 1  2  3  4  5  6  7  8  9 10
 2  4  6  8 10 12 14 16 18 20
 3  6  9 12 15 18 21 24 27 30
 4  8 12 16 20 24 28 32 36 40
 6 12 18 24 30 36 42 48 54 60
... (skipped row for 5)

4. Infinite Loops with continue

Using continue in an infinite loop allows selective skipping without terminating the loop.

main.c
#include <stdio.h>
 
int main() {
    int i = 0;
 
    while (1) {
        i++;
 
        if (i % 2 == 0) {
            continue;  // Skip even numbers
        }
 
        printf("%d ", i);
 
        if (i >= 10) {
            break;  // Exit the loop when i >= 10
        }
    }
 
    printf("\nLoop exited.\n");
    return 0;
}

Output:

1 3 5 7 9
Loop exited.

Explanation:

  1. The continue statement skips printing for even numbers.
  2. The break statement ensures the loop exits after i reaches 10.

5. Key Points to Remember

  1. The continue statement skips the remaining code in the current iteration and moves to the next iteration of the loop.
  2. It is especially useful for filtering data or skipping unwanted conditions.
  3. When used in nested loops, it only affects the innermost loop.
  4. Use continue cautiously to avoid making code harder to read.

Summary

The continue statement in C is a powerful tool for controlling the flow of loops. By understanding its behavior and applications, you can write cleaner and more efficient programs.

In the next blog, we’ll explore infinite loops and their practical applications in C. Stay tuned!