The continue statement is a control flow statement in C that alters the execution of loops. It allows developers to skip the remaining source code in the current iterations of a loop and proceed to the next iteration. This can be particularly useful when conditions are met, allowing for more efficient and clear source code. This article will explore the continue statement in detail, including its syntax, usage, and how it works with practical examples.
What is the Continue statement in C?
The continue statement in C regulates loops. It enables the program to exit the current iterations and proceed to the next iteration. Before the break statement ends a loop, it skips over the rest of the source code in the current iteration and returns to the loop’s test expression condition.
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure
Syntax of Continue in C
The syntax of the continue statement is straightforward.
continue;
This statement can be used in loops to cause the control to pass immediately to the next iteration—simply, the continue statement for that iteration.
Let’s discuss the usage of continue statement in C language.
Skip Current Iteration: The continue statement skips the remaining source code within a loop’s current iteration.
Control Loop Flow: The continue statement helps control loop execution by bypassing specific iterations.
Conditional Skipping: We can continue with conditional statements to skip iterations based on certain conditions.
Efficiency: The continue statement improves the efficiency of your source code by avoiding the execution of unnecessary source code from the specific loop iterations.
Common Use Cases: The continue statement also handles the exception case where we want to exclude the specific values from the loop while processing.
Versatility: The continue statement can work with different loops, such as ‘for,’ ‘while,’ and ‘do-white’ loops.
Programming Logic: This tool is valuable for managing program logic and ensuring the source code executes as intended within loops.
Careful Usage: The continue statement must be used to avoid potential infinite loops or unintended logic errors.
Examples of Continue Statement in C
Let’s see the simple continue statement in C language.
# include<stdio.h>
int main(){
int number = 0 ;
while(number < 20){
number++ ;
if(number < 5){
continue ;
}
printf("Number = %dn",number) ;
}
return 0 ;
}
Output
Number = 5
Number = 6
Number = 7
Number = 8
Number = 9
Number = 10
Number = 11
Number = 12
Number = 13
Number = 14
Number = 15
Number = 16
Number = 17
Number = 18
Number = 19
Number = 20
Program 2
#include <stdio.h>
int main() {
printf("Odd numbers from 1 to 10:n");
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue;
}
printf("%d ", i);
}
printf("n");
return 0;
}
Output
Odd numbers from 1 to 10:
1 3 5 7 9
Program 3: The following program demonstrates the continue statement with Nested Loops.
#include <stdio.h>
int main (){
int i, j, k;
for(i = 1; i <= 4; i++){
for(j = 1; j <= 4; j++){
if (i == j)
continue;
for (k=1; k <= 4; k++){
if (k == j || k == i)
continue;
printf("%d %d %d n", i,j,k);
}
}
}
return 0;
}
Encounter “continue”: When a continue statement is encountered in a loo, it instantly bypasses the remaining code in the current iteration and proceeds to the next iteration.
Skips Code: All source code following continue in the current iteration is ignored.
Conditional: It is often used with conditional statements to skip specific iterations based on conditions, allowing fine-grained loop control.
Now, we understand the continue statement used to skip iterations in a loop. Let’s see how the flow diagram visualizes how to continue works in a loop.
Difference between Break and Continue Statements in C
It terminates the loop or switch statement completely
This skips the rest of the current loop iteration and moves to the next iteration
Effect on Loop
This exits the loop immediately no further iterations occur
It skips only the current iteration; the loop continues with the next iteration.
Usage
It can be used in for, while, do-white, and switch statements
It can be used for while and do-while loops (not in switch statements).
Loop Termination
The loop is terminated permanently
The loop continues executing unless a condition ends it.
Example
if(i ==3) {break ;}
if(i==3) {continue;}
Conclusion
The continue statement in C programming is used to skip the current iteration of a loop and proceed with the next iteration. When encountered inside loops like for, while, or do-white. It causes the program to bypass the remaining code within the loop for that specific iteration and immediately test its conditions to determine whether to continue with the next iteration. This is particularly useful when specific conditions must be avoided or bypassed without breaking out of the loop entirely. You can check out our Certificate Program in full-stack development with a web and mobile development specialization course to learn more about development.
FAQs
What is the purpose of the continue statement in C?
The continue statement skips the rest of the loop’s current iterations and moves on to the next one. It is often used to bypass specific conditions inside loops.
Can the continue statement be used in all types of loops in C?
Yes, the continue statement can be used in nested loops. However, it only affects the loop in which it is directly placed, skipping to the next iteration of that specific loop.
Is there a continue statement in other programming languages?
Yes, many programming languages, such as Java, Python, and C++, have a continue statement, and it works similarly to how it functions in C.
Is there a continue statement in C# or other modern languages?
Yes, C# and many other modern languages such as Python, Java, and JavaScript also include the continue statement, and its behavior is quite similar across these languages.
In which loops can the continue statement be used?
The continue statement can be used in all types of loops in C, including for, while, and do-white loops
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.