Continue Statement in C
The continue statement is a control statement used in the C programming that allows you to skip the current iteration of a loop and jump to the subsequent iteration. This statement is principally used inside loops, such as for and while loops, to skip over or terminate certain code blocks based on a condition. This article will discuss the syntax, working, and some examples of the continue statement in C.
Syntax:
continue;
The continue statement in C is operational:
When the continue statement is used inside of a loop, the remaining statements in the current iteration are skipped, and the next iteration of the loop is started instead. This indicates that the control expression for the loop is once again evaluated, and if it is true, the loop moves on to the next iteration. The loop, however, ends if the expression is false.
Only loops like the for, while, and do-while loops and others that can be used inside of loops can use the continue statement. According to the program, it will cause a syntax error if used outside of a loop. Thus, the execution of an outer loop from an inner loop can be continued using the continue statement and labels.
To skip a specific loop iteration, the continue statement is typically used inside of a loop. The program switches to the next iteration of the loop when the continue command has been executed. This indicates that all statements in the loop's body that follow the continue statement will be skipped.
Any loop in C can have the current iteration skipped using the continue statement. We can use it in the following loop types in C:
- Single Loop
- Nested Loop
Flowchart:
Example:
Skip negative values by using a while loop in the continue statement.
#include <stdio.h>
int main() {
int i = -5;
while(i <= 5) {
// Using the continue statement to skip negative numbers
if(i < 0) {
i++;
continue;
}
// Printing the value of I if it is non-negative
printf("%d ", i);
i++;
}
return 0;
}
Output:
In this illustration, the values from -5 to 5 are iterated using a while loop. We are determining whether the value of i is negative within the loop. If so, we will skip the current iteration and move on to the next one using the continue command. Using the printf() method, we print the value of i if its value is not negative.
Print a pattern using a for loop:
#include <stdio.h>
int main() {
int m = 5;
int n = 5;
for(int i = 1; i <= m; i++) {
for(int j = 1; j <= n; j++) {
// Using the continue statement to skip Printing * if i is even
if(i % 2 == 0) {
continue;
}
//Printing * for odd values of i and j
printf("* ");
}
//Printing a new line after each row
printf("\n");
}
return 0;
}
Output:
program skipped printing the asterisks for even rows because of the continue statement, resulting in a 5x5 pattern of asterisks (*).
The statement continues within C:
A loop is a programming construct that enables a specific block of code to be run continuously up until a specific condition is met. To move on to the next iteration of the loop, you might want to skip over a particular iteration in some circumstances. The continue statement is used in this situation.
- Skipping when a loop's final set of values is reached: You can use the continue statement to skip those iterations and move on to the next positive number, for instance, if you are looping through a list of numbers and want to skip over all the negative values.
- Handling exceptions: If an exception occurs during an iteration of a loop, you might wish to skip that iteration and move on to the next one. This can be helpful for addressing errors or unforeseen circumstances that might happen inside of a loop
- Filtering undesired elements out: In some circumstances, you should exclude some elements from a list or array. The continue statement can ignore those items and process only the ones you want.
- Code optimization: You can speed up and improve the efficiency of your code by using the continue statement to skip through pointless iterations.
- The "continue" declaration can be an effective technique for managing a loop's progress and improving loop efficiency. It enables you to bypass pointless iterations and concentrate on the information or circumstances that are most crucial to your programme.