In this article I am going to discuss about C# Break Statement which is one of the type of jump statements but before that if you had not read my previous article of C# foreach loop then must read it.
Introduction of Jump Statements in C#
Jump Statements are one of the type of control flow statements which is used to transfer the control of execution from one block of statement to another block of statement due to some specified condition while executing the program. The Jump Statements allow us to exit a loop, and start the next iteration, or explicitly transfer the program control to a specified location in your program. There are various types of Jump Statements are there in csharp which is given below:
- Break Statement
- Continue Statement
- Goto Statement
- Return Statement
- Throw Statement
These Statements have their own uses and own work. Return Statement is used to return from the program means terminating the program and throw statement is used in exception handling so when we discuss exception handling we will discuss this statement in detail. Today I am going to discuss C# Break Statement in detail.
Introduction to C# Break Statement
In C#, the break is a keyword which is used to terminate either the loop body or the switch body. The most important point that you need to keep in mind is the use of a C# break statement is optional but if you want to use then the break statement should be placed either within the loop body or switch body. As we had already discussed switch statement and there we had used this break statement to terminate from the switch body.
When to use Break Statement?
When we know the maximum number of repetitions of a loop but if some condition is there where we need to terminate the loop body then we can use break statement. That means the Break Statement in C# provides a convenient way to immediately exit from a loop or Switch Case statement. The break statement ends the loop immediately when it is encountered.
C# Program to Explain Break Statement
using System; namespace BreakStatementDemo { class Program { static void Main(string[] args) { for (int i = 1; i <= 10; i++) { Console.WriteLine($"Value of i : {i}"); if (i == 5) { break; } } Console.ReadKey(); } } }
Output:
Value of i : 1
Value of i : 2
Value of i : 3
Value of i : 4
Value of i : 5
In above example you can see that I have using for loop which is started from 1 and end till i is not less than or equal to 10 but inside the loop I have wrote one condition that if i is equal to 5 then break the loop which means to terminate the loop body. Therefore the value of i is only printed till i is equal to 5 and after that loop body has been terminated.
C# Program to Explain Break Statement with Inner Loop
using System; namespace BreakStatementWithInnerLoop { class Program { static void Main(string[] args) { for (int outer = 1; outer <= 5; outer++) { Console.WriteLine("Outer Loop : "+ outer); for (int inner = 1; inner <= 5; inner++) { if (inner > 3) { break; } Console.WriteLine(" Inner Loop : " + inner); } Console.WriteLine(); } Console.ReadKey(); } } }
Output:
Outer Loop : 1
Inner Loop : 1
Inner Loop : 2
Inner Loop : 3
Outer Loop : 2
Inner Loop : 1
Inner Loop : 2
Inner Loop : 3
Outer Loop : 3
Inner Loop : 1
Inner Loop : 2
Inner Loop : 3
Outer Loop : 4
Inner Loop : 1
Inner Loop : 2
Inner Loop : 3
Outer Loop : 5
Inner Loop : 1
Inner Loop : 2
Inner Loop : 3
In above program I have used the break statement inside the inner loop to terminate the body if inner is greater than 3. Therefore every time when inner loop started it will terminate when it is greater than 3 and only print till 3.
Conclusion
In this C# tutorials article I have discussed about jump statements , its types and also discussed the csharp break statement. Hope you like this article. In my next article I am going to discuss about Continue Statement in detail.