C# Continue Statement with Examples

  Keywords : C# continue statement to continue the current flow of while loop or do while loop or switch statements with examples, C# continue statement to skip the execution of loops with examples

Here we will learn continue statement in c# with examples, use c# continue statement in for loop with examples, use c# continue statement in while or do-while loop with examples.

C# Continue Statement

In c#, Continue statement is used to pass a 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 previous section, we learned break statement in c#. The main difference between break statement and continue statement is, the break statement will completely terminate the loop or statement execution but the continue statement will pass a control to the next iteration of loop.

Syntax of C# Continue Statement

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

 

continue;

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

C# Continue Statement Flow Chart

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

 

C# Continue Statement Flow Chart Diagram

 

Now we will see how to use 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 continue keyword we can skip the execution of further code and send back control to next iteration of for loop statement based on our requirements.

 

Following is the example of using 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 Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above code, we used a continue statement to pass control back to the next iteration of for loop whenever the variable i value equals to 3.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Continue Statement with For Loop Example Result

 

If you observe above result, whenever the variable i value equals to 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 we use 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 the execution of further statements from the specified position and send the control back to the further iteration of while loop immediately.

 

Following is the example of using continue keyword in while loop to pass the control to next iteration of loop in 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 Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, whenever the variable (i) value become 2 we are skipping the further execution of statements and passing the control back to the further iteration of while loop using continue statement.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Continue Statement with While Loop Example Result

 

This is how we can use continue statement with while loop to pass the control back to the further iteration of loop based on our requirements.

C# Do-While Loop with Continue Statement

Following is the example of using continue keyword in do…while loop to pass the control to next iteration of 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)

                    break;

            } while(i < 4);

            Console.WriteLine("Press Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, whenever the variable (i) value become 2 we are skipping the further execution of statements and passing the control back to the further iteration of loop using continue statement.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Continue Statement with Do-While Loop Example Result

 

This is how we can use continue statement with do…while loop to pass the control back to the further iteration of loop based on our requirements.

 

This is how we can use continue statement in our c# applications to stop the execution of further statements from the specified position and pass a control back to the next iteration of loop based on our requirements.