Table of Contents
What is the Continue Statement in C?
The continue statement is used inside loops (for, while, and do-while) to skip the current iteration and move directly to the next one.Instead of terminating the loop like break, it simply ignores the remaining code for that iteration.
Syntax:
Flow of Execution:continue;
- When continue is encountered, the loop does not stop.
- It skips the remaining statements inside the loop.
- Control jumps to the next iteration.
Example 1: Continue in a for Loop
Below is the C program to use the continue statement in a for loop:
// C program to use continue statement
// in a for loop
#include stdio.h
int main()
{
int i;
for(i = 1; i = 5; i++)
{
if(i == 3)
{
continue;
}
printf("%d\n", i);
}
return 0;
}
Output:
Explanation:1
2
4
5
When the value of i becomes 3, the continue statement is executed. This skips the printf statement for that iteration, so 3 is not printed.
Example 2: Continue in a while Loop
Below is the C program to use the continue statement in a while loop:
// C program to use continue statement
// in while loop
#include stdio.h
int main()
{
int i = 0;
while(i 5)
{
i++;
if(i == 2)
{
continue;
}
printf("%d\n", i);
}
return 0;
}
Output:
Explanation:1
3
4
5
When i equals 2, the loop skips printing and moves to the next iteration.
Example 3: Continue in a do-while Loop
Below is the C program to use the continue statement in a do-while loop:
// C program to use continue statement
// in a do-while loop
#include stdio.h
int main()
{
int i = 0;
do
{
i++;
if(i == 4)
{
continue;
}
printf("%d\n", i);
} while(i 5);
return 0;
}
Output:
Explanation:1
2
3
5
The value 4 is skipped because of the continue statement.
Continue Statement vs Break Statement
Below is the difference between the continue statement and the break statement:| Feature | Continue | Break |
|---|---|---|
| Function | Skips current iteration | Terminates the loop completely |
| Loop Execution | Continues next iteration | Stops the loop |
| Use Case | Ignore specific conditions | Exit loop early |
Advantages of Continue Statement in C
- Improves Code Readability: The continue statement helps avoid deeply nested if conditions, making the code easier to read and understand.
- Skips Unnecessary Operations: It allows the program to ignore unwanted conditions quickly without executing the remaining loop statements.
- Makes Loops More Efficient: By skipping unnecessary code execution, the continue statement can improve loop performance in some situations.
- Simplifies Conditional Logic: Programmers can handle special cases easily without writing extra blocks of code.
- Useful in Data Filtering: The continue statement is very useful when processing arrays, user input, or lists where some values need to be ignored.
Common Mistakes To Avoid
- Forgetting to Update Loop Variables: If loop variables are not updated before continue, the loop may run forever and create an infinite loop.
- Confusing Continue with Break: Many beginners think continue stops the loop completely, but it only skips the current iteration.
- Using Continue Excessively: Too many continue statements can make the program flow difficult to understand and debug.
- Placing Important Code After Continue: Any statement written after continue inside the same iteration will not execute, which may lead to logical errors.
- Using Continue Outside a Loop: The continue statement only works inside loops, and using it elsewhere causes a compilation error.
Conclusion
The continue statement in C gives you precise control over loop execution by skipping specific iterations without stopping the loop entirely. It is especially useful when handling conditions where certain values should be ignored. Once you understand how it works across different loops, writing efficient and clean code becomes much easier.Frequently Asked Questions
1. What is the purpose of the continue statement in C?2. Can continue be used outside loops?The continue statement is used to skip the current iteration of a loop and move to the next iteration.
3. What is the difference between break and continue?No, the continue statement can only be used inside loops like for, while, and do-while.
4. Does continue work in nested loops?Break exits the loop completely, while continue skips only the current iteration.
5. Can continue cause an infinite loop?Yes, but it only affects the loop in which it is used.
Yes, if loop variables are not updated properly before using continue.
0 Comments