C# continue statement
In this chapter you will learn:
- What is continue statement
- How to create continue statement
- Example for continue statement
- continue within if statement
Description
The continue statement forgoes the remaining statements in a loop and makes an early start on the next iteration.
Syntax
continue
statement has the following syntax.
continue;
Example
The following code only prints out 6,7,8,9,10.
using System;//w ww . jav a 2 s . co m
class Program
{
static void Main(string[] args)
{
for (int i = 0; i <= 10; i++)
{
if (i <= 5)
{
continue;
}
Console.WriteLine(i);
}
}
}
The output:
Example 2
The following code combines the modular operator and continue statement to print out odd numbers only.
using System;//ww w.j a v a 2s. c om
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 20; i++)
{
if (i % 2 == 0)
{
continue;
}
Console.WriteLine(i);
}
}
}
The output:
Next chapter...
What you will learn in the next chapter: