Continue Statement in C#

Continue Statement in C# with Examples

In this article, I am going to discuss Continue Statement in C# Language with Examples. Please read our previous articles, where we discussed Break Statement in C# with Examples. At the end of this article, you will understand what is Continue Statement and when and how to use the continue statement in C# language with examples.

Continue Statement in C# Language:

In C#, continue is a keyword. By using the continue keyword, we can skip the statement execution from the loop body. Like the break statement, the use of the continue statement is also optional but use, then it should be placed within the loop body only.

If we know the maximum number of repetitions but some condition is there where we need to skip the statement execution from the loop body then we need to go for the continue statement. The Continue Statement in C# provides a convenient way to immediately start the next iteration of the enclosing FOR, WHILE, or Do While, for each loop.

The BREAK statement exits from a loop, whereas the CONTINUE statement exits only from the current loop iteration, proceeding immediately to the next iteration. The continue statement is almost always used with the if…else statement. In the case of for loop, the continue statement causes the conditional test and increment portions of the loop to execute. For while and do-while loops, the continue statement causes the program control to pass the conditional tests.

Continue Statement Flowchart:

Continue Statement Flowchart

The continue statement in C# is very similar to the break statement, except that instead of terminating the loop, it starts executing the body of the loop over from the top. The continue statement skips the rest of the body of the loop and immediately checks the loop’s condition. if the loop’s condition remains true, the loop’s execution resumes at the top of the loop.

How Does the Continue Statement Work in C# Language?

How Does the Continue Statement Work in C# Language?

Syntax: continue;

Example to Understand Continue Statement in C# Language:
using System;
namespace JumpStatementDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int counter = 10;
            while (counter >= 0)
            {
                if (counter == 7)
                {
                    counter--;
                    continue;
                }
                Console.WriteLine(counter);
                counter--;
            }
            Console.ReadKey();
        }
    }
}
Output:

Example to Understand Continue Statement in C# Language

if we do not place the counter statement in the body of “if” then the value of the counter would remain 7 indefinitely. The print statement is skipped when the counter value was 7.

Note: When the continue statement is executed within the loop body then control will pass back to the condition without executing the remaining statement.

The continue statement starts a new iteration of the closest enclosing iteration statement (for, for each, while, or do loop). For a better understanding, please have a look at the below example.

using System;
namespace JumpStatementDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 5; i++)
            {
                Console.Write($"Iteration {i}: ");

                if (i < 3)
                {
                    Console.WriteLine("skip");
                    continue;
                }

                Console.WriteLine("done");
            }
            Console.ReadKey();
        }
    }
}
Output:

Continue Statement in C# with Examples

Note: Like the break statement we should avoid using “Continue Statement” where it’s possible. the continue statement executes some statements of the loop and then exits the loop without executing some statements after it. we can use the if statement for this purpose instead of continue.

Some tricky Questions Related to C# Continue Statement.

Question1: What will be the output in the below program?
using System;
namespace JumpStatementDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 10;
            while (a <= 25)
            {
                a += 2;
                if (a > 15 && a < 20)
                    continue;
                Console.Write($"{a} ");
            }

            Console.ReadKey();
        }
    }
}

Output: 12 14 20 22 24 26

Question2: What will be the output in the below program?
using System;
namespace JumpStatementDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 1;
            while (a <= 50)
            {
                Console.Write($"{a} ");
                if (a >= 5 && a <= 35)
                    continue;
                a = a + 2;
            }

            Console.ReadKey();
        }
    }
}

Output: infinite loop

Question3: What will be the output in the below program?
namespace JumpStatementDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 8;
            while (a <= 42) ;
            {
                a += 2;
                if (a >= 20 && a <= 30)
                    continue;
                Console.Write($"{a} ");
            }

            Console.ReadKey();
        }
    }
}

Output: Error CS0139 No enclosing loop out of which to break or continue

Note: When the semicolon (;) is placed after the while then it becomes a dummy loop. When the dummy loop is created then the compiler will create an empty body without any statement and the current body becomes outside. Then automatically continue is placing outside then it becomes an error.

In the next article, I am going to discuss Goto Statement in C# Language with Examples. Here, in this article, I try to explain Continue Statement in C# Language with Examples. I hope you enjoy this Continue Statement in C# with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published.