Home Basics C# continue keyword

C# continue keyword

The continue keyword transfers program control just before the end of a loop.

The condition for the loop is then checked, and if it is met, the loop performs another iteration.

Syntax

 

jump-statement;    
continue;

 

For loop Example

Here is a continue statement used inside a for loop

using System;

public class Program
{
	public static void Main()
	{
       for(int i=1;i<=10;i++)
	   {    
            if(i==5)
			{    
                continue;    
            }    
            Console.WriteLine(i);    
        }   
	}
}

This is the output you will see, you will notice that the number 5 is skipped and not outputted to the console and t he loop keeps running until it completes

1
2
3
4
6
7
8
9
10

While loop example

 

using System;

public class Program
{
	public static void Main()
	{
		int i = 0;
		while (i < 10)
		{
			i++;
			if (i == 5)
				continue;
			Console.WriteLine(i);
		}
		Console.WriteLine("Press Enter Key to Exit..");
		Console.ReadLine();
	}
}

 

You may also like