C# Break Statement

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.

Let us see the C# break statement example code.

C# Break Statement 1

C# Break Statement Example

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

using System;
class Program
  static void Main()
    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;
      case 2:
        TotalAmount += 2;
      case 3:
        TotalAmount += 3;
        Console.WriteLine("you have entered {0}, an invalid choice", choice);
        goto start;
    Console.WriteLine("Do you want to buy another one - yes or no");
    string decision = Console.ReadLine();

      case ("yes"):
        goto start;
      case ("no"):
        Console.WriteLine("your choice is invalid");
        goto decision;

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

C# Break Statement 2

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# break statement 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 following lines.

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 message “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 lines.

“Thanks for shopping with us.”

“Total Bill Amount is 5