Continue Statement in C Language


The continue statement in C is used to skip the current iteration of a loop and continue with the next iteration. It allows for more control over the flow of loops, enabling you to skip specific cases without terminating the entire loop.

1. Using Continue in for Loops

When used inside a for loop, the continue statement skips the remaining code inside the loop for the current iteration and jumps to the next iteration of the loop.

In this example, the loop iterates from 0 to 9, but the continue statement skips printing even numbers. The output will be:


    Odd number: 1
    Odd number: 3
    Odd number: 5
    Odd number: 7
    Odd number: 9
        

2. Using Continue in while Loops

The continue statement can also be used in while loops to achieve similar functionality.

In this case, the loop increments i first, and then skips the even numbers, producing the same output as before.

3. Using Continue in do while Loops

The continue statement works with do while loops in the same way, allowing the loop to skip to the next iteration based on a condition.

This example will also yield the same output, demonstrating the versatility of the continue statement across different loop types.

4. Nested Loops with Continue

When using continue in nested loops, it only affects the innermost loop. The outer loop will continue its normal execution.

In this example, when j equals 1, the inner loop skips that iteration, leading to the output:


    i = 0, j = 0
    i = 0, j = 2
    i = 1, j = 0
    i = 1, j = 2
    i = 2, j = 0
    i = 2, j = 2
        

Conclusion

The continue statement is a useful feature in C programming that allows for the efficient handling of loop iterations. By skipping certain iterations based on specific conditions, programmers can create more streamlined and readable code.






Advertisement