Switch Case - potatoscript/csharp GitHub Wiki

πŸ”„ Switch-Case Statements in C# πŸ”„

πŸ§‘β€πŸ’» What Are Switch-Case Statements? πŸ§‘β€πŸ’»

Imagine you're playing a game where you have to choose an action based on the number you roll on a dice. Each number (1-6) means something different, like:

  • 1 means jump,
  • 2 means run,
  • 3 means spin,
  • and so on.

In programming, a Switch-Case statement works the same way! It helps you check one value and decide what to do based on different possibilities.


🏷️ How Do Switch-Case Statements Work? 🏷️

A Switch-Case statement is a special way to handle multiple possible values for a variable. It’s like saying, β€œIf this number matches one of the options, do this action!”

In a regular If-Else statement, you have to write a lot of if and else if for each condition. But in a Switch-Case, you can check many possibilities in a much cleaner way!

πŸ§‘β€πŸ’» Basic Syntax:

switch (variable)
{
    case value1:
        // Code to run if variable == value1
        break;
    case value2:
        // Code to run if variable == value2
        break;
    case value3:
        // Code to run if variable == value3
        break;
    default:
        // Code to run if no case matches
        break;
}
  • Variable: The value you want to check, like a number or string.
  • Case: The possible values that the variable might be.
  • Break: This tells the program to stop checking once a match is found (so it doesn’t keep checking other cases).
  • Default: What happens if none of the cases match (optional, but helpful).

πŸ§‘β€πŸ’» Let’s Make a Fun Example: "What Day is It?" πŸ§‘β€πŸ’»

Let’s say we want to make a program that tells you what day of the week it is based on a number (like a calendar). For example, 1 means Monday, 2 means Tuesday, and so on. Here's how we can do that using Switch-Case.

using System;

class Program
{
    static void Main()
    {
        // The day number (1 = Monday, 2 = Tuesday, etc.)
        int day = 3; // You can change this number to test different days

        // Switch-Case to check the day number
        switch (day)
        {
            case 1:
                Console.WriteLine("It's Monday! Time to start the week! β˜€οΈ");
                break;
            case 2:
                Console.WriteLine("It's Tuesday! Keep going! πŸ’ͺ");
                break;
            case 3:
                Console.WriteLine("It's Wednesday! Halfway through the week! πŸŽ‰");
                break;
            case 4:
                Console.WriteLine("It's Thursday! Almost there! ⏳");
                break;
            case 5:
                Console.WriteLine("It's Friday! Yay! The weekend is near! πŸŽ‰");
                break;
            case 6:
                Console.WriteLine("It's Saturday! Time to relax and play! πŸ–οΈ");
                break;
            case 7:
                Console.WriteLine("It's Sunday! Rest and recharge for the week ahead! πŸ’€");
                break;
            default:
                Console.WriteLine("Oops! That's not a valid day number.");
                break;
        }
    }
}

Explanation:

  • The program checks the number (from 1 to 7) and tells you what day it is.
  • The switch statement looks at the day variable.
  • For each case, if the day number matches (like 1), it prints the message for that day.
  • If the number is outside of 1-7, the default case prints a message saying it's not a valid day.

πŸ§‘β€πŸ’» Let’s Try Another Example: "What Is Your Favorite Fruit?" πŸ§‘β€πŸ’»

Now let’s make a program that checks your favorite fruit and prints a message about it. We will use the Switch-Case statement to check different fruit options.

using System;

class Program
{
    static void Main()
    {
        // The favorite fruit
        string fruit = "apple"; // Change this to test with different fruits

        // Switch-Case to check the fruit
        switch (fruit)
        {
            case "apple":
                Console.WriteLine("Yum! Apples are delicious and crunchy! 🍏");
                break;
            case "banana":
                Console.WriteLine("Bananas are sweet and full of energy! 🍌");
                break;
            case "orange":
                Console.WriteLine("Oranges are juicy and full of vitamin C! 🍊");
                break;
            case "grape":
                Console.WriteLine("Grapes are small, sweet, and fun to snack on! πŸ‡");
                break;
            default:
                Console.WriteLine("Hmm, I don't know that fruit. What is it? 🧐");
                break;
        }
    }
}

Explanation:

  • The program checks which fruit you like (the value of the fruit variable).
  • The Switch-Case compares the fruit to each case (like "apple", "banana", etc.).
  • If the fruit matches one of the cases, it prints a fun message about that fruit.
  • If it doesn’t match any of the cases, the default message says, "Hmm, I don't know that fruit."

πŸ’‘ Why Use Switch-Case Instead of If-Else? πŸ’‘

  • Clean and Organized: If you need to check many possible values, the Switch-Case is much cleaner and easier to read than lots of If-Else statements.
  • Performance: For large numbers of conditions, Switch-Case can be faster than If-Else because the program doesn’t have to check every condition one by one.
  • Easier to Maintain: If you need to add more cases (like more days or fruits), it's simple to add another case in a Switch-Case.
⚠️ **GitHub.com Fallback** ⚠️