Continue Statement in C

Last Updated : 20 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The continue statement in C is a jump statement that is used to bring the program control to the start of the loop. We can use the continue statement in the while loop, for loop, or do..while loop to alter the normal flow of the program execution. Unlike break, it cannot be used with a C switch case.

What is continue in C?

The C continue statement resets program control to the beginning of the loop when encountered. As a result, the current iteration of the loop gets skipped and the control moves on to the next iteration. Statements after the continue statement in the loop are not executed.

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

Using continue in infinite loops is not useful as skipping the current iteration won’t make a difference when the number of iterations is infinite.

Example of continue in C

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

The continue statement can be used in for loop, while loop, and do-while loop.

C




// 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

The continue statement will only work in a single loop at a time. So in the case of nested loops, we can use the continue statement to skip the current iteration of the inner loop when using nested loops.

C




// 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 

The continue skips the current iteration of the inner loop when it executes in the above program. As a result, the program is controlled by the inner loop update expression. In this way, 3 is never displayed in the output.

How continue statement works?

working of continue in c

Working of C continue in for Loop

The working of the continue statement is as follows:

  • STEP 1: The loop’s execution starts after the loop condition is evaluated to be true.
  • STEP 2: The condition of the continue statement will be evaluated.
  • STEP 3A: If the condition is false, the normal execution will continue.
  • STEP 3B: If the condition is true, the program control will jump to the start of the loop and all the statements below the continue will be skipped.
  • STEP 4: Steps 1 to 4 will be repeated till the end of the loop.

Flowchart of continue in C

flowchart of continue in c

Flowchart of the continue Statement in C

C break and continue Statement Differences

break statement: By using break statement, we terminate the smallest enclosing loop (e.g, a while, do-while, for, or switch statement).

continue statement: By using the continue statement, the loop statement is skipped and the next iteration takes place instead of the one prior.

Example: C Program to demonstrate the difference between the working of break and continue statement in C.

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 

Explanation: In the above program, the first loop will print the value of i till 3 and will break the loop as we have used a break statement at i equal to 3. And in the second for loop program will continue but will not print the value of i when i will be equal to 3.

Conclusion

In this article, we have discussed continue statement which is one of the four jump statements in C. We also studied its syntax, working, and how we can use it to alter the normal flow of out C program.

FAQs on C continue statement

1. What is the use of continue statement in C?

The continue statement in C is used in loops to skip the current iteration and move on to the next iteration without executing the statements below the continue in the loop body.

2. What type of statements are break and continue?

The break and continue in C are jump statements that are used to alter the flow of the normal execution of the loops.



Previous Article
Next Article

Similar Reads

Print "Even" or "Odd" without using conditional statement
Write a program that accepts a number from the user and prints "Even" if the entered number is even and prints "Odd" if the number is odd. You are not allowed to use any comparison (==, &lt;,&gt;,...etc) or conditional statements (if, else, switch, ternary operator,. Etc). Method 1 Below is a tricky code can be used to print "Even" or "Odd" accordi
4 min read
Return Statement vs Exit() in main() in C++
The return statement in C++ is a keyword used to return the program control from the called function to the calling function. On the other hand, the exit() function in C is a standard library function of &lt;stdlib.h&gt; that is used to terminate the process explicitly. The operation of the two may look different but in the case of the main() funct
3 min read
C++ Break Statement
The break in C++ is a loop control statement that is used to terminate the loop. As soon as the break statement is encountered from within a loop, the loop iterations stop there and control returns from the loop immediately to the first statement after the loop. Syntax: break; Basically, break statements are used in situations when we are not sure
5 min read
goto Statement in C
The C goto statement is a jump statement which is sometimes also referred to as an unconditional jump statement. The goto statement can be used to jump from anywhere to anywhere within a function. Syntax: Syntax1 | Syntax2 ---------------------------- goto label; | label: . | . . | . . | . label: | goto label; In the above syntax, the first line te
3 min read
Break Statement in C
The break statement is one of the four jump statements in the C language. The purpose of the break statement in C is for unconditional exit from the loop What is break in C? The break in C is a loop control statement that breaks out of the loop when encountered. It can be used inside loops or switch statements to bring the control out of the block.
6 min read
C if...else Statement
The if-else statement in C is a flow control statement used for decision-making in the C program. It is one of the core concepts of C programming. It is an extension of the if in C that includes an else block along with the already existing if block. C if Statement The if statement in C is used to execute a block of code based on a specified condit
6 min read
Return Statement in C
Pre-requisite: Functions in C C return statement ends the execution of a function and returns the control to the function from where it was called. The return statement may or may not return a value depending upon the return type of the function. For example, int returns an integer value, void returns nothing, etc. In C, we can only return a single
4 min read
C - if Statement
The if in C is the most simple decision-making statement. It consists of the test condition and if block or body. If the given condition is true only then the if block will be executed. What is if in C? The if in C is a decision-making statement that is used to execute a block of code based on the value of the given expression. It is one of the cor
5 min read
Data type of case labels of switch statement in C++?
In C++ switch statement, the expression of each case label must be an integer constant expression. For example, the following program fails in compilation. C/C++ Code /* Using non-const in case label */ #include&lt;stdio.h&gt; int main() { int i = 10; int c = 10; switch(c) { case i: // not a &quot;const int&quot; expression printf(&quot;Value of c
2 min read
Interesting facts about switch statement in C
Prerequisite - Switch Statement in C Switch is a control statement that allows a value to change control of execution. C/C++ Code // Following is a simple program to demonstrate syntax of switch. #include &amp;lt;stdio.h&amp;gt; int main() { int x = 2; switch (x) { case 1: printf(&amp;quot;Choice is 1&amp;quot;); break; case 2: printf(&amp;quot;Cho
3 min read
Article Tags :
three90RightbarBannerImg