The C# jump statements are break
, continue
, goto
, return
, and throw
.
The continue statement skips the remaining statements in a loop and makes an early start on the next iteration.
The following loop skips even numbers:
for (int i = 0; i < 10; i++) {
if ((i % 2) == 0){
continue; // continue with next iteration
}
Console.Write (i + " ");
}
Output:
1 3 5 7 9
Syntax of C# Continue Statement:
The syntax for a continue
statement in C# is as follows:
continue;
C# Continue Statement Flow Chart:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
/* local variable definition */
int a = 10;
/* do loop execution */
do
{
if (a == 15)
{
/* skip the iteration */
a = a + 1;
continue;
}
Console.WriteLine("value of a: {0}", a);
a++;
}
while (a < 20);
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
}
}
Output:
When the above code is compiled and executed, it produces the following result:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
continue;
}
Console.WriteLine(i);
}
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
}
}
Output:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 4; i++)
{
if (i == 3)
continue;
Console.WriteLine("i value: {0}", i);
}
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
}
}
Output:
C# Continue Statement continues inner loop only if you use continue statement inside the inner loop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
if (i == 2 && j == 2)
{
continue;
}
Console.WriteLine(i + " " + j);
}
}
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
}
}
Output:
When the above code is compiled and executed, it produces the following result:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 4; i++)
{
if (i == 3)
continue;
Console.WriteLine("i value: {0}", i);
}
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
}
}
Output:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
int i = 0;
while (i < 4)
{
i++;
if (i == 2)
continue;
Console.WriteLine("i value: {0}", i);
}
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
}
}
Output:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
int i = 1;
do
{
Console.WriteLine("i value: {0}", i);
i++;
if (i == 2)
continue;
} while (i < 4);
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
}
}
Output: