C# continue Keyword - Apply the continue keyword in a loop to end the current loop iteration.
While
Sleep
Part 1 We increment the number “value” which starts at 0, and is incremented on its first iteration (so 0 is never printed).
Part 2 If the number is equal to 3, the rest of the iteration is terminated. Control moves to the next iteration.
C# program that uses continue keyword
using System;
using System.Threading;
class Program {
static void Main()
{
int value = 0;
while (true)
{
// Part 1: increment number.
value++;
// Part 2: if number is 3, skip the rest of this loop iteration.
if (value == 3)
{
continue;
}
Console.WriteLine(value);
// pause.
Thread.Sleep(100);
}
}
}
1
2
4
5…
No enclosing loop. We must use a continue statement within a loop. We get an error from the compiler if no enclosing loop is found. The same restriction applies to break.
Break
C# program that causes continue error
class Program {
static void Main()
{
continue;
}
}
Error CS0139