Summary: in this tutorial, you’ll learn how to use the C# continue
statement to start a new iteration of an enclosing loop.
Introduction to the C# continue statement
The continue
statement skips the current loop iteration prematurely and immediately starts a new one. The continue
statement is only valid if you use it inside a loop, including while
, do while
, and for
loops.
The following shows the syntax of the continue
statement:
continue;
Code language: C# (cs)
Typically, you’ll use the continue
statement with the if
statement:
if (condition)
{
continue;
}
Code language: C# (cs)
The following shows how to use the continue
statement in a while
loop:
while (expression)
{
if (condition)
{
continue;
}
// statements
}
Code language: C# (cs)
In this syntax, if the condition is true
, the continue
statement will skip all the remaining statements underneath.
The following flowchart illustrates how the continue
statement works in a while
loop:
Likewise, you can use the continue
statement in a do while
statement:
do
{
if (condition)
{
continue;
}
// statements
} while (expression);
Code language: C# (cs)
The following flowchart illustrates how the continue
statement works in a do while
loop:
And for
loop statement:
for (initializer; loopCondition; iterator)
{
if(condition)
{
continue;
}
// statements
}
Code language: C# (cs)
If you use the continue
statement inside a nested loop, it’ll only skip the current iteration of the innermost loop.
The following flowchart shows how the continue
statement works in a for
loop:
C# continue statement examples
Let’s take some examples of using the continue
statement.
1) Using the C# continue statement in a for loop
The following example uses the continue
statement in a for
loop to print out only the odd numbers:
for (int i = 0; i < 10; i++)
{
if (i % 2 == 0)
{
continue;
}
Console.WriteLine($"{i}");
}
Code language: C# (cs)
Output:
1
3
5
7
9
Code language: C# (cs)
How it works.
The for
loop starts from 0 to 9.
To check if a number is even, you compare the remainder of that number after dividing it by two. If the remainder is zero, the number is an even number. Otherwise, it’s an odd number.
The following expression is true if i
is 0, 2, 4, 6, or 8:
i % 2 == 0
Code language: C# (cs)
The if
and continue
statements skip the statement that outputs the even number:
if (i % 2 == 0)
{
continue;
}
Code language: C# (cs)
Therefore, you see only the odd numbers in the output.
2) Using the continue statement in a while loop example
The following example prompts you to input a positive number and calculates its square root by using the Math.Sqrt()
method.
If you enter a negative number, the program issues a validation message and prompts you to enter a positive number. Otherwise, it’ll calculate the square root of the entered number and output it to the console.
If you enter the letter Q
or q
, the loop will exit due to the break
statement.
while (true)
{
Console.Write("Enter a positive number to calculate the square root (Q or q to exit):");
string input = Console.ReadLine();
// exit the loop if the input string is Q or q
if (input == "Q" || input == "q")
{
break;
}
// convert the input string to a double
double number = Convert.ToDouble(input);
// start the new iteration if the number is negative
if(number < 0)
{
Console.WriteLine("Please enter a positive number.");
continue;
}
Console.WriteLine($"The square root of {Math.Sqrt(number): 0.##}");
}
Code language: C# (cs)
In this example, we use a while
loop with the condition that is always true
.
Here’s how it works inside the loop:
First, prompt users to enter a positive number or letter q or Q to exit:
Console.Write("Enter a positive number to calculate the square root (Q or q to exit):");
string input = Console.ReadLine();
Code language: C# (cs)
Next, terminate the loop if the input string is Q or q using the break
statement:
if (input == "Q" || input == "q")
{
break;
}
Code language: C# (cs)
Then, convert the input string into a number:
double number = Convert.ToDouble(input);
Code language: C# (cs)
After that, start a new iteration using the continue
statement if the number is negative:
if (number < 0)
{
Console.WriteLine("Please enter a positive number.");
continue;
}
Code language: C# (cs)
Finally, calculate the square root of the number if it is zero or positive:
Console.WriteLine($"The square root of {Math.Sqrt(number): 0.##}");
Code language: C# (cs)
Summary
- Use the
continue
statement to start a new iteration prematurely and skip the remaining code in a loop immediately.