Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Continue Statement in C

Last Updated on December 29, 2023 by Ankit Kochar

In the realm of programming languages, the continue statement plays a pivotal role in controlling the flow of execution within loops. Specifically associated with the C programming language, the continue statement allows for the skipping of the remaining code within a loop’s body for the current iteration and proceeds with the next iteration. This construct is instrumental in enhancing the flexibility and efficiency of loop structures, providing programmers with a means to tailor the execution of their code to meet specific conditions.

What is the Continue Statement in C?

Continue Statement is the flow control statement which is used to bring the control of the program to the start of the loop. The continue Statement skips the statements of the loop and shifts the program control to the next iteration of the loop.

The continue Statement in C can be used with the following:

  • while Loop
  • do-while Loop
  • for Loop

The continue Statement in C cannot be used with the switch case.

Syntax of Continue Statement in C

The continue Statement in C is defined with the use of the keyword “continue”. Here is the syntax of the continue Statement in C.

//loop statement
continue;
// statements inside loop (these will be skipped)

Working of Continue Statement in C

Let us understand the working is the continue Statement in C with the following illustration depicting a simple scenario.

In this illustration, the loop body starts and executes statements present in the body before the “if” statement. As soon as it reaches the “if” statement, if the condition specified in this statement is false, it will move forward but if this is evaluated to be true, it executes the “continue” Statement in C, which is present in the “if block”. The continue Statement shifts the control of the program to the start of the loop and the rest of the statements in the loop will be skipped.

Flowchart of Continue Statement in C

The above illustration can be better understood by a flowchart. The flowchart for the above illustration is given below.

Examples of Continue Statement in C

Here are some examples of how to use the continue Statement in C.

Example 1 of Continue Statement in C: In while loop
This example shows the usage of the continue Statement in C inside of a while loop.

#include<stdio.h>
 
int main () {
   int i = 0;
 
    while(i<20){
        i++;
        if(i%2==0){
            continue;
        }
        printf("%d\n", i);
    }
 
   return 0;
}

Output:

1
3
5
7
9
11
13
15
17
19

Explanation:
The above example prints the odd number between 1 to 20 using a while. In the code, we have used an if statement for checking number is odd or not. If the number is even, we have used the continue statement in C which will skip the next statements and shifts the control of the program to the next iteration of the loop.

Example 2 of Continue Statement in C: In do-while Loop
The continue Statement in C can also be used with the do-while loops for skipping some unwanted statements. This example demonstrates the use of a continue Statement in C inside of a do-while loop.

#include <stdio.h>
 
int main(void) {
    int i = 0;
 
    do {
       i++;
       if(i == 5) {
          continue;
       }
       printf("%d\n", i);
    } while(i < 10);
 
    return 0;
}

Output:

1
2
3
4
6
7
8
9
10

Explanation:
In the above code example, we have used a do-while loop for printing the values from 1 to 10 except 5. Inside the loop, we have used an if statement to check whether the current number equals 5 or not. If the answer is no, the program executes further statements and prints the number but if the current number is equal to 5, it will encounter the “continue” statement which will skip the following statement in that iteration and move the control of program to the next iteration.

Example 3 of Continue Statement in C: In for Loop
Here is the code that is using the continue Statement in C inside a for loop.

#include <stdio.h>
 
int main(void) {
    // your code goes here
    for(int i = 3; i < 8; i++) {
       if(i == 5) {
          continue;
       }
       printf("%d ", i);
    }
    return 0;
}

Output:

3 4 6 7

Explanation:
This program prints the numbers from 3 to 8 except 5 with the use of the for loop in C. Inside the loop, we have an if statement that checks if the value of the variable i is equal to 5. If the value is not equal to 5, it will print the number on the screen. But if the number is equal to 5, it will move the control of the program to the Next iteration of the for loop and will skip the statements of the current iteration.

Conclusion
The continue statement in C is a valuable tool that contributes to the elegance and efficiency of loop structures. Its ability to selectively skip over certain iterations based on conditional logic empowers programmers to create more streamlined and customized solutions. By understanding how and when to use the continue statement, developers can optimize their code, making it not only more readable but also more responsive to the specific requirements of their algorithms and applications.

Frequently Asked Questions (FAQs) related to Continue Statement in C

Here are some Frequently Asked Questions on “continue Statement in C”.

1. What is the purpose of the continue statement in C?
The continue statement in C is used within loops to skip the remaining code within the loop’s body for the current iteration and proceed directly to the next iteration. It allows programmers to selectively bypass certain iterations based on specified conditions.

2. How does the continue statement differ from the break statement?
While both continue and break are loop control statements in C, they serve different purposes. The continue statement skips the remaining code in the current iteration and moves to the next iteration, while the break statement terminates the loop prematurely, exiting it altogether.

3. When should I use the continue statement?
The continue statement is useful when there is a need to skip specific iterations of a loop based on certain conditions. It is often employed to optimize the execution of loops by avoiding unnecessary code execution in certain scenarios.

4. Can the continue statement be used in any type of loop in C?
Yes, the continue statement is applicable to all types of loops in C, including for, while, and do-while loops. It provides a consistent mechanism for controlling the flow of execution within these loop structures.

5. Are there any best practices for using the continue statement?
It is generally advisable to use the continue statement judiciously to enhance code readability and maintainability. Excessive use of continue may lead to complex and less understandable code, so it’s essential to strike a balance and employ it where it genuinely improves code structure and efficiency.

Leave a Reply

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