Switch Case - potatoscript/csharp GitHub Wiki
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.
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!
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 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.
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."
- 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.