C# Continue Statement with Examples

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 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.

Syntax of C# Continue Statement

Following is the syntax of defining a continue statement in c# programming language.

 

continue;

We can use the continue statement in our applications whenever we want to skip the code's execution from the particular position and send back the control to the next iteration of the loop based on our requirements.

C# Continue Statement Flow Chart

Following is the pictorial representation of the continue statement process flow in the c# programming language.

 

C# Continue Statement Flow Chart Diagram

 

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.

C# For Loop with Continue Statement

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.

 

using System;

namespace Tutlane
{
     class Program
     {
         static void Main(string[] args)
         {
             for (int i = 1; i <= 4; i++)
             {
                 if (i == 3)
                     continue;
                 Console.WriteLine("i value: {0}", i);
             }
             Console.WriteLine("Press Enter Key to Exit..");
             Console.ReadLine();
         }
     }
}

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 as shown below.

 

C# Continue Statement with For Loop Example Result

 

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 further iteration of for loop based on our requirements.

C# While Loop with Continue Statement

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.

 

using System;

namespace Tutlane
{
     class Program
     {
         static void Main(string[] args)
         {
             int i = 0;
             while (i < 4)
             {
                 i++;
                 if (i == 2)
                    continue;
                 Console.WriteLine("i value: {0}", i);
             }
             Console.WriteLine("Press Enter Key to Exit..");
             Console.ReadLine();
         }
     }
}

If you observe the above example, whenever the variable (i) value becomes 2, we are skipping the further execution of statements and passing 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 as shown below.

 

C# Continue Statement with While Loop Example Result

 

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.

C# Do-While Loop with Continue Statement

Following is the example of using the continue keyword in do…while loop to pass the control to the next iteration of the loop in c# programming language.

 

using System;

namespace Tutlane
{
     class Program
     {
         static void Main(string[] args)
         {
             int i = 1;
             do
             {
                 Console.WriteLine("i value: {0}", i);
                 i++;
                 if (i == 2)
                    continue;
             } while(i < 4);
             Console.WriteLine("Press Enter Key to Exit..");
             Console.ReadLine();
         }
     }
}

If you observe the above example, whenever the variable (i) value becomes 2, we are skipping the further execution of statements and passing 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 as shown below.

 

i value: 1
i value: 2
i value: 3
Press Enter Key to Exit..

This is how we can use the continue statement with 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.