Switch Statement In C#

In our previous post, we learned about if-else statements. In this post we will discuss about switch statement which can also serve as a substitute to if-else in some scenarios.






Why & How To Use switch Keyword?


switch statement is useful where all the conditions are based on a single variable value. In If-Else statements the conditions can be of any type but in case of switch statement the conditions are set of possible values and the variable for which switch is applied is checked for those set of possible values to match. Consider the below example of using switch for a pricing of a beverage:

public void CheckDrinkAmount(string drinkName)
{
    int amount;
    switch (drinkName)
    {
        case "Coffee":
            amount = 5;
            break; //Use of break is explained below
        case "Tea":
            amount = 3;
            break;
        case "Juice":
            amount = 7;
            break;
        default:
            amount = 4;
            break;
    }
}

//How it will be implemented using if-else
public void CheckDrinkAmount(string drinkName)
{
    int amount;
    if (drinkName == "Coffee")
    {
        amount = 5;
    }
    else if (drinkName == "Tea")
    {
        amount = 3;
    }
    else if (drinkName == "Juice")
    {
        amount = 7;
    }
    else
    {
        amount = 4;
    }
}


Both if-else and switch will do the needful but here as you can see, switch statement is much more cleaner and the way .Net handles switch is different from if-else. In terms of performance switch is better than if-else, however the performance difference is not such big so as a beginner you need not to worry.

Use Of break In switch


break keyword is used as a jump statement to exit the switch statement, so that the compiler knows that the code for specific condition has been completed. Instead of break you can also use goto, return or throw keywords to exit out of the switch statement. It is must to include these jump statement after each case otherwise compiler will throw an error.

Only time we can skip these jump statement is when we have clubbed few conditions together, like in following example:

public void CheckDrinkAmount(string drinkName)
{
    int amount;
    switch (drinkName)
    {
        case "Coffee":
        case "Tea":
        case "Lemonde":
            amount = 3;
            break;
        case "Juice":
            amount = 7;
            break;
        default:
            amount = 4;
            break;
    }
}

Here in this case, switch statement will return 3 for "Coffee", "Tea" & "Lemonade", break can only be omitted if same code has to be executed for different conditions. Here "Coffee", "Tea" & "Lemonade" conditions are clubbed together to return 3.


You must be wondering if switch is better than why would anyone use if-else ever?

For this question the answer is following example. As we told you already that switch can only be used when conditions are based on set of possible values of a single variable. So let's take previous example and add another condition. Suppose we want to offer "Juice" at subsidized rates to those who are old customers.

public void CheckDrinkAmount(string drinkName, bool isOldCustomer)
{
    int amount;
    if (drinkName == "Coffee")
    {
        amount = 5;
    }
    else if (drinkName == "Tea")
    {
        amount = 3;
    }
    else if (drinkName == "Juice" && isOldCustomer)
    {
        amount = 5;
    }
    else if (drinkName == "Juice")
    {
        amount = 7;
    }
    else
    {
        amount = 4;
    }
}


Here in this scenario switch can't be used as now the condition is based on two variables drinkName and isOldCustomer, so we can't use switch as switch requires only one variable.

Let us know if you any query or concerns. Happy Reading (refer full series here - C# Tutorial Index)




Comments

Popular posts from this blog

Just Maths - Views, View Groups and Layouts

Create Android Apps - Getting Started