C# Break Statement

Jump statements in C# allow the control to move from one place to another place during the program execution. In general, C# supports the following jump statements.

  1. Break
  2. GoTo
  3. Continue

C# Break Statement Introduction

As we see all know, the C# break statement will let the control come out (terminate) of the switch after the match case executed without continuing execution of the cases until the end. Apart from that, C# break statement helps you exit from the loops. It is very helpful to exit the compiler from the nested for loops.

C# Break Statement 1

Let us see the break statement example code.

C# Break Statement Example

In this example, we are using the C# statement inside the switch case.

using System;
class Program
{
  static void Main()
  {
    start:
    int TotalAmount = 0;
    Console.WriteLine("Choose your pizza size: 1-small, 2-Medium, 3-Large");
    int choice = int.Parse(Console.ReadLine());

    switch (choice)
    {
      case 1:
        TotalAmount += 1;
        break;
      case 2:
        TotalAmount += 2;
        break;
      case 3:
        TotalAmount += 3;
        break;
      default:
        Console.WriteLine("you have entered {0}, an invalid choice", choice);
        goto start;
    }
    decision:
    Console.WriteLine("Do you want to buy another one - yes or no");
    string decision = Console.ReadLine();

    switch(decision.ToLower())
    {
      case ("yes"):
        goto start;
      case ("no"):
        break;
      default:
        Console.WriteLine("your choice is invalid");
        goto decision;
    }

    Console.WriteLine();
    Console.WriteLine("Thanks for shopping with us");
    Console.WriteLine("Total Bill Amount is {0}", TotalAmount);

    }
  }

OUTPUT

C# Break Statement 2

ANALYSIS

The above C# break statement code is for taking the pizza order and calculating the bill amount. The choice is an integer variable to store the pizza size, which the user has chosen, i.e., whether small-1, medium-2, large-3.

Here TotalAmount is an integer variable that initialized with 0. 

The C# Switch cases are written for calculating the bill amount. And this amount is depending on the user’s order.

If the user has given a choice that is not in any of the cases, then the default statement executed. And it again goes to the label startand executes from the beginning.

After the user has given any order, the label decision will allow the user for the next order. And if the user wants to give another order, then switch case decision executed.

If yes it will go to start and if no it quits the switch and prints the statements

Thanks for shopping with us

And it gives the total amount.

Here pizza size 2 is chosen.

It asks for the next order. It is given, yes.

So this time, pizza size 7 is chosen, which is not in any of the cases. So the statement “you have entered 7, an invalid choice”.

It will ask again to Choose your pizza size and give it as 3.

Now it will ask for another order, 

given as “hdbd” which is junk data instead of yes or no

So it is saying “your choice is invalid”.

Since it has not given relevant input, it will ask for the order again, which is this time given “no”. So it will quit from the switch and executes the statements

“Thanks for shopping with us.”

“Total Bill Amount is 5