Switch statement in C#

In C#, the switch statement is a decision-making statement that evaluates an expression and executes different blocks of code based on the value of the expression. It provides a concise way to handle multiple cases or conditions.

Here's the basic syntax of a switch statement in C#:


switch (expression)
{
    case value1:
        // Code to be executed if expression matches value1
        break;
    case value2:
        // Code to be executed if expression matches value2
        break;
    // Additional cases
    default:
        // Code to be executed if expression does not match any case
        break;
}

The expression is evaluated once, and its value is compared against the values specified in the case labels. When a match is found, the corresponding block of code is executed until a break statement is encountered, which exits the switch statement.

Here's an example of a switch statement in C#:


int day = 3;
string dayName;

switch (day)
{
    case 1:
        dayName = "Monday";
        break;
    case 2:
        dayName = "Tuesday";
        break;
    case 3:
        dayName = "Wednesday";
        break;
    case 4:
        dayName = "Thursday";
        break;
    case 5:
        dayName = "Friday";
        break;
    case 6:
        dayName = "Saturday";
        break;
    case 7:
        dayName = "Sunday";
        break;
    default:
        dayName = "Invalid day";
        break;
}

Console.WriteLine("Day: " + dayName);

In this example, the switch statement evaluates the value of the variable "day" and assigns the corresponding day name to the variable "dayName". Since the value of "day" is 3, it matches the case label case 3:, and the value "Wednesday" is assigned to "dayName".
The output will be:


Day: Wednesday

If none of the case labels match the value of the expression, the code block under the default label is executed. In this case, if the value of "day" were, for example, 9, the output would be "Invalid day".

Difference between 'if' and 'switch' statement

The "if" statement and the "switch" statement are both decision-making constructs in C#, but they differ in their usage and the scenarios they are best suited for.

The main differences between the "if" statement and the "switch" statement are as follows:

  1. Usage: The "if" statement is used when you have to evaluate a condition that can be expressed as a Boolean expression. It allows for arbitrary conditions and complex branching logic. On the other hand, the "switch" statement is used when you have to evaluate a single expression against multiple constant values or specific cases.
  2. Condition Evaluation: The "if" statement evaluates a condition that results in either true or false. It allows for more flexible conditions, including relational and logical operators, making it suitable for complex conditions. The "switch" statement evaluates an expression once and compares its value against the constant values specified in the case labels.
  3. Multiple Conditions: The "if" statement allows you to check multiple conditions using "else if" or nested "if" statements, creating an "if-else if" ladder. Each condition is evaluated independently. The "switch" statement handles multiple cases by providing separate cases with corresponding constant values that are compared against the expression.
  4. Execution Flow: In an "if" statement, the code block associated with the first true condition is executed, and subsequent conditions are not evaluated. In a "switch" statement, the code block associated with the matched case is executed, and then the execution continues until a break statement is encountered or the switch statement is exited.
  5. Value Comparisons: The "if" statement allows for more complex value comparisons and conditions using relational and logical operators. The "switch" statement compares a single expression against constant values, which are typically integers, characters, or strings.

In general, use the "if" statement when you have complex conditions or when you need to perform different actions based on various conditions. Use the "switch" statement when you have a single expression and need to perform different actions based on specific constant values or cases. The choice between the two depends on the specific requirements and readability of the code.