In c#, Break statement is used to break or terminate the execution of loops (for, while, do-while, etc.) or switch statements and the control is passed immediately to the next statements that follow a terminated loops or statements.
In c# nested loops also we can use the break statement to stop or terminate the execution of inner loops based on our requirements.
Following is the syntax of defining a break statement in c# programming language.
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.
Following is the pictorial representation of the break statement process flow in c# programming language.
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.
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 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 to 3.
When you execute the above c# program, you will get the result as shown below.
If you observe the above result, whenever the variable i value equals 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.
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 break keyword in a while loop to terminate the 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 Enter 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 the above c# program, we will get the result as shown below.
This is how we can use break statements with a while loop to terminate the execution of a loop based on our requirements.
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 break keyword in a do-while loop to terminate the execution of loop in 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 above example, whenever the variable (i) value become 2 we are terminating the loop using break statement.
When we execute the above c# program, we will get the result as shown below.
This is how we can use break statements in the do-while loop to terminate the execution of a 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 the break statement in our c# applications to terminate the execution of loops or statements based on our requirements.