C# Break Statement with Examples

  Keywords : C# break statement to break the current flow of while loop or do while loop or switch statements with examples, C# break statement to stop the execution of loops at a given condition example

Here we will learn break statement in c# with examples, use break statement in c# for loop with examples, use break statement in c# while and do-while loops with examples.

C# Break Statement

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

 

In c# nested loops also we can use 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 c# programming language.

 

break

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

C# Break Statement Flow Chart

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

 

C# Break Statement Flow Chart Diagram

 

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

C# For Loop with Break Statement

In c#, by using 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 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 Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

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

 

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

 

C# For Loop with Break Statement Example Result

 

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

 

This is how we can we use 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 while loop immediately by using break keyword.

 

Following is the example of using break keyword in while loop to terminate execution of loop in 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 Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

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

 

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

 

C# While Loop with Break Statement Example Result

 

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

C# Do-While Loop with Break Statement

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

 

Following is the example of using break keyword in do-while loop to terminate execution of loop in 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 Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

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

 

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

 

C# Do While Loop with Break Statement Example Result

 

This is how we can use break statement in do-while loop to terminate the execution of loop 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 break statement in our c# applications to terminate the execution of loops or statements based on our requirements.