Home > C# > C# Loop Control Statements

Loop control statements in c#

Loops are control structures provided by C#. However, there are some control statements that change the execution of loops from its normal sequence. All variables or objects that we create inside the scope of these control structures no longer exist when we use these control statements in loops.
Following are loop control statements in C#

  1. break
  2. continue

C# break statement

The C# break statement is used to break a loop body. i.e. Terminate the execution of the loop body. When the compiler encounters a break statement inside a loop body. It terminates the execution of loop and shifts control to the statement after loop body. We can also use break statement in the switch statement to control the flow of execution depending on cases.
Below example will show the working of C# break statement inside the loop body.
Example Program

using System;

namespace @break
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int x=0; x < 5; x++)
            {
                Console.WriteLine("This is inside loop before break statement. value of x = "+x);
                break;
                Console.WriteLine("This is inside loop after break statement.");
            }
            Console.WriteLine("This is Outside loop after break statement.");
        }
    }
}

Output
 loop control statements in C# - C# break statement
As from the output we can see that the compiler executes the loop body once because of break statement inside the loop body. As when the compiler executes the loop for the first time it displays message This is inside loop before break statement. value of x = 0 then the compiler finds a break statement thus exiting from the loop body and shifting the control to the statement after the loop body. Because of that, it terminated execution of statements inside loop body and thus it didn’t print message This is inside loop after break statement. Hence, shifting control outside the loop.

C# continue Statement

Continue statement in C# works almost like break statement. Yet with some difference. i.e. instead of terminating execution of the loop, it forces the compiler for the next iteration.
Whenever the compiler finds a continue statement in a loop body. The compiler skips all the statements below the C# continue statement and the loop will start its next iteration from the start.
Below example will clarify this concept.
Example program

using System;
namespace @continue
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int x = 0; x < 5; x++)
            {
                Console.WriteLine("This is inside loop before continue statement. 
                value of x = " + x);
                continue;
                Console.WriteLine("This is inside loop after continue statement.");
            }
            Console.WriteLine("This is Outside loop after continue statement.");
        }
    }
}

Output
 loop control statements in C# - C# continue Statement
It can be seen in the image above that loop is executed 5 times as expected. But one thing that is to be noted that it never executed any statement inside loop body which is after continue. i.e. a message This is inside loop after the continue statement. After complete execution of loop body, it then reaches the statement outside loop body and displays message This is Outside loop after the C# continue statement.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.