The continue statement in C is used to transfer control to the beginning of the loop. The continue statement skips some code inside the loop and continues with the next iteration. It is mainly used to skip certain code based on specific conditions.
Syntax:
// Loop statement continue;// Some code lines to skip
Example of continue Statement 1
#include <stdio.h>
void main () { int i = 0; while (i != 10) { printf("%d", i); continue; i++; }}
Output
Infinite Loop
Example of continue Statement 2
👇 Click to receive 👇
👉 Collection of C Language Knowledge Materials
#include <stdio.h>
int main() { int i = 1; // Initialize a local variable // Loop from 1 to 10 for (i = 1; i <= 10; i++) { if (i == 5) { // If the value of i equals 5, continue the loop continue; } printf("%d\n", i); } return 0;}
Output
1234678910
As you can see, 5 is not printed on the console because the loop is skipped when i equals 5.
C Continue Statement with Inner Loops
In this case, the C continue statement will only continue the inner loop and will not continue the outer loop.
#include <stdio.h>
int main() { int i = 1, j = 1; // Initialize local variables for (i = 1; i <= 3; i++) { for (j = 1; j <= 3; j++) { if (i == 2 && j == 2) { continue; // Only continues the j loop } printf("%d %d\n", i, j); } } return 0;}
Output
1 11 21 32 12 33 13 23 3
As you can see, 2 2 is not printed on the console because the inner loop is skipped when i equals 2 and j equals 2.
Programmer Technical Exchange Group
Scan the code to join the group and remember to note: city, nickname, and technical direction.