The C language is one of the easiest programming languages that exist in the world. In general, C is the first programming language of most programmers. The reason for the immense popularity of the C language is because of the presence of a wide range of features that it provides. One of those features is the support for the flow control statements like break and continue statements in C. Here in this article, we will discuss the continue Statement in C in detail with the help of examples.
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 considered a powerful tool for controlling the flow of the program as it allows us to skip the unwanted statements which are present in the loop body. In this article, we learned about the continue Statement in C and how to use them in the while, do-while, and for loops.
Frequently Asked Questions (FAQs)
Here are some Frequently Asked Questions on “continue Statement in C”.
Ques 1. Can multiple continue statements be used within a loop?
Ans. Yes, multiple continue statements can be used within a loop, depending on the specific needs of the program.
Ques 2. Can the continue statement be used in switch statements?
Ans. No, the continue statement cannot be used in switch statements.
Ques 3. How does the continue statement interact with nested loops?
Ans. When the continue statement is used within a nested loop, it only applies to the current loop and does not affect any outer loops.
Ques 4. Can the continue statement be used in recursive functions?
Ans. No, the continue statement cannot be used in recursive functions.
Ques 5. What are some common use cases for the continue statement?
Ans. The continue statement is often used in loops to skip over certain values or iterations that do not meet certain criteria, or to avoid performing certain actions under certain conditions.
Ques 6. Can the continue statement be used in all types of loops in C?
Ans. Yes, the continue statement can be used in all types of loops in C, including while loops, do-while loops, and for loops.