To "Switch" or to "If"

Hello.

I enjoy the switch/case statement a lot! I especially like it for decreasing the number of lines my code takes so I can see more on my screen at a time.

A challenge I have for this lecture: Originally I allowed the player to type the name of the place to hack, or the word associated with the difficulty (easy, moderate, difficult). Now that we have switched :wink: to using the switch statement that converts the string input to an int, I have to find another way to reintegrate the various ways a player can access a level, other than by entering a number. I contemplated using other bools but implementing them had the opposite effect the new switch statement had (code looked dirty again) So for now, they are just commented out as little dreams…

Here is my RunMainMenu():

    void RunMainMenu(string input)  //Handle main menu input for level selection; pass level to StartGame
    {
        bool isValidLvNumber = (input == "1" || input == "2" || input == "3");

        //bool isValidLv1Entry = (input.ToLower() == "pet shop" || input.ToLower() == "easy" || input == "=^o.o^=");
        //bool isValidLv2Entry = (input.ToLower() == "college" || input.ToLower() == "moderate");
        //bool isValidLv3Entry = (input.ToLower() == "social security" || input.ToLower() == "difficult");


        if (isValidLvNumber)
        {
            level = int.Parse(input); //typecast string input to int level and pass to StartGame
            StartGame(level);
        }
        else if (input == "42") //easter egg
        {
            Terminal.WriteLine("--Don't Panic! ...but do choose again.");
        }
        else
        {
            Terminal.WriteLine("--Invalid entry. Choose a level or ");
            Terminal.WriteLine("  \"menu\" to return to the Main Menu");
            Terminal.WriteLine("  or \"help\" to view the Help Menu.");
        }
    }

I have also converted this output text to a switch statement (I like headers):

        switch (level)
        {
            case 1:
                Terminal.WriteLine("PET SHOP - Easy");
                break;
            case 2:
                Terminal.WriteLine("COLLEGE - Moderate");
                break;
            case 3:
                Terminal.WriteLine("SOCIAL SECURITY - Difficult");
                break;
        }
        Terminal.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");

I believe the if is perfect for some cases. I will still use my ifs for simpler stuff like this:

    void CheckPassword(string input, string password)                           //CHECK PASSWORD - either wrong (try again) or correct (WIN!)
    {
        if (input == password)
        {
            ShowWinScreen();
        }
        else
        {
            Terminal.WriteLine("ACCESS DENIED. Please try again.");
        }
    }

How much do you appreciate the switch? :wink:

Have a nice day :sunny:
-GK

I agree. At first I thought “I wont bother using this but ill learn it anyway” but after implementing switches I love how clean they are. They feel easier on my brain when looking at a bunch of code on a screen and after a couple of cases, the space they save really adds up compared to ‘if then’ statements.

Privacy & Terms