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

C# Break Statement

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.

  • Set up the environment

Create a new C# Console Application project in Visual Studio.

  • Break in a for loop

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
  • Break in a while loop

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.

  • Break in a do-while loop

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.

  • Break in a switch statement

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.

  1. C# break statement example:

    • Description: This example introduces the break statement in C#, which is used to exit a loop or switch statement prematurely.
    • Code:
      for (int i = 1; i <= 5; i++)
      {
          Console.WriteLine(i);
          if (i == 3)
          {
              // Using break to exit the loop
              break;
          }
      }
      
  2. C# switch statement with break:

    • Description: Showing how the break statement is used within a switch statement to exit the switch block.
    • Code:
      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;
      }
      
  3. Breaking out of nested loops in C#:

    • Description: Illustrating how break can be used to exit both inner and outer loops in nested loop structures in C#.
    • Code:
      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;
              }
          }
      }
      
  4. C# for loop with break condition:

    • Description: Showing how to use break within a for loop based on a specific condition.
    • Code:
      for (int i = 1; i <= 10; i++)
      {
          Console.WriteLine(i);
          if (i == 5)
          {
              // Using break to exit the loop at a specific condition
              break;
          }
      }
      
  5. Using break in while and do-while loops in C#:

    • Description: Demonstrating the use of break within both while and do-while loops in C# to exit the loop based on a condition.
    • Code:
      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);