C# continue Statement
In C#, the continue
statement is used to skip the current iteration of a loop and move on to the next iteration. When the continue
statement is encountered, the program skips the remaining code in the current iteration of the loop and immediately starts the next iteration.
Here is an example that uses the continue
statement in a for
loop:
for (int i = 1; i <= 10; i++) { if (i % 2 == 0) { continue; } Console.WriteLine(i); }
In this example, the for
loop iterates over the values of i
from 1 to 10. However, when i
is an even number (i.e., when i % 2 == 0
), the continue
statement is executed, which skips the remaining code in the current iteration of the loop and immediately starts the next iteration. As a result, only the odd values of i
(i.e., 1, 3, 5, 7, and 9) are printed to the console.
Note that the continue
statement can also be used in a while
or do...while
loop to skip the remaining code in the current iteration and immediately start the next iteration.