The switch selection statement



Using the switch statement

If there are too many if...else statements, code can become messy and difficult to follow. In this scenario, a better solution is to use a switch statement.

In each case statement, notice the break keyword. This causes control to jump to the end of the switch after processing the block of code. If you omit the break keyword, your code will not compile.

Notice that there is an else block labeled default. This block of code will execute when none of the other blocks match.

The following sample shows how you can use a switch statement:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace UsingSwitch
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Type in a super hero's name to see his nickname:");
                string userValue = Console.ReadLine();   
                switch(userValue.ToUpper())
                {
                    case "BATMAN":
                        Console.WriteLine("Caped Crusader");
                        break;
                    case "SUPERMAN":
                        Console.WriteLine("Man of Steel");
                        break;
                    case "GREENLANTERN":
                        Console.WriteLine("Emerald Knight");
                        break;
                    default:
                        Console.WriteLine("Does not compute");
                        break;
                }
                Console.ReadLine();    
            }
        }
    }
    Output:
    Type in a super hero's name to see his nickname:
    BATMAN
    Caped Crusader

Leveraging the OR operator in a switch statement

You leveraging the OR operator in a switch statement. The case statements are stacked onto each other. If any of this case statements evaluates to true, the code following that case is processed.

The following code demostrates this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace UsingSwitchOR
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Type in a Color name:");
                string userValue = Console.ReadLine();   
                switch(userValue.ToUpper())
                {
                    case "RED":
                    case "GREEN":
                    case "BLUE":
                        Console.WriteLine("This is part of RGB !!!");
                        break;
                    case "Yellow":
                        Console.WriteLine("This isn't part of RGB !!!");
                        break;
                    default:
                        Console.WriteLine("Does not compute");
                        break;
                }
                Console.ReadLine();    
            }
        }
    }
    Output:
    Type in a Color name:
    GREEN
    This is part of RGB !!!

PROMOTIONS