Updated March 17, 2023
Introduction to Break in C#
Generally, when we are talking about terminating some execution at that time we are giving condition to do so. But in many cases, we don’t have an exact condition to get out of some loop or condition. Here, with the break statement, we are matching the condition to get out of the current execution and pass the control over the next upcoming statement. It helps us to continue with execution by avoiding particular operations at a certain stage. In programming languages, we often encountered with the break statements. Break statements are something that resembles its meaning to break the logic here. Like other programming languages, c# also has a break statement. You must have seen a break statement in the switch case also. In switch cases after every case, we find this break statement to come out of that case if not matched and move forward.
Syntax:
Break statement has very easy syntax as follows:
break;
Just the keyword break and semicolon. Defining a break statement is nothing but handing over the control to the next operation in sequence. Break statements are the rule applied to get out of a particular situation on time and stop further execution.
Flow Diagram
- Above, the flow chart simply shows the working of the break statement, at the start of the flow it checks for the particular condition. If it satisfied then the loop continues. At the point where the loop gets a break statement. Or, the condition where this loop gets out of the loop with the use of a break statement.
- The flow is effortless just need to understand it by executing some examples.
- The best thing to get familiar with the break statement is to write the code and try output with different scenarios.
- Break statements are very easy. But a lot of people got confused with it as it gets out of the loop and starts further execution process.
- There are many cases where we use these statements, especially in nested loops. In the nested loop inner loop gets break statements at a particular event to get out of the loop.
How does Break Statement Work in C#?
Suppose we have one program and we are running loop in that program. Our requirement is if the loop reaches to 5 stop the execution of the loop and start running code in the sequence. If you look at the examples carefully break statements more likely to work as a meaning it has. It breaks the execution flow at the specified location and control is going to pass over the next required operation.
Examples of Break Statement in C#
Examples of Break Statement in C# are given below:
Example #1
Program to get no’s till 10. If it exceeds 10 then break the loop.
using System;
public class EvenNo {
public static void Main(string[] args) {
for(int i=0;i<=20;i++){
Console.WriteLine(i);
if(i==10){
break;
}
}
} }
Output:
In the above program, we used one for a loop. In this, we have given the condition if i is less than equal to 20 then execute further. We have given if condition in for loop that if the value of i reaches to 10 then stop executing for a loop. And this scenario we achieved through break statement. Try this example in an editor and you will get an output as above.
Example #2
Now we are going to see break statement with the switch case
using System;
public class Switch
{
public static void Main(string[] args)
{
int n=2;
switch (n)
{
case 1:
Console.WriteLine("Current value of n is: 1");
break;
case 2:
Console.WriteLine("Current value of n is: 2");
break;
case 3:
Console.WriteLine("Current value of n is: 3");
break;
case 4:
Console.WriteLine("Current value of n is: 4");
break;
default:
Console.WriteLine("Please give the correct no.");
break;
}
}
}
Output:
In the above program, we have used a switch case. In this, we are checking multiple cases. From case one we are checking the case against the given condition. If the switch case doesn’t match the particular condition it breaks that case with a break statement and jumps to the next case. It executes till getting a matching case. If the case gets matched then it gets executed and output will be shown. In the worst case if none of the condition gets satisfied then execution get the default case mentioned in the switch case statements with the keyword default. After executing this statement again it gets the break statement and operation get over.
Example #3
We are going to see break statement with do-while loop here:
using System;
public class Program
{
public static void Main(string[] args)
{
int i = 0;
do
{
Console.WriteLine("The value of i is :{0}", i);
i+=2;
if (i == 10)
break;
}while (i < 20);
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
}
Output:
In the above program, we have used a do-while loop with a break statement. We have checked for a value of i. we increment the value of I by 2. We break the loop once it reaches 10. But we have while condition to check it till 20. We break the execution in the middle of the loop as per our requirement. Till now we have seen examples of a break statement. Which shows how can we use the break statement with different loops and with if condition. These are very basic examples to check to work of break statements. To try these examples in a suitable editor. When we are executing any loop inside the program and we need to break it in between of the execution at that time we use break statement with the keyword break and semicolon. At this time of the break, the statement gets the execution out of the loop. After that control gets over to the next execution statement in the sequence.
Conclusion
Every language has a break statement to come out of the loop or a condition at a particular point. It totally depends on the requirement. This is a very small but useful statement in any language and thus, it for c# also. Try to gets hands dirty on break statement usage.
Recommended Articles
This is a guide to Break Statement in C#. Here we discuss the introduction and working of break statement in c# along with its examples. You may also look at the following articles to learn more –