Example 1:
This instance shows how to utilize the “break” statement with the nested “if” statement.
class Program
{
static void Main() {
int i=0;
int Number_0= 98;
while(i90)
{
Console.WriteLine("The Number is greater than 90 ");
if(i == 2)
break;
}
}
}
}
In the first program, initialize an integer type variable, say “i” with 0. Similarly, another variable which is “Number_0” having an integer type is declared with a value of 98. Then, use “while” to loop around the “if” statement. Inside the “if” statement defines a condition. The condition shows that it iterates until “i” reaches 3 (i<4). Inside the body of “while”, increment the “i” iterator and set the “if” conditional statement to check whether the “Number_0” variable is greater than 90 or not. If it is true, implement the body. Inside the body of “if”, we have another “if” – the nested “if”. This checks whether the value of “i” is 2 or not. When the iterator value is 2, it stops the loop and moves out of the body of “while”. It does not matter whether the condition of the “while” loop is satisfied or not. But when the “break” statement is encountered, it moves out. When the value of “i” becomes 2, it terminates the code due to the use of the “break” statement.
Example 2:
Here, the “break” statement is applied within the “for” loop.
class Program
{
static void Main() {
for(int i=0; i<5; i++)
{
Console.WriteLine("Hello "+i+" Times");
if(i == 3)
{
break;
}
}
}
}
Utilize a “for” loop in this code. Within this loop, initialize the iterator to 0, define a condition, and then increment the iterator. After applying the “for” loop, open the curly braces to write some code in the body of the “for” loop. Now, print a text with the iterator value by concatenating both inside the Console.WriteLine() function. Then, use a conditional statement to check the limit. Here, the conditional statement shows that when “i” reaches 3, it breaks the loop. The loop executes when i=0, the body is checked, then the “if” condition is checked. Whenever the condition of “if” becomes false, its body does not execute and “i” is incremented by one. Then, loop executes again until the value of “i” is equal to 3. When it reaches 3, the “if” statement becomes true and its body is implemented that terminates the loop.
Example 3:
This sample demonstrates the functionality of the “break” statement inside the “switch”.
class Program
{
static void Main() {
char alpha= 'P';
switch(alpha)
{
case 'p':
{
Console.WriteLine("Alphabet found");
break;
}
case 'P':
{
Console.WriteLine("Alphabet found");
break;
}
default:
{
Console.WriteLine("Not found");
break;
}
}
}
}
First, declare the “char” type variable. Then, utilize the “switch” statement. Then, give the variable name to the “switch” and open the body of the “switch” statement. We define the different cases within the body. Then, the compiler checks the cases one by one. In the first case, assign the “p” value. And in the body of the first case, console out the “Alphabet found” message and then break the switch. Now, close the body of the first case. In the second case, set the condition to “P” and employ the Console.WriteLine() method inside its body to show the text, “Alphabet found”. Then, utilize the “break” statement. In the next statement, set the default. When no case is encountered, the default is executed. This works in such a way that the compiler puts a value of the char type “alpha” variable in the switch and it compares with all the cases of the “switch” statement. The compiler checks the first case. The first case shows that both the character in the case and the alpha value are “p”. The only distinction between the two is that one is in small letters, while the other is in the capital letters. C# language is case-sensitive, so it does not fulfill the condition of the first case. Then, it checks the second case. If the second case is true, the compiler goes inside the body of the second case and executes the code. When “break” reaches, the compiler moves out of the body of the “switch” and terminates the switch. Consider a case: When no case is true, the default is carried out. Since we are dealing with the character, all the characters are written inside single quotes.
Conclusion
The use of a “break” statement in C# language is thoroughly discussed in this guide. We utilize this in the loop to break the loop at a required condition. This statement stops the loop and executes the next statement. We implemented the codes to show the usage of the “break” statement inside the “if”, “for”, “while”, and “switch” statements. Because the switch statement requires it, the “break” statement is frequently employed. When one condition is fulfilled, the “break” statement stops the switch. The switch statement works in such a way that it checks all the conditions for one variable. If the first condition is true, it executes its body and then breaks the switch. It cannot check the other cases. When the compiler encounters the “break” statement, it stops the code.