Easy Tutorial
❮ Csharp Text Files Csharp Generic ❯
# C# `switch Statement`

[C# Decision](csharp-decision.html)

A **switch** statement allows testing a variable against multiple values. Each value is called a case, and the variable being tested is checked against each **switch case**.

## Syntax

The syntax for a **switch** statement in C#:

switch(expression){ case constant-expression : statement(s); break; case constant-expression : statement(s); break;

/* You can have any number of case statements */
default : /* Optional */
   statement(s);
   break;

}


**switch** statements must follow these rules:

- The **expression** in a **switch** statement must be an integral or enumerated type, or a class type with a single conversion function to an integral or enumerated type.

- A switch can have any number of case statements. Each case is followed by the value to compare and a colon.

- The **constant-expression** for a case must be the same data type as the variable in the switch, and it must be a constant.

- When the variable being tested equals the constant in a case, the statements following that case will execute until a **break** statement is encountered.

- When a **break** statement is encountered, the switch terminates, and the control flow jumps to the next line following the switch statement.

- Not every case needs to contain a **break**. If a case statement is empty, it can omit the **break**, and the control flow will *continue* to subsequent cases until a break is encountered.

- C# does not allow execution to continue from one case label to the next. If a case statement has been executed, it must include a **break** or other jump statement.

- A **switch** statement can have an optional **default** statement at the end. The default statement is used to perform a task when none of the cases are true. The default case should also include a **break** statement, which is a good practice.

- C# does not support explicit fall-through from one case label to another. To make C# support explicit fall-through from one case label to another, you can use `goto` to a switch-case or `goto default`.

## Flowchart

## Example

The following example determines the current day of the week:

using System;

namespace MyApplication {   class Program   {     static void Main(string[] args)     {       int day = 4;       switch (day)       {         case 1:           Console.WriteLine("Monday");           break;         case 2:           Console.WriteLine("Tuesday");           break;         case 3:           Console.WriteLine("Wednesday");           break;         case 4:           Console.WriteLine("Thursday");           break;         case 5:           Console.WriteLine("Friday");           break;         case 6:           Console.WriteLine("Saturday");           break;         case 7:           Console.WriteLine("Sunday");           break;       }         }   } }


The execution result varies depending on the current date. The result for this day is:

Thursday


The following example determines a student's grade, including the **default** statement:

using System;

namespace DecisionMaking {         class Program     {

```csharp
static void Main(string[] args)
{
    /* Local variable definition */
    char grade = 'B';

    switch (grade)
    {
        case 'A':
            Console.WriteLine("Excellent!");
            break;
        case 'B':
        case 'C':
            Console.WriteLine("Well done");
            break;
        case 'D':
            Console.WriteLine("You passed");
            break;
        case 'F':
            Console.WriteLine("Better try again");
            break;
        default:
            Console.WriteLine("Invalid grade");
            break;
    }
    Console.WriteLine("Your grade is {0}", grade);
    Console.ReadLine();
}

When the above code is compiled and executed, it produces the following result:

Well done
Your grade is B

C# Decision

❮ Csharp Text Files Csharp Generic ❯