Home > Article > Backend Development > 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.
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# are given below:
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.
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.
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.
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.
The above is the detailed content of Break in C#. For more information, please follow other related articles on the PHP Chinese website!