YourCodingMentor

The continue statement in C is used to skip the remaining part of the current iteration of a loop and move to the next iteration. Unlike the break statement, which exits the loop entirely, the continue statement only skips the rest of the code in the current iteration and proceeds with the next iteration.

Syntax

continue;
  • continue can be used in loops such as for, while, or do...while.
  • It affects the loop control flow by skipping the rest of the loop body for the current iteration and then proceeding to the next iteration of the loop.

Example 1: Using continue in a for loop

In a for loop, the continue statement causes the loop to skip the current iteration and move to the next one.

#include <stdio.h>

int main() {
    for (int i = 1; i <= 10; i++) {
        if (i == 5) {
            continue;  // Skip the rest of the code when i equals 5
        }
        printf("i = %d\n", i);
    }

    return 0;
}

Output:

i = 1
i = 2
i = 3
i = 4
i = 6
i = 7
i = 8
i = 9
i = 10

In this example:

  • The loop runs from i = 1 to i = 10.
  • When i equals 5, the continue statement is executed, which skips the printf function for that iteration, and the loop moves to the next iteration (i = 6).

Example 2: Using continue in a while loop

The continue statement can also be used in a while loop to skip the rest of the current iteration and move to the next loop iteration.

#include <stdio.h>

int main() {
    int i = 1;

    while (i <= 10) {
        if (i == 5) {
            i++;  // Increment i to avoid an infinite loop
            continue;  // Skip the rest of the code when i equals 5
        }
        printf("i = %d\n", i);
        i++;
    }

    return 0;
}

Output:

i = 1
i = 2
i = 3
i = 4
i = 6
i = 7
i = 8
i = 9
i = 10

In this example:

  • The while loop runs while i is less than or equal to 10.
  • When i equals 5, the continue statement is executed, skipping the printf function for that iteration.
  • Note that i is incremented manually before the continue to avoid an infinite loop.

Example 3: Using continue in a do...while loop

You can also use continue in a do...while loop.

#include <stdio.h>

int main() {
    int i = 1;

    do {
        if (i == 5) {
            i++;  // Increment i to avoid an infinite loop
            continue;  // Skip the rest of the code when i equals 5
        }
        printf("i = %d\n", i);
        i++;
    } while (i <= 10);

    return 0;
}

Output:

i = 1
i = 2
i = 3
i = 4
i = 6
i = 7
i = 8
i = 9
i = 10

In this example:

  • The do...while loop runs at least once before checking the condition, and the continue statement is used to skip the printf function when i equals 5.
  • Like in the while loop example, i is incremented before the continue to avoid an infinite loop.

Example 4: Using continue with Multiple Conditions

You can use continue with more complex conditions, allowing you to skip the current iteration under multiple scenarios.

#include <stdio.h>

int main() {
    for (int i = 1; i <= 10; i++) {
        if (i == 3 || i == 7) {
            continue;  // Skip when i is 3 or 7
        }
        printf("i = %d\n", i);
    }

    return 0;
}

Output:

i = 1
i = 2
i = 4
i = 5
i = 6
i = 8
i = 9
i = 10

In this example:

  • The continue statement is executed when i is either 3 or 7, skipping the printf statement for these values.

Example 5: Using continue in Nested Loops

In nested loops, continue only affects the innermost loop where it is called. If you want to skip the outer loop’s iteration, you will need to add additional logic or use labels (although labels are generally avoided in good programming practice).

#include <stdio.h>

int main() {
    for (int i = 1; i <= 3; i++) {  // Outer loop
        for (int j = 1; j <= 3; j++) {  // Inner loop
            if (i == 2 && j == 2) {
                continue;  // Skip the iteration of the inner loop when i = 2 and j = 2
            }
            printf("i = %d, j = %d\n", i, j);
        }
    }

    return 0;
}

Output:

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

In this example:

  • When i equals 2 and j equals 2, the continue statement skips the printf function, but it only affects the inner loop. The outer loop continues as usual.

Summary

  • The continue statement skips the current iteration of a loop and proceeds to the next iteration.
  • It can be used in for, while, and do...while loops.
  • continue only affects the loop in which it is placed, skipping the remaining code for the current iteration and moving to the next iteration.
  • It is useful when certain conditions make it unnecessary to execute the rest of the loop body for specific iterations, allowing for more efficient control flow.

Leave a Reply

Your email address will not be published. Required fields are marked *