In c#, the Continue statement is used to pass control to the next iteration of loops such as for, while, do-while, or foreach from the specified position by skipping the remaining code.
In the previous chapter, we learned the break statement in c#. The main difference between the break statement and the continue statement is, the break statement will completely terminate the loop or statement execution. Still, the continue statement will pass control to the next iteration of the loop.
Following is the syntax of defining a continue statement in c# programming language.
We can use the continue statement in our applications whenever we want to skip the code's execution from a particular position and send back the control to the next iteration of the loop based on our requirements.
Following is the pictorial representation of the continue statement process flow in the c# programming language.
Now we will see how to use the continue statement in for loop, while loop, do-while loop, and with switch statement in c# programming language with examples.
In c#, by using the continue keyword, we can skip further code execution and send back control to the next iteration of for loop statement based on our requirements.
Following is the example of using a continue statement with for loop in c# programming language.
If you observe the above code, we used a continue statement to pass control back to the next iteration of for loop whenever the variable i value equals 3.
When we execute the above c# program, we will get the result below.
If you observe the above result, whenever the variable i value equals 3, it skips the further execution of statements and passes the control back to the next iteration of for loop.
This is how we can use the continue statement in for loop to skip the further execution of statements and send the control back to the further iteration of for loop based on our requirements.
In c#, we can stop executing further statements from the specified position and immediately send the control back to the further iteration of the while loop.
Following is the example of using the continue keyword in a while loop to pass the control to the next iteration of the loop in the c# programming language.
If you observe the above example, whenever the variable (i) value becomes 2, we are skipping the further execution of statements and pass the control back to the further iteration of the while loop using the continue statement.
When we execute the above c# program, we will get the result below.
This is how we can use the continue statement with a while loop to pass the control back to the further iteration of the loop based on our requirements.
Following is the example of using the continue keyword in the do…while loop to pass the control to the next iteration of the loop in c# programming language.
If you observe the above example, whenever the variable (i) value becomes 2, we are skipping the further execution of statements and pass the control back to the further iteration of the loop using the continue statement.
When we execute the above c# program, we will get the result below.
This is how we can use the continue statement with the do…while loop to pass the control back to the further iteration of the loop based on our requirements.
This is how we can use the continue statement in our c# applications to stop the execution of further statements from the specified position and pass control back to the next iteration of the loop based on our requirements.