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# Continue Statement

In this tutorial, we'll learn about the continue statement in C#. The continue statement is a control statement that can be used within loops (such as for, while, and do-while). It's used to skip the current iteration of the loop and move on to the next one.

  • Understanding the continue statement

The continue statement can be helpful when you want to skip over certain conditions or operations within a loop. Instead of processing the remainder of the loop body for that iteration, the loop moves on to the next iteration immediately.

  • Using continue in a for loop

Let's take a look at an example of using the continue statement in a for loop. We'll print out only the odd numbers between 1 and 10:

using System;

namespace ContinueStatementExample
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 10; i++)
            {
                if (i % 2 == 0) // If the number is even
                {
                    continue; // Skip the current iteration
                }

                Console.WriteLine(i); // Print the odd number
            }
        }
    }
}

In this example, if the number i is even, the continue statement is executed. This causes the loop to skip the current iteration and move on to the next one, effectively printing only the odd numbers between 1 and 10.

  • Using continue in a while loop

Here's an example of using the continue statement in a while loop to print the odd numbers between 1 and 10:

using System;

namespace ContinueStatementExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 1;

            while (i <= 10)
            {
                if (i % 2 == 0)
                {
                    i++;
                    continue;
                }

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

In this example, we use a while loop instead of a for loop, but the principle remains the same: when an even number is encountered, the continue statement is executed, skipping the current iteration and moving on to the next one.

  • Using continue in a do-while loop

Finally, let's see how to use the continue statement in a do-while loop:

using System;

namespace ContinueStatementExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 1;

            do
            {
                if (i % 2 == 0)
                {
                    i++;
                    continue;
                }

                Console.WriteLine(i);
                i++;
            } while (i <= 10);
        }
    }
}

As with the other loop structures, when an even number is encountered, the continue statement is executed, skipping the current iteration and moving on to the next one.

  • Conclusion

In this tutorial, we have discussed the continue statement in C# and demonstrated its usage in various loop structures (for, while, and do-while). The continue statement can be a helpful tool for controlling the flow of your loops and skipping over specific conditions or operations. Remember to use it judiciously, as overusing it can sometimes make your code more difficult to understand and maintain.

  1. C# continue statement example:

    • Description: The continue statement in C# is used to skip the rest of the loop body and move to the next iteration.
    • Code:
      for (int i = 1; i <= 5; i++)
      {
          if (i == 3)
          {
              Console.WriteLine("Skipping iteration 3");
              continue;
          }
          Console.WriteLine($"Iteration {i}");
      }
      
  2. C# while loop with continue:

    • Description: Demonstrating the use of the continue statement in a while loop to skip iterations based on a condition.
    • Code:
      int i = 0;
      while (i < 5)
      {
          i++;
          if (i == 3)
          {
              Console.WriteLine("Skipping iteration 3");
              continue;
          }
          Console.WriteLine($"Iteration {i}");
      }
      
  3. Nested loops and continue statement in C#:

    • Description: Discussing the use of continue in nested loops to skip iterations within both the inner and outer loops.
    • Code:
      for (int i = 1; i <= 3; i++)
      {
          for (int j = 1; j <= 3; j++)
          {
              if (j == 2)
              {
                  Console.WriteLine($"Skipping iteration in inner loop ({i}, {j})");
                  continue;
              }
              Console.WriteLine($"Iteration ({i}, {j})");
          }
      }
      
  4. Continue statement in switch case C#:

    • Description: Demonstrating the use of continue in a switch statement to skip certain case blocks.
    • Code:
      int option = 2;
      switch (option)
      {
          case 1:
              Console.WriteLine("Option 1 selected");
              break;
          case 2:
              Console.WriteLine("Skipping option 2");
              continue;
          case 3:
              Console.WriteLine("Option 3 selected");
              break;
          default:
              Console.WriteLine("Invalid option");
              break;
      }