Summary: in this tutorial, you will learn how to use the C continue
statement to skip the current iteration of a loop including for
loop, while
loop, and do...while
loop.
Introduction to the C continue statement
So far, you have learned how to execute a code block repeatedly based on a condition using the for loop, while loop, and do while loop statements. These loop statements execute a code block repeatedly a fixed number of times or as long as a condition is met.
Sometimes, it’s convenient to skip the current iteration of a loop without testing the condition.
In the previous tutorial, you learned how to use the break
statement to exit a loop early. In this tutorial, you’ll learn how to use the continue
statement to skip the current iteration and start a new one from the top of the loop.
The continue
statement skips the rest of the current iteration in a loop and returns to the top of the loop. The continue
statement works like a shortcut to the end of the loop body.
To continue statement includes the continue
keyword with a semicolon (;
):
continue;
Code language: JavaScript (javascript)
In practice, you often use the continue
statement with an if
statement to specify a condition for skipping the current iteration:
// somewhere in a loop
if (expression)
continue;
Code language: JavaScript (javascript)
Using the C continue statement in a for loop
The following shows how to use the continue
statement in a for
loop:
for (/*...*/)
{
//-- other statements
if (condition)
continue;
// other statements
}
Code language: JavaScript (javascript)
When the program encounters the continue
statement, it skips all the statements that follow the continue
statement to the end of the loop body and starts a new iteration from the beginning of the loop.
The following example uses the continue
statement in a for
loop to display only odd numbers from 1 to 10:
#include <stdio.h>
int main()
{
int i;
for (i = 0; i < 10; i++)
{
if (i % 2 == 0)
continue;
printf("%d ", i);
}
return 0;
}
Code language: PHP (php)
Output:
1 3 5 7 9
The for
loop runs 10 times. If the current number is an even number, the condition i % 2 == 0
becomes true
. The program encounters the continue
statement.
Since the continue
statement skips the current iteration and starts a new one, it skips the printf("%d ", i)
statement. As the result, the loop doesn’t display even numbers.
Using the C continue statement in a while loop
The following example uses the continue statement in a while loop:
while (expression)
{
//-- other statements
if (condition)
continue;
// other statements
}
Code language: JavaScript (javascript)
The following example uses the continue
statement in a while
loop to calculate the square root of an input number:
#include <stdio.h>
#include <math.h>
int main()
{
double number;
while (1)
{
// prompt for a positive number
printf("\nEnter a positive number to calcualte the square root (0 to exit):");
scanf("%lf", &number);
// exit the loop
if (number == 0)
break;
// start the new iteration
if (number < 0)
continue;
printf("The square root of %.2lf is %.2lf", number, sqrt(number));
}
return 0;
}
Code language: PHP (php)
The program repeatedly prompts for a positive number and calculates the square root of the entered number. To calculate a square root of a number, the program uses the sqrt()
function from the math.h
library.
If the input number is zero, the loop exits because of the break
statement:
if (number == 0)
break;
Code language: JavaScript (javascript)
If the input number is less than zero, the following statement starts the new iteration from the top of the loop:
if (number < 0)
continue;
Code language: JavaScript (javascript)
If the input number is positive, the followning statement shows the result:
printf("The square root of %.2lf is %.2lf", number, sqrt(number));
Code language: CSS (css)
Using the C continue statement in a do…while loop
Here’s the syntax for using the continue stament in a do...while
loop:
do
{
//-- other statements
if (condition)
continue;
// other statements
} while (expression);
Code language: JavaScript (javascript)
Then following example uses the continue
statement in a do...while
loop to display the even numbers from 1 to 10:
#include <stdio.h>
int main()
{
int n = 0;
do
{
n++;
// skip the odd number
if (n % 2 != 0)
continue;
printf("%d ", n);
} while (n < 10);
return 0;
}
Code language: PHP (php)
Output:
2 4 6 8 10
How it works.
- First, declare the n variable and initialize it to zero.
- Second, add 1 to n at the beginning of the loop.
- Third, start a new iteration if n is an even number. Otherwise, display the value of n.
Repeat the second step as long as n is less than 10.
Summary
- Use the C
continue
statement to skip the current iteration and start a new one from the beginning of the loop.