In C programming, the continue keyword is used within loops to skip the rest of the code inside the loop for the current iteration and proceed to the next iteration of the loop.
It is typically used in loops like for, while, and do-while.
Skipping Code: When the `continue` statement is placed inside a loop, the remaining code inside the loop's body is skipped, and the control is transferred to the next iteration of the loop.
Loop Control: It allows us to control the execution flow within loops, especially when certain conditions are met.
Selective Skipping: continue allows us to selectively skip certain iterations of a loop based on specific conditions.
Efficiency: It can lead to more efficient code execution by avoiding unnecessary computation or processing within a loop.
#include <stdio.h> int main() { int i; // Print even numbers between 1 and 10 for (i = 1; i <= 10; i++) { if (i % 2 != 0) { // Skip odd numbers continue; } printf("%d\n", i); } return 0; }
2 4 6 8 10
when "I" is an odd number, the "continue" statement is executed, skipping the "printf" statement, and the loop proceeds to the next iteration.
As a result, only even numbers between 1 and 10 are printed.
#include <stdio.h> int main() { int i; // Print multiples of 3 from 1 to 20, skipping those divisible by 2 for (i = 1; i <= 20; i++) { if (i % 3 != 0) { continue; // Skip numbers that are not multiples of 3 } if (i % 2 == 0) { continue; // Skip multiples of 2 } printf("%d\n", i); } return 0; }
3 9 15
#include <stdio.h> int main() { int i = 1; // Print even numbers between 1 and 10 while (i <= 10) { i++; if (i % 2 != 0) { // Skip odd numbers continue; } printf("%d\n", i); } return 0; }
2 4 6 8 10