C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In this tutorial, we'll explore the use of the break
statement in C#. The break
statement is used to terminate the execution of a loop or switch statement prematurely and transfer control to the statement following the loop or switch.
Create a new C# Console Application project in Visual Studio.
The break
statement can be used to exit a for
loop early when a specific condition is met. Consider the following example:
for (int i = 0; i < 10; i++) { if (i == 5) { break; } Console.WriteLine($"Value: {i}"); }
In this example, the for
loop will stop executing when i
is equal to 5, and the remaining iterations will be skipped. The output will be:
Value: 0 Value: 1 Value: 2 Value: 3 Value: 4
The break
statement can also be used in a while
loop. Consider the following example:
int i = 0; while (true) { if (i == 5) { break; } Console.WriteLine($"Value: {i}"); i++; }
Here, we have an infinite while
loop that will continue to execute until the break
statement is encountered when i
is equal to 5. The output will be the same as in the previous example.
Similarly, the break
statement can be used in a do-while
loop:
int i = 0; do { if (i == 5) { break; } Console.WriteLine($"Value: {i}"); i++; } while (true);
Again, the output will be the same as in the previous examples.
The break
statement is used in switch
statements to exit a specific case after executing the related code block:
int number = 2; switch (number) { case 1: Console.WriteLine("One"); break; case 2: Console.WriteLine("Two"); break; case 3: Console.WriteLine("Three"); break; default: Console.WriteLine("Unknown"); break; }
In this example, the break
statements ensure that the switch
statement exits after executing the code for the matching case. The output will be:
Two
In this tutorial, we've shown you how to use the break
statement in C# to terminate the execution of a loop or switch statement prematurely. The break
statement can be useful for optimizing code by stopping the execution of a loop or switch when a specific condition is met, saving resources and time.
C# break statement example:
break
statement in C#, which is used to exit a loop or switch statement prematurely.for (int i = 1; i <= 5; i++) { Console.WriteLine(i); if (i == 3) { // Using break to exit the loop break; } }
C# switch statement with break:
break
statement is used within a switch
statement to exit the switch block.int choice = 2; switch (choice) { case 1: Console.WriteLine("Option 1 selected"); break; case 2: Console.WriteLine("Option 2 selected"); break; default: Console.WriteLine("Invalid option"); break; }
Breaking out of nested loops in C#:
break
can be used to exit both inner and outer loops in nested loop structures in C#.for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { Console.WriteLine($"i: {i}, j: {j}"); if (i == 2 && j == 2) { // Using break to exit both loops break; } } }
C# for loop with break condition:
break
within a for
loop based on a specific condition.for (int i = 1; i <= 10; i++) { Console.WriteLine(i); if (i == 5) { // Using break to exit the loop at a specific condition break; } }
Using break in while and do-while loops in C#:
break
within both while
and do-while
loops in C# to exit the loop based on a condition.int count = 0; while (count < 5) { Console.WriteLine(count); if (count == 2) { // Using break to exit the while loop break; } count++; } int number = 1; do { Console.WriteLine(number); if (number == 3) { // Using break to exit the do-while loop break; } number++; } while (number <= 5);