7.1: Switch Case

In this module we'll study switch statements.

File Location

  1. Right click on the solution you made in the first module.

  2. Go to Add > New Project and select Console App (.NET Framework).

  3. Name it 07_conditionals_switch.

  4. Write your code for this module within Program.cs of that project.

Description

According to the MSN docs, "a switch statement is a type of conditional that chooses a single switch section to execute from a list of candidates based on a pattern match with the match expression."

It's a bit easier to study switch statements by writing them, so lets do that now.

Switch Case

Below is an example of a switch case that you can add to the Program.cs file:

class Program
{
    static void Main(string[] args)
    {
        //1
        Console.WriteLine("What's your name?");
        string inputName = Console.ReadLine().ToLower();

        //2
        switch (inputName)
        {
            //3
            case "fred":
                Console.WriteLine("Hey Fred, let's go golfing.");
                //4
                break;
            case "karl":
                Console.WriteLine("Let's hang.");
                break;
            case "john":
                Console.WriteLine("Sorry, I'm busy right now.");
                break;
            default:
                Console.WriteLine("Hey " + inputName + ", can I call you back in a minute?");
                break;
        }

        //5
    }
}

Analysis

  1. In the example above, we ask the user their name and then store that name in inputName variable. To standardize the input, we use .ToLower() to convert the user's input to lowercase.

  2. Next, we declare a switch that takes in the inputName variable.

  3. We write out the various cases. The program looks for fred, then karl, then john, and then has a case for none, if none of these cases are the value. If the value of inputName is 'Fred' or 'fred', the switch will execute the Console statement to go golfing.

  4. The statement will break out of the switch statemetn and the program will move on to #5.

More Practice

Without looking, try making a switch case that outputs descriptions of your different friends.

It could look like this:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("What's your friend's name?");
        string friend = Console.ReadLine().ToLower();

        switch (friend)
        {       
            case "jay":
                Console.WriteLine("Jay is a great guy.");
                break;
            case "paul":
                Console.WriteLine("Paul is pretty cool.");
                break;
            case "kenn":
                Console.WriteLine("Kenn has an old soul.");
                break;
            case "carr":
                Console.WriteLine("Carr is the coolest dude ever");
                break;
            default:
                Console.WriteLine($"I don't know {friend}.");
                break;
        }
    }
}

Last updated