Break and Continue

In C#, break and continue are control flow statements that allow you to alter the execution flow of loops (such as for, while, do-while, and foreach). Both statements are crucial for managing how and when a loop terminates or skips iterations.

break Statement

The break statement terminates the nearest enclosing loop (or switch statement) and transfers control to the statement immediately following the loop or switch. This is particularly useful when you want to exit a loop based on a specific condition.

Example

Let’s consider an example where we use a break statement to exit a for loop when a specific value is encountered in an array.
using System;

class Program
{
  static void Main()
  {
      int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
      int target = 5;
      for (int i = 0; i < numbers.Length; i++)
      {
          if (numbers[i] == target)
          {
              Console.WriteLine("Target number found: " + numbers[i]);
              break;
          }
          Console.WriteLine("Processing number: " + numbers[i]);
      }
  }
}

Outlook

Processing number: 1
Processing number: 2
Processing number: 3
Processing number: 4
Target number found: 5

Explanation of break Example

  1. Initialization: We define an array numbers containing the integers 1 through 9 and set a target value target to 5.
  2. for Loop: The loop iterates over the elements of the numbers array.
  3. Condition Check: Inside the loop, we check if the current element numbers[i] is equal to the target value.
  4. Break on Condition: When the target value is found, the break statement is executed, terminating the loop.
  5. Output: The loop prints numbers up to the target and exits as soon as the target is found.

continue Statement

The continue statement skips the current iteration of the nearest enclosing loop and proceeds with the next iteration. This is useful when you want to skip certain iterations based on a condition without terminating the entire loop.

Example

Let’s consider an example where we use a continue statement to skip even numbers in an array and only process odd numbers.
using System;

class Program
{
  static void Main()
  {
      int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
      for (int i = 0; i < numbers.Length; i++)
      {
          if (numbers[i] % 2 == 0)
          {
              continue;
          }
          Console.WriteLine("Processing odd number: " + numbers[i]);
      }
  }
}

Output

Processing odd number: 1
Processing odd number: 3
Processing odd number: 5
Processing odd number: 7
Processing odd number: 9
Processing odd number: 11

Explanation of continue Example

  1. Initialization: We define an array numbers containing the integers 1 through 12.
  2. for Loop: The loop iterates over the elements of the numbers array.
  3. Condition Check: Inside the loop, we check if the current element numbers[i] is even using the modulus operator %.
  4. Continue on Condition: If the number is even, the continue statement is executed, skipping the rest of the loop body for that iteration.
  5. Output: The loop only processes and prints odd numbers.

Combining break and continue

You can also combine break and continue statements in a single loop to handle more complex control flow scenarios. Here’s an example that demonstrates both break and continue in a loop:
using System;

class Program
{
  static void Main()
  {
      int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
      int target = 8;
      for (int i = 0; i < numbers.Length; i++)
      {
          if (numbers[i] == target)
          {
              Console.WriteLine("Target number found: " + numbers[i]);
              break;
          }
          if (numbers[i] % 2 == 0)
          {
              continue;
          }
          Console.WriteLine("Processing odd number: " + numbers[i]);
      }
  }
}

Output

Processing odd number: 1
Processing odd number: 3
Processing odd number: 5
Processing odd number: 7
Target number found: 8

Explanation of Combined Example

  1. Initialization: Define the numbers array and set the target value target to 8.
  2. for Loop: Iterate over the elements of the numbers array.
  3. Break Condition: Check if the current element is the target value. If true, print the message and break the loop.
  4. Continue Condition: Check if the current element is even. If true, execute continue to skip the rest of the loop body for that iteration.
  5. Output: The loop processes odd numbers and stops when the target is found.
Post a comment

Leave a Comment

Scroll to Top