Break in C#

The break statement in C# is a control flow statement used to exit from the innermost loop immediately. It is used within loops, such as for, for each, while, and do while.

Syntax

break;

Example 1. Break in For Loop

for (int i = 0; i <= 10; i++)
{
    if (i == 5)
    {
        break;
    }

    Console.WriteLine(i);
}

In this provided code snippet, a for loop iterates from 0 to 10, printing the value of i at each iteration. However, if i is equal to 5, the loop terminates prematurely using the break statement.

Steps

  1. Initialization: The loop starts with I set to 0.
  2. Condition check: The loop continues as long as i is less than or equal to 10.
  3. Inside the Loop.
  4. The loop checks if i is equal to 5 using the condition if (i == 5).
  5. If i is indeed equal to 5, the break statement is executed, causing the loop to exit immediately.
  6. If i is not equal to 5, the loop continues to execute, printing the value of i.

Explanation

  1. The loop starts with i = 0 and prints it.
  2. The loop increments i to 1 and prints it.
  3. This process continues until I become 5.
  4. When i equals 5, the if condition becomes true, and the break statement is executed, causing the loop to terminate prematurely.
  5. Therefore, only the numbers 0 through 4 are printed.

Result

Output

Example 2. Break in Foreach Loop

int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

foreach (int number in numbers)
{
    if (number == 5)
    {
        break; // Exit the loop when number equals 5
    }
    Console.WriteLine(number); // Print the current number
}

Explanation

  1. The foreach loop iterates over each element in the numbers array.
  2. For each iteration, it checks if the current number is equal to 5 using the condition if (number == 5).
  3. If the number is indeed equal to 5, the break statement is executed, causing the loop to exit immediately.
  4. If the number is not equal to 5, it prints the value of a number.
  5. Therefore, only the numbers 0 through 4 are printed, and the loop terminates when 5 is encountered.

Result

Output

Example 3. Break in While Loop

int i = 0;
while (i <= 10)
{
    if (i == 5)
    {
        break;
    }

    Console.WriteLine(i);
    i++;
}

Explanation

  1. Initialization: Start with i set to 0.
  2. The loop continues as long as i is less than or equal to 10.
  3. Inside the Loop.
  4. Check if i is equal to 5 using the condition if (i == 5).
  5. If i is indeed equal to 5, the break statement is executed, causing the loop to exit immediately.
  6. If i is not equal to 5, the loop continues to execute, printing the value of i.
  7. After printing the value of i, it increments i by 1.
  8. Therefore, the loop terminates prematurely when i equals 5, and only the numbers 0 through 4 are printed.

Result

Output

Example 4. Break in Do While Loop

int i = 0;
do
{
    if (i == 5)
    {
        break; // Exit the loop when i equals 5
    }
    Console.WriteLine(i); // Print the current value of i
    i++; // Increment i
} while (i <= 10);

Explanation

  1. Initialization: Start with i set to 0.
  2. Inside the Loop.
  3. Check if i is equal to 5 using the condition if (i == 5).
  4. If i is indeed equal to 5, the break statement is executed, causing the loop to exit immediately.
  5. If i is not equal to 5, the loop continues to execute, printing the value of i.
  6. After printing the value of i, it increments i by 1.
  7. Condition Check: The loop continues as long as i is less than or equal to 10.
  8. Therefore, the loop terminates prematurely when i equals 5, and only the numbers 0 through 4 are printed.

Result

Output

 

If you have any doubts or need further clarification on the break statement in C#, please leave a comment, and I'll be happy to assist..!


Recommended Free Ebook
Similar Articles