C continue
Statement
The C continue
statement is a control statement that allows developers to skip the current iteration of a loop and continue to the next one. This statement is typically used in for, while, and do-while loops.
Syntax
continue;
Example
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
printf("%d\n", i);
}
return 0;
}
Output
1
2
4
5
Explanation
In the above example, we have used the continue
statement inside a for loop to skip the current iteration when the value of i
is equal to 3
. When i
is equal to 3
, the continue statement is executed, and the loop skips the remaining statements in that iteration and continues with the next iteration.
Use
The continue
statement is typically used to skip specific iterations of a loop when a certain condition is met. It can be used to simplify complex logic and prevent unnecessary computations.
Important Points
- The
continue
statement can only be used inside loops. - When the
continue
statement is executed, the remaining statements in the current iteration of the loop are skipped, and the next iteration of the loop begins. - When the
continue
statement is executed in a nested loop, only the current iteration of the inner loop is affected.
Summary
In summary, the continue
statement is a useful control statement in C that allows developers to skip specific iterations of a loop based on certain conditions. It can be used to simplify complex logic and prevent unnecessary computations. Understanding the syntax and appropriate use cases for the continue
statement is an essential skill for any C programmer.