C# break Statement

The C# break statement is used inside loops and switch statements. As soon as the control reads the break statement, it stops the execution of code block immediately inside which it exists.

After termination, the control gets transferred to the next statement which comes after that code block.

In the following example, a for loop is being used to print the numbers. Following loop is supposed to print the values from 1 to 10. But, inside for loop, there is an if statement which is checking the value of i. When the value of i becomes 5 the break statement gets executed.

The break statement terminates the loop after 5 counts.

Example

Try It
using System;
 
namespace TutorialsLeader
{
    public class MyFirstClass
    {
        static void Main()
        {
            for (int i = 0; i < 10; i++)
            {
                if (i == 5)
                {
                    break;
                }
                Console.WriteLine("The value of i is {0}", i);
            }
 
            Console.WriteLine("The above loop terminated after 5 times");
        }
    }
}

break in nested loop

Following is a nested for loop which is printing a series of stars '*'. To print the series in a particular manner the break statement is being used.

Example

Try It
using System;
 
namespace TutorialsLeader
{
    public class MyFirstClass
    {
        static void Main()
        {
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; /*Empty condition part*/ ; j++)
                {
                    if (j > i)
                        break;
                    else
                        Console.Write("*");
                }
                Console.Write("\n");
            }
        }
    }
}

Using break in switch statement

In the following example break is being used in switch statement. After execution of a particular case block it terminates the switch.

Example

Try It
using System;
 
namespace TutorialsLeader
{
    public class MyFirstClass
    {
        static void Main()
        {
            Console.Write("Enter a value between 1 to 5: ");
            string str = Console.ReadLine();
            int Val = Int32.Parse(str);
 
            switch (Val)
            {
                case 1:
                    Console.WriteLine("You have entered one.");
                    break;
                case 2:
                    Console.WriteLine("You have entered two.");
                    break;
                case 3:
                    Console.WriteLine("You have entered three.");
                    break;
                case 4:
                    Console.WriteLine("You have entered four.");
                    break;
                case 5:
                    Console.WriteLine("You have entered five.");
                    break;
                default:
                    Console.WriteLine("Sorry, you have entered an invalid number.");
                    break;
            }
        }
    }
}

Let's learn about continue statement in the next section.