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, this programming language 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 is executed without continuing execution of the cases until the end. Apart from that, the 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 Syntax

C# Break Statement Example

In this C# example, we are using the break 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 the user has chosen, i.e., whether small-1, medium-2, or large-3.

Here, the TotalAmount is an integer variable that will initialize with 0.

The C# break statement Switch cases are written to calculate the bill amount. And this amount depends on the user’s order.

The default statement will execute if the user has given a choice that is not in any of the cases. And it again goes to the label start and 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, a switch case decision will execute.

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, we chose the pizza size as 2.

It asks for the next order. Let us give yes.

So this time, we chose the pizza size 7, 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”

Categories C#