Difference Between Break and Continue Statement in C#

In this blog, I am going to explain the difference between Break and Continue Statement in C#. This detailed blog will cover the following topics as follows

  1. Introduction
  2. Jump Statements in C#
    • Types of Jump Statements in C#
  3. What is the Break Statement in C#?
    • Syntax
    • Examples
  4. What is the Continue Statement in C#?
    • Syntax
    • Examples
  5. Difference between Break and Continue Statement in C#
  6. Conclusion

Jump Statements in C#

As per Microsoft, "The jump statements unconditionally transfer control. The break statement terminates the closest enclosing iteration statement or switch statement. The continue statement starts a new iteration of the closest enclosing iteration statement. The return statement terminates execution of the function in which it appears and returns control to the caller. The goto statement transfers control to a statement that is marked by a label".

In simple words, a jump statement is used to change program control from one point to another at any time during the execution of the program.

Types of Jump Statements in C#

There are mainly five types of jump statements used in C#:

  1. Break
  2. Continue
  3. Return
  4. Goto
    • Forward jump
    • Backward jump
  5. Throw

Break Statement in C#

As per Microsoft, "The break statement terminates the closest enclosing iteration statement (that is, for, foreach, while, or do loop) or switch statement. The break statement transfers control to the statement that follows the terminated statement, if any".

In other words, "The break statement is used to end the current loop iteration or switch statement in which it appears. It changes the flow of execution of a program".

Syntax

for (int i = 0; i < 10; i++)
{
    if (i == 5)
        break;  // Exit the loop when i is 5
}

Examples

The examples given in this section demonstrate the functionality of the Break Statement in C#. Let's see.

1) Simple Break Statement

namespace BreakVsContinue
{
    internal class ForEachBreak
    {
        static void Main(string[] args)
        {
            int[] numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
            foreach (int number in numbers)
            {
                if (number == 7)
                {
                    break;
                }

                Console.Write($"{number} ");
            }
            Console.WriteLine();
            Console.Read();
        }
    }
}

Output

2) Break Statement in Nested Loop

namespace BreakVsContinue.BreakStatement
{
    internal class NestedLoop
    {
        static void Main(string[] args)
        {
            for (int outer = 1; outer <= 7; outer++)
            {
                for (int inner = 1; inner <= 7 inner++)
                {
                    if (inner > outer)
                    {
                        break;
                    }

                    Console.Write($"{inner} ");
                }
                Console.WriteLine();
            }
            Console.Read();
        }
    }
}

Output

Continue Statement in C#

As per Microsoft, "The continue statement starts a new iteration of the closest enclosing iteration statement (that is, for, foreach, while, or do loop), as the following example shows",

The continue statement is used to transfer program control to the beginning of the loop. It passes control to the next iteration of the nearest enclosing iteration statement.

Syntax

for (int i = 0; i < 10; i++)
{
    if(i == 5)
        continue;    // Skip when i is 5
    Console.WriteLine("Value of i is {0}", i);
}

Examples

The examples given in this section demonstrate the functionality of the Continue Statement in C#. Let's see.

1) Printing even numbers using the Continue statement

namespace BreakVsContinue
{
    class PrintEvenNumnbers
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 20; i++)
            {
                if (i % 2 != 0)
                    continue;
                Console.WriteLine("Value of i: {0}", i);
            }
            Console.Read();
        }
    }
}

Output

2) Continue Statement in ForEach Loop

namespace BreakVsContinue.Continue_Statement
{
    internal class ForEachContinue
    {
        static void Main(string[] args)
        {
            string[] names = { "Aadi", "Aasha", "Aarya", "Alankriti", "Aanandi" };
            foreach (var name in names)
            {
                if (name.StartsWith("Aa"))      // if the name starts with 'Aa'
                {
                    continue;                   // skip the rest
                }
                Console.WriteLine("Unique Name: {0}", name);        // if it does not start with "Aa", print the name
            }
            Console.Read();
        }
    }
}

Output

Break Vs Continue Statement in C#

The following example will show you both the Break and Continue statements working together.

namespace BreakVsContinue
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 10; i++)
            {
                if (i == 6)
                    continue;
                if (i == 10)
                    break;
                Console.WriteLine("Value of i: {0} ", i);
            }
            Console.ReadLine();
        }
    }
}

Output

The Break and Continue Statement in C# are often confusing for beginners and experienced alike, but they serve different purposes. Now, let's look at the quick difference between Break and Continue Statement in C#

S.No. Key Points Break Statement Continue Statement
1  Purpose The break statement is used to end the current loop iteration or switch statement in which it appears. The continue statement is used to transfer program control to the beginning of the loop. It passes control to the next iteration of the nearest enclosing iteration statement.
2  Syntax break; continue;
3  Control When a break statement is encountered, the control immediately exits the loop construct. Control is automatically passed to the beginning of the loop statement when the continue statement is encountered.
4  Flow The break statement exits the loop completely. The continue statement moves on to the next iteration of the loop.

See you in the next blog, till then, take care and be happy learning.

You can connect with me @

Conclusion

In this blog, we have discussed the difference between Break and Continue Statement in C#

I hope you enjoyed this blog. Follow C# Corner to learn more new and amazing things about C#.

Thanks for reading.