The continue statement is used inside loops to skip the remaining statements of the current iteration and move to the next iteration of the loop.
break, the continue
statement does not terminate the loop. It only skips the current iteration.
The continue statement skips the remaining part of the
current loop iteration.
continue;
After continue, execution proceeds with the next iteration
according to the rules of the loop.
continue;
The keyword continue is followed by a semicolon.
for (int i = 1; i <= 5; i++)
{
if (i == 3)
{
continue;
}
printf("%d ", i);
}
Output:
1 2 4 5
When i is 3, the continue statement skips the
printf() statement for that iteration.
for (int i = 1; i <= 10; i++)
{
if (i % 2 == 0)
{
continue;
}
printf("%d ", i);
}
Output:
1 3 5 7 9
The even numbers are skipped.
int i = 0;
while (i < 10)
{
i++;
if (i == 5)
{
continue;
}
printf("%d ", i);
}
Output:
1 2 3 4 6 7 8 9 10
The value 5 is skipped.
int i = 0;
do
{
i++;
if (i == 3)
{
continue;
}
printf("%d ", i);
}
while (i < 5);
Output:
1 2 4 5
The continue statement is often used inside an
if statement to decide which iterations should be skipped.
for (int i = 1; i <= 10; i++)
{
if (i < 5)
{
continue;
}
printf("%d ", i);
}
Output:
5 6 7 8 9 10
for (int i = 1; i <= 5; i++)
{
if (i == 3)
{
continue;
}
printf("Number: %d\n", i);
printf("Hello\n");
}
When i is 3, both printf() statements after
continue are skipped.
for (int i = 1; i <= 10; i++)
{
if (i % 2 != 0)
{
continue;
}
printf("%d ", i);
}
Output:
2 4 6 8 10
Odd numbers are skipped, so only even numbers are printed.
for (int i = 1; i <= 10; i++)
{
if (i % 2 == 0)
{
continue;
}
printf("%d ", i);
}
Output:
1 3 5 7 9
for (int i = 1; i <= 15; i++)
{
if (i % 3 == 0)
{
continue;
}
printf("%d ", i);
}
Output:
1 2 4 5 7 8 10 11 13 14
int numbers[] = {10, -5, 20, -8, 30};
for (int i = 0; i < 5; i++)
{
if (numbers[i] < 0)
{
continue;
}
printf("%d ", numbers[i]);
}
Output:
10 20 30
Negative values are skipped.
int numbers[] = {10, 0, 20, 0, 30};
for (int i = 0; i < 5; i++)
{
if (numbers[i] == 0)
{
continue;
}
printf("%d ", numbers[i]);
}
Output:
10 20 30
int number;
for (int i = 1; i <= 5; i++)
{
printf("Enter number: ");
scanf("%d", &number);
if (number < 0)
{
continue;
}
printf("Positive number = %d\n", number);
}
Negative values are ignored and the next iteration begins.
int sum = 0;
for (int i = 1; i <= 10; i++)
{
if (i % 2 != 0)
{
continue;
}
sum += i;
}
printf("Sum = %d", sum);
Output:
Sum = 30
Only even numbers are added to the sum.
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
continue;
}
printf("5 x %d = %d\n", i, 5 * i);
}
The multiplication for 5 is skipped.
char text[] = "HELLO";
for (int i = 0; text[i] != '\0'; i++)
{
if (text[i] == 'L')
{
continue;
}
printf("%c ", text[i]);
}
Output:
H E O
Both occurrences of L are skipped.
char text[] = "computer";
for (int i = 0; text[i] != '\0'; i++)
{
if (text[i] == 'o')
{
continue;
}
printf("%c", text[i]);
}
Output:
cmputer
The character o is skipped.
In a for loop, when continue is executed, control
moves to the loop's update expression before the next condition check.
for (int i = 1; i <= 5; i++)
{
if (i == 3)
{
continue;
}
printf("%d ", i);
}
After skipping 3, the i++ update still occurs.
In a while loop, you must be careful about updating the loop
variable before reaching continue.
int i = 0;
while (i < 5)
{
i++;
if (i == 3)
{
continue;
}
printf("%d ", i);
}
Output:
1 2 4 5
The update occurs before the continue, so the loop progresses.
| continue | break |
|---|---|
| Skips the current iteration. | Terminates the nearest loop or switch. |
| The loop continues. | The loop ends immediately. |
| Useful for skipping selected values. | Useful for stopping early. |
// continue
if (condition)
{
continue;
}
// break
if (condition)
{
break;
}
for (int i = 1; i <= 2; i++)
{
for (int j = 1; j <= 5; j++)
{
if (j == 3)
{
continue;
}
printf("%d ", j);
}
printf("\n");
}
The continue affects the nearest enclosing loop, so here it
skips the current iteration of the inner loop.
for (int i = 1; i <= 5; i++)
{
if (i == 3)
{
continue;
}
printf("%d ", i);
}
printf("\nLoop completed.");
Output:
1 2 4 5
Loop completed.
The loop continues after skipping 3.
A continue statement can accidentally create an infinite loop
if the loop variable is not updated before reaching it.
int i = 1;
while (i <= 5)
{
if (i == 3)
{
continue;
}
i++;
}
When i becomes 3, continue is executed and
i++ is skipped. The value remains 3, so the loop can continue
forever.
Move the update before the continue when necessary.
int i = 0;
while (i < 5)
{
i++;
if (i == 3)
{
continue;
}
printf("%d ", i);
}
Output:
1 2 4 5
int numbers[] = {10, -2, 20, -5, 30};
int size = 5;
for (int i = 0; i < size; i++)
{
if (numbers[i] < 0)
{
continue;
}
printf("Valid: %d\n", numbers[i]);
}
The program ignores negative values and processes only non-negative values.
#include <stdio.h>
int main()
{
int n;
printf("Enter a positive number: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
if (i % 2 != 0)
{
continue;
}
printf("%d ", i);
}
return 0;
}
This program skips odd numbers and prints only even numbers.
#include <stdio.h>
int main()
{
int number;
for (int i = 1; i <= 5; i++)
{
printf("Enter number %d: ", i);
scanf("%d", &number);
if (number <= 0)
{
continue;
}
printf("Valid positive number: %d\n", number);
}
return 0;
}
Invalid values are skipped, while positive values are processed.
#include <stdio.h>
int main()
{
int numbers[] = {10, -5, 20, 0, 30, -8};
int size = 6;
int sum = 0;
for (int i = 0; i < size; i++)
{
if (numbers[i] <= 0)
{
continue;
}
sum += numbers[i];
}
printf("Sum of positive numbers = %d", sum);
return 0;
}
The program skips zero and negative values and adds only positive values.
Practice the following programs:
continue inside a nested loop and observe its effect.
Practice carefully with both for and while loops,
especially when the update statement is involved.
continue statement skips the current iteration.if statement.for, while, and do-while loops.for loop, continue proceeds to the update expression.while loop, make sure the loop variable still gets updated.continue and break have different purposes.continue when you want the loop to keep running while skipping selected iterations.Question: What does the continue statement do inside a loop?