C# Break Statement with Examples

In c#, Break statement is useful to break or terminate the execution of loops (for, while, do-while, etc.) or switch statements. The control is passed immediately to the next statements that follow a terminated loop or statements.

 

In c# nested loops, we can also use the break statement to stop or terminate the execution of inner loops based on our requirements.

Syntax of C# Break Statement

Following is the syntax of defining a break statement in the c# programming language.

 

break;

In our applications, we can use a break statement whenever we want to stop the execution of a particular loop or statement based on our requirements.

C# Break Statement Flow Chart

Following is the pictorial representation of the break statement process flow in the c# programming language.

 

C# Break Statement Flow Chart Diagram

 

Now we will see how to use break statement in for loopwhile loopdo-while loop, and switch statement in c# programming language with examples.

C# For Loop with Break Statement

In c#, by using the break keyword we can stop the execution of for loop statement based on our requirements.

 

Following is the example of stop the execution of for loop using a break statement.

 

using System;

namespace Tutlane
{
     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();
         }
     }
}

If you observe the above code, we used a break statement to exit for loop whenever the variable i value equals 3.

 

When you execute the above c# program, you will get the result as shown below.

 

C# For Loop with Break Statement Example Result

 

If you observe the above result, whenever the variable i value equals 3, the loop execution has stopped automatically.

 

This is how we can use the break statement in for loop to terminate the execution of for loop based on our requirements.

C# While Loop with Break Statement

In c#, we can exit or terminate the execution of a while loop immediately by using a break keyword.

 

Following is the example of using the break keyword in a while loop to terminate the execution of the loop in the c# programming language.

 

using System;

namespace Tutlane
{
     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();
         }
     }
}

If you observe the above example, whenever the variable (i) value becomes 2, we terminate the loop using the break statement.

 

When we execute the above c# program, we will get the result as shown below.

 

C# While Loop with Break Statement Example Result

 

This is how we can use break statements with a while loop to terminate a loop's execution based on our requirements.

C# Do-While Loop with Break Statement

In c#, we can exit or terminate the execution of a do-while loop immediately by using the break keyword.

 

Following is the example of using the break keyword in a do-while loop to terminate the execution of the loop in the c# programming language.

 

using System;

namespace Tutlane
{
     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();
         }
     }
}

If you observe the above example, whenever the variable (i) value becomes 2, we terminate the loop using the break statement.

 

When we execute the above c# program, we will get the result as shown below.

 

C# Do While Loop with Break Statement Example Result

 

This is how we can use break statements in the do-while loop to terminate a loop's execution based on our requirements.

 

To know how to use break statement in switch statements, check this Switch Statement in C# with Examples.

 

This is how we can use the break statement in our c# applications to terminate the execution of loops or statements based on our requirements.