C# Continue
Syntax
continue;
Example
Print all even numbers between 0 and 10, using the continue
statement.
for(int i=0; i<=10; i++)
{
if(i % 2 != 0)
{
continue;
}
Console.WriteLine(i);
}
Output
0
2
4
6
8
10
Explanation
The continue
statement is used to skip the current iteration of a loop. In the above example, the for
loop iterates through every number between 0 and 10. However, when the iteration variable i
is odd, we use the continue
statement to skip that iteration and move on to the next. As a result, only the even numbers are printed to the console.
Use
The continue
statement is typically used within a loop to skip certain iterations based on a condition. This can be useful when you want to selectively perform certain operations or skip certain values within a loop.
Important Points
- The
continue
statement is used to skip the current iteration of a loop. - When the
continue
statement is encountered, the loop immediately moves on to the next iteration. - The
continue
statement is typically used within a loop to selectively perform certain operations or skip certain values.
Summary
The continue
statement is a useful tool in the C# programming language, allowing you to skip certain iterations of a loop based on a condition. By using the continue
statement, you can selectively perform certain operations or skip certain values, making your code more efficient and easier to read.