In C#, the break statement is used to terminate the execution of the nearest enclosing loop, switch statement, or foreach loop. Once the break statement is executed, control is immediately transferred to the statement following the terminated structure.
1. In switch Statements The break statement prevents the execution from "falling through" to subsequent cases in a switch.
int day = 2;
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
default:
Console.WriteLine("Other Day");
break;
}
Explanation:
Without the break, all subsequent cases would be executed after a match.
The break ensures that only the matched case executes.
2.In Loops The break statement is used to exit the loop prematurely when a specific condition is met.
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
break; // Exit the loop when i equals 5
}
Console.WriteLine(i);
}
0
1
2
3
4
3. In while and do-while Loops You can use break in while or do-while loops to exit the loop when a condition is met.
int count = 0;
while (true)
{
Console.WriteLine(count);
if (count == 3)
{
break; // Exit the loop when count equals 3
}
count++;
}
0
1
2
3
4. In foreach Loops The break statement can terminate a foreach loop when a specific condition is met.
string[] names = { "Alice", "Bob", "Charlie", "David" };
foreach (var name in names)
{
if (name == "Charlie")
{
break; // Exit the loop when "Charlie" is encountered
}
Console.WriteLine(name);
}
Output
Alice
Bob
Key Points
The break statement only affects the nearest enclosing loop or switch statement.
In nested loops or switch inside loops, break only exits the current loop or switch. Use goto or other mechanisms to exit multiple layers if needed.
break cannot be used to exit functions or methods. For that, use return.
When to Use break
Use break when:
In C#, the break statement is used to terminate the execution of the nearest enclosing loop, switch statement, or foreach loop. Once the break statement is executed, control is immediately transferred to the statement following the terminated structure.
1. In switch Statements The break statement prevents the execution from "falling through" to subsequent cases in a switch.
int day = 2; switch (day) { case 1: Console.WriteLine("Monday"); break; case 2: Console.WriteLine("Tuesday"); break; case 3: Console.WriteLine("Wednesday"); break; default: Console.WriteLine("Other Day"); break; }
Explanation:
Without the break, all subsequent cases would be executed after a match.
The break ensures that only the matched case executes.
2.In Loops The break statement is used to exit the loop prematurely when a specific condition is met.
for (int i = 0; i < 10; i++) { if (i == 5) { break; // Exit the loop when i equals 5 } Console.WriteLine(i); }
0 1 2 3 4
3. In while and do-while Loops You can use break in while or do-while loops to exit the loop when a condition is met.
int count = 0; while (true) { Console.WriteLine(count); if (count == 3) { break; // Exit the loop when count equals 3 } count++; }
0 1 2 3
4. In foreach Loops The break statement can terminate a foreach loop when a specific condition is met.
string[] names = { "Alice", "Bob", "Charlie", "David" }; foreach (var name in names) { if (name == "Charlie") { break; // Exit the loop when "Charlie" is encountered } Console.WriteLine(name); }
Output
Alice Bob
Key Points
The break statement only affects the nearest enclosing loop or switch statement.
In nested loops or switch inside loops, break only exits the current loop or switch. Use goto or other mechanisms to exit multiple layers if needed.
break cannot be used to exit functions or methods. For that, use return.
When to Use break
Use break when:
C# is primarily used on the Windows . NET framework, although it can be applied to an open source platform. This highly versatile programming language is an object-oriented programming language (OOP) and comparably new to the game, yet a reliable crowd pleaser.
The C# language is also easy to learn because by learning a small subset of the language you can immediately start to write useful code. More advanced features can be learnt as you become more proficient, but you are not forced to learn them to get up and running. C# is very good at encapsulating complexity.
The decision to opt for C# or Node. js largely hinges on the specific requirements of your project. If you're developing a CPU-intensive, enterprise-level application where stability and comprehensive tooling are crucial, C# might be your best bet.
C# is part of .NET, a free and open source development platform for building apps that run on Windows, macOS, Linux, iOS, and Android. There's an active community answering questions, producing samples, writing tutorials, authoring books, and more.
Copyrights © 2024 letsupdateskills All rights reserved