Home  >  Article  >  Backend Development  >  Continue in C#

Continue in C#

WBOY
WBOYOriginal
2024-09-03 15:09:44784browse

Continue is one of the many conditional statements that can be used inside a conditional loop block in the C# programming language, which can function as a clause to continue the loop execution after the iterative condition is executed in order to move on to the execution of next iteration in the conditional loop. It is typically used along with the iterative conditional loops like a for-while loop, a do-while loop and a for-each loop.

How Continue statement works in C#?

In the below diagram, when the loop starts and if there is a continue statement, it will stop the current iteration and pass the control to the next iteration by going back to the beginning of the loop.

Flowchart

Below is the flow diagram of the continue statement showing how it is implemented.

Continue in C#

Below are the examples that show how it works with looping bodies like for, while, do-while, foreach, and inner loops:

Example #1

a. In the below example, for loop, is used. When the value of the variable is equal to 5, the continue statement will skip the current iteration and jumps to the next iteration to display the value.

using System;
using System.Collections.Generic;
using System. Linq;
using System. Text;
using System.Threading.Tasks;
namespace ContinueExample
{
class Demo
{
static void Main(string[] args)
{
for(int x=1; x<=6; x++ )  // loop runs six times
{
if (x == 5)  //  value is equal to 5
continue;     // skips the iteration
Console.WriteLine("value is :{0}", x);
}
Console.ReadLine();
}
}
}

Output:

Continue in C#

b. In the below example, when the value of the variable is less than 6, it will skip the iteration and jumps to the next iteration where the value is equal or greater than 6 and displays the values.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContinueExample
{
class Demo
{
static void Main(string[] args)
{
for(int x=1; x<=10; x++ )   // loop runs ten times
{
if (x < 6)     // values less than six
continue;      // skips the iteration
Console.WriteLine("value is :{0}", x);
}
Console.ReadLine();
}
}
}

Output:

Continue in C#

c. In the below example, the loop runs ten times and skips the iteration whenever the variable x is an odd number and passes the control to the next iteration and prints the value for variable x when it is even. This is how we can print even number series by using the continue statement.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContinueExample
{
class Demo
{
public static void Main(string[] args)
{
for (int x = 2; x <= 10; x++)   // loop runs ten times
{
if (x % 2 == 1)   // logic to print even number
{
continue;   // skips the iteration
}
Console.Write("{0} ", x);
}
Console.ReadLine();
}
}
}

Output: 

Continue in C#

Example #2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContinueExample
{
class Demo
{
static void Main(string[] args)
{
int x = 0;   // initializing variable
while(x < 7) // loop runs seven times
x++;   // incrementing the value of x
if(x==5)   // value is equal to 5
continue; // skips the iteration
Console.WriteLine("value is :{0}", x);
}
Console.ReadLine();
}
}
}

In the above example, while loop is used. A variable x is initialized. When the value of x is equal to 5, the continue statement is used to skip the iteration and display the other values.

Output: 

Continue in C#

Example #3

a. In the below example, do while looping statement is used. A variable x is initialized, and when the value of x is equal to 4, the continue statement stops the iteration and gives control to the next execution and displays the values.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContinueExample
{
class Demo
{
static void Main(string[] args)
{
int x = 1; // initializing variable
do
{
Console.WriteLine("value is :{0}", x);
x++;  // incrementing the value of x
if (x == 4)
continue;  //  skips the iteration
} while(x < 6) ;
Console.ReadLine();
}
}
}

Output:

Continue in C#

b. In the below example, while loop is used. A variable x is initialized. When the value of x is equal to 8, the continue statement is used to skip the iteration and display the other values.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContinueExample
{
class Demo
{
public static void Main(string[] args)
{
int x = 8;   // initializing variable
do
{
if (x == 13)
{
x = x + 1;
continue;   // skips the iteration
}
Console.WriteLine("a: {0}", x);
x++;      // incrementing the value of x
}
while (x < 15);
Console.ReadLine();
}
}
}

Output:

Continue in C#

Example #4

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContinueExample
{
class Demo
{
public static void Main(string[] args)
{
for (int x = 1; x <= 4; x++)                      // loops run four times
{
for (int y = 1; y <= 4; y++)
{
if (x == 3 && y == 3)
{
continue;                                     // skips the iteration
}
Console.WriteLine(x + " " + y);
}
Console.ReadLine();
}
}
}
}

In the above example, the continue statement is used inside the inner loops to skip the iteration based on the value of variables x and y.

Output:

Continue in C#

Example #5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContinueExample
{
class Demo
{
public static void Main(string[] args)
{
int[]arr = { 2, 4, 25, 34, 28, 57};          // initializing array
foreach (int a in arr)                       // iteration
{
if (a == 25)                              //Array element value equal to 25
{
continue;                            // skips the iteration
}
Console.WriteLine(a);
}
Console.ReadLine();
}
}
}

In the above example, foreach is used for iteration. An array of an element is initialized, which consists of six elements. When the variable is equal to 25, the continue statement will skip the iteration and passes the control to the next iteration and displays the values.

Output:

Continue in C#

Conclusion

This is how we can use the continue statement with different looping bodies like for, foreach, while, do-while, etc., and skips the iteration based on the condition. Mostly continue statement is used with for and foreach looping bodies.

The above is the detailed content of Continue in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Else If in C#Next article:Else If in C#