Break and Continue Statements in C#

This article explains some very useful concepts of C#, these are also called loops. I'll explain and differentiate them with an example.

Loop in C#

Break Statement

A Break statement breaks out of the loop at the current point or we can say that it terminates the loop condition.

It is represented by break;

Continue Statement

A Continue statement jumps out of the current loop condition and jumps back to the starting of the loop code.

It is represented by continue;

Example | Break vs Continue

This example shows the functioning of both of the statements together, afterwards I'll explain them one by one with their functionality.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text; 

 

namespace Hello

{

    class Program

    {

        static void Main(string[] args)

        {

 

            int i;          

          for (i = 0; i <= 10; i++)

          { 

              if (i == 5)                             

                  continue;

             

              if (i == 8)              

                  break

              Console.WriteLine("value is"  +i);            

          } 

          Console.ReadLine();              

        }    

    }
}

Output Window

Break and Continue

Explanation

In this example the output is from 0 to 7 except 5, 8, 9?

CASE 1: It is because in the first case:

if (i == 5)                             
continue;

In other words it will skip the current loop and jump to the top of the loop, so there is no 5 in the output window.

CASE 2: In second cases:

if (i == 8)              

break;

It will terminate the loop or get out of it when the value becomes 8, so there is no 8 or 9 in the output window.

Now let's do some changes in the code and see what happens, here we go.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Hello

{

    class Program

    {

        static void Main(string[] args)

        {

            int i;          

          for (i = 0; i <= 10; i++)

          { 

             if (i == 5)                             

                  continue;             

          //   if (i == 8)              

          //        break; 

             Console.WriteLine("value is: "  +i);            

          }

          Console.ReadLine();              

        }    

    }

}

 
Loop Explanation 

Now you can see the output. The condition continues to flow depending on the continue statement functioning.

Now I am making some other modifications, in the code snippet, as:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Hello

{

    class Program

    {

        static void Main(string[] args)

        { 

            int i;          

          for (i = 0; i <= 10; i++)

          { 

          //   if (i == 5)                             

          //        continue;

             

             if (i == 8)              

                  break

             Console.WriteLine("value is: "  +i);            

          } 

          Console.ReadLine();              

        }    

    }

}

 
continue statement 

Now you can see, it is again showing from 0 to 7 and because of the break statement condition, it's again out of the loop so there is no 8, 9 or 10.

Up Next
    Ebook Download
    View all
    Learn
    View all