In the article, we will get the concept of the continue statement and how it works in the C# programming language.
The “continue” is the symbol for it. The continue statement is identical to the break statement in functionality. The sole difference is that the break statement ends the loop, but the continue statement directs action to the conditional test, which checks the condition. The continue statement advances the encapsulating statement to the next iteration in the while, do, for, or foreach loop.
How to Use the Continue Statement in C#
To understand the working of the continue statement and its usage in C#, we have some simple examples below:
Example1: Using Continue Statement with For Loop
We have a c# programming language example below of utilizing a continue statement with for loop.
We have to import the system library first in the C# program that will allow us to access its function and method required in the program. After that, the namespace is given a name as “continue”. In the namespace block, we created the class which is represented as “Example”. The class has a main function in which we used a for-loop. We set the variable “i” to “0” in the for loop. The condition is set for the variable “i” that “i” should be greater than and equal to the value “4”.
If the condition becomes true, the for loop is executed. Otherwise, on a false condition, the for loop is terminated. The value initialized variable “i” will increment and check the specified condition. Then in the for loop body, we have a conditional statement that “i” is equal to “2” which will not print because we used a continue statement that will be executed. The writeLine will print the values of “i” from 0 to 4.
Note that the value 2 is not executed as continue statement is used. The iteration on “2” is skipped here. The output is presented in the appended image.
Example2: Using Continue Statement with While Loop
The program below demonstrates how to use the continue statement in a while loop. We can immediately return control to the next iteration of the while loop in C# by preventing subsequent statements from being executed at the particular point.
We imported the system library “using System” of C# which is required for the C# program. The namespace is given the name “ContinueWhileLoop” and in the namespace region we have a class defined and named that class “Program”. The class has a main function with a static keyword. In the void main function, we have a variable declared as “a” of integer data type and initialized with the numeric value “1”.
Then a while loop is used here in which we have a condition that variable “a” should be greater than the numeric value “8”. The variable “a” is incremented till the condition is executed. The while loop body has a condition that should be equal to the value “4”. The continue statement skips the next iteration of the while loop and transfers control to the next iteration of the while loop when the value of “a” equals “4”. The value “4” will not be printed because the continue statement will execute. The writeLine method will print the value of “a” from 1 to 8 except for the value “4”.
Upon execution of the above while loop of the continue statement, the output we get is shown below.
Example3: Using Continue Statement with Nested Loop
The continue statement merely skips the current inner loop iteration. The outer loop is not altered in any way in the Nested loop. Let’s execute the program to see the working of the continue statement in the inner loop.
The program starts by importing the system library “using the system” of C#. The namespace keyword is used which organizes the class of the C# program and we named the namespace “ContinueNestedLoop”.Then in the block of the namespace, we have defined a class “MyProgram” and called the main function in the class. The main function has for loop with another for loop which is called a nested loop. The outer for loop has a variable “x” of int data type and the variable “x” is assigned a numeric value “1”.The condition is applied on the variable “x” that “x” should be less than or equal to “2” and then increment the value of “x”. Then we have an inner for loop in which there is a variable “y” of int data type and assigned with the numeric value “1”. The variable “y” has a condition that “y” should be less than or equal to “2”.
Within the inner for loop, we have utilized the continue statement. When “y” is equal to a value “3” then the continued statement executes. The writeLine method is invoked which will print the “x” and “y” values. Note that the only change is in the inner for loop as we used the continue statement there.
The value “3” of the variable “j” is ignored here. This can be displayed in the cited image below.
Example4: Using Continue Statement with Foreach Loop
The Continue keyword can also be used with foreach C# loops. The following example skips specific loop cycles in a foreach loop.
The above C# program has a class defined as “ContinueProgram” which we called the main function. In the main function, we have an array of six different values. The array is initialized in a variable “numbers” of data type int. Then we have a foreach loop that will print the elements of an initialized array.
The foreach loop has a variable as “num” to which an item of the variable “number” is stored through the “in” keyword. The “in” keyword iterates over each array’s elements. The foreach loop will not print the value “6” from the array as the continue statement executes when the “num” is equal to the value “6”.
From C# program execution above, the results are as follows:
This article shows how we utilize the continue statement in the C#. The continue statement uses various looping bodies, such as for, foreach, while, do-while, and so on to skip iterations based on the condition. We explained the introduction and working of the continue statement in the C# programming language. We have various examples of the continue statement in which all the possible loops are used.