C# Break Statement

C# Break Statement

The C# jump statements are break, continue, goto, return, and throw.

1 - C# Break Statement

The C# break statement ends the execution of the body of an iteration or switch statement:

int x = 0; 
while (true) {
    if (x++ > 5) 
        break ; // break from the loop 
} 
// execution continues here after break 

 

Syntax of C# Break Statement:

jump-statement;    
break;  

 

C# Break Statement Flow Chart:

 

C# Break Statement - XDevSpace

 

2 - C# Break Statement Example

Let's see a simple example of C# break statement which is used inside the loop:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 10; i++)
            {
                if (i == 5)
                {
                    break;
                }
                Console.WriteLine(i);
            }
            //Console.WriteLine("Press Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

Output:

C# Break Statement Example - XDevSpace

 

2.1 - C# Break Statement Example 2:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 0;
            while (true)
            {
                Console.WriteLine("before break:" + x);
                if (x++ > 5)
                    break;      // break from the loop
                Console.WriteLine("after break:" + x);
            }
            Console.ReadLine();
        }
    }
}

Output:

C# Break Statement Example 2 - XDevSpace

 

2.2 - C# Break Statement Example 3:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            /* local variable definition */
            int a = 10;

            /* while loop execution */
            while (a < 20)
            {
                Console.WriteLine("value of a: {0}", a);
                a++;
                if (a > 15)
                {
                    /* terminate the loop using break statement */
                    break;
                }
            }
            Console.ReadLine();
        }
    }
}

Output:

C# Break Statement Example

 

3 - C# Break Statement with Inner Loop

The C# break statement breaks inner loop only if you use break statement inside the inner loop. 

Let's see the example code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 3; i++)
            {
                for (int j = 1; j <= 3; j++)
                {
                    if (i == 2 && j == 2)
                    {
                        break;
                    }
                    Console.WriteLine(i + " " + j);
                }
            }
            Console.ReadLine();
        }
    }
}

Output:

When the above code is compiled and executed, it produces the following result:

C# Break Statement with Inner Loop - XDevSpace

4 - C# For Loop with Break Statement

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

Output:

C# For Loop with Break Statement - XDevSpace

 

5 - C# While Loop with Break Statement

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

Output:

C# While Loop with Break Statement - XDevSpace

 

6 - C# Do-While Loop with Break Statement

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    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 Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

Output:

C# Do-While Loop with Break Statement - XDevSpace