Continue in C#
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.
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:
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:
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:
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:
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:
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:
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:
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:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

There are several ways to modify XML formats: manually editing with a text editor such as Notepad; automatically formatting with online or desktop XML formatting tools such as XMLbeautifier; define conversion rules using XML conversion tools such as XSLT; or parse and operate using programming languages such as Python. Be careful when modifying and back up the original files.
