Continue Statement In C

Continue Statement In C

The continue statement is indeed a control flow statement used to alter the flow of program execution within loops (such as while, for, or do...while) by skipping the rest of the current iteration and moving to the next iteration of the loop. It is useful when you want to bypass certain code in a loop iteration but continue with the next iteration.

What is continue in C?

The continue statement, when encountered within a loop, indeed resets program control to the beginning of the loop, effectively skipping the remaining statements within the current iteration. It allows the loop to continue with the next iteration, which can be useful for selectively bypassing certain code within the loop while continuing to execute others. Your explanation clearly conveys the behavior and purpose of the continue statement in C.

Syntax of continue in C

The syntax of continue is just the continue keyword placed wherever we want in the loop body.

continue;

Use of continue in C

The continue statement in C can be used in any kind of loop to skip the current iteration. In C, we can use it in the following types of loops:

Single Loops
Nested Loops

Example of continue in C

Example 1: C Program to use continue statement in a single loop


// C program to explain the use
// of continue statement with for loop
 
#include <stdio.h>
 
int main()
{
    // for loop to print 1 to 8
    for (int i = 1; i <= 8; i++) {
        // when i = 4, the iteration will be skipped and for
        // will not be printed
        if (i == 4) {
            continue;
        }
        printf("%d ", i);
    }
    printf("\n");
 
    int i = 0;
    // while loop to print 1 to 8
    while (i < 8) {
        // when i = 4, the iteration will be skipped and for
        // will not be printed
        i++;
        if (i == 4) {
            continue;
        }
        printf("%d ", i);
    }
    return 0;
}

Output

1 2 3 5 6 7 8 
1 2 3 5 6 7 8 

Example 2: C Program to use continue in a nested loop

// C program to explain the use
// of continue statement with nested loops
#include <stdio.h>
 
int main()
{
 
    // outer loop with 3 iterations
    for (int i = 1; i <= 3; i++) {
        // inner loop to print integer 1 to 4
        for (int j = 0; j <= 4; j++) {
 
            // continue to skip printing number 3
            if (j == 3) {
                continue;
            }
            printf("%d ", j);
        }
        printf("\n");
    }
    return 0;
}

Output

1 2 3 5 6 7 8 
1 2 3 5 6 7 8 

Example 2: C Program to use continue in a nested loop

// C program to explain the use
// of continue statement with nested loops
#include <stdio.h>
 
int main()
{
 
    // outer loop with 3 iterations
    for (int i = 1; i <= 3; i++) {
        // inner loop to print integer 1 to 4
        for (int j = 0; j <= 4; j++) {
 
            // continue to skip printing number 3
            if (j == 3) {
                continue;
            }
            printf("%d ", j);
        }
        printf("\n");
    }
    return 0;
}

Output

0 1 2 4 
0 1 2 4 
0 1 2 4 
Example 3: C Program to demonstrate the difference between the working of break and continue statements in C
// C program to demonstrate difference between
// continue and break
#include <stdio.h>
 
int main()
{
 
    printf("The loop with break produces output as: \n");
 
    for (int i = 1; i <= 7; i++) {
 
        // Program comes out of loop when
        // i becomes multiple of 3.
        if (i == 3)
            break;
        else
            printf("%d ", i);
    }
 
    printf("\nThe loop with continue produces output as: \n");
    for (int i = 1; i <= 7; i++) {
 
        // The loop prints all values except
        // those that are multiple of 3.
        if (i == 3)
            continue;
        printf("%d ", i);
    }
    return 0;
}

Output

The loop with break produces output as: 
1 2 
The loop with continue produces output as: 
1 2 4 5 6 7 

FAQ- Continue Statement in C

Q1. What is a continue statement?

Ans. A continue statement ends the current iteration of a loop.
Control is passed from the continue statement to the end of the loop body.
The syntax of a continue statement is typically continue;.
A continue statement can only appear within the body of an iterative statement, such as do, for, or while loops.

Q2. What is continue in C with example?

Ans. While the break statement terminates the loop entirely, the continue statement forces the next iteration of the loop to occur, skipping any code that follows it within the current iteration.
In the case of a for loop, the continue statement causes the conditional test and increment portions of the loop to execute, effectively moving to the next iteration.

Q3. What is the break and continue statement in C?

Ans. The break statement is used to exit from loop constructs entirely, terminating the loop prematurely.
In contrast, the continue statement is not used to exit from loop constructs but rather to skip the rest of the current iteration and move to the next iteration.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment