Welcome to The Coding College! In this tutorial, we’ll learn about the switch
statement in C#, one of the most useful tools for handling multiple conditions in your program.
By the end of this guide, you will:
- Understand what the
switch
statement is. - Learn its syntax and how it works.
- Explore practical examples to implement it efficiently.
Let’s get started! 🚀
What is the Switch Statement in C#?
The switch
statement in C# is a control flow statement used to execute one block of code among multiple options, based on the value of an expression. It is a cleaner alternative to writing multiple if...else
conditions when checking a single variable against multiple values.
Syntax of Switch Statement
Here is the basic syntax of a switch
statement in C#:
switch (expression)
{
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
case value3:
// Code to execute if expression == value3
break;
default:
// Code to execute if no case matches
break;
}
How It Works:
- The
expression
is evaluated once. - The value of the
expression
is compared with eachcase
. - If a match is found, the code inside that
case
block is executed. - The
break
statement exits theswitch
block. - The
default
case executes if no other cases match.
Example 1: Simple Switch Statement
Let’s see an example where we display the name of a day based on its number:
using System;
class Program
{
static void Main()
{
int day = 3;
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
case 4:
Console.WriteLine("Thursday");
break;
case 5:
Console.WriteLine("Friday");
break;
case 6:
Console.WriteLine("Saturday");
break;
case 7:
Console.WriteLine("Sunday");
break;
default:
Console.WriteLine("Invalid day number");
break;
}
}
}
Output:
Wednesday
Explanation:
- The
day
variable has the value3
. - The
switch
statement checks each case. - The case
3
matches, so it prints “Wednesday”.
Example 2: Using the Default Case
Let’s see what happens when the switch
expression does not match any case.
using System;
class Program
{
static void Main()
{
int number = 10;
switch (number)
{
case 1:
Console.WriteLine("Number is 1");
break;
case 2:
Console.WriteLine("Number is 2");
break;
default:
Console.WriteLine("Number is not 1 or 2");
break;
}
}
}
Output:
Number is not 1 or 2
Explanation:
- Since
number
is10
and there is no matching case, thedefault
block executes.
Example 3: Switch Statement Without Break
If the break
statement is omitted, the program will fall through and execute all subsequent cases.
using System;
class Program
{
static void Main()
{
int number = 2;
switch (number)
{
case 1:
Console.WriteLine("Number is 1");
case 2:
Console.WriteLine("Number is 2");
case 3:
Console.WriteLine("Number is 3");
break;
default:
Console.WriteLine("Unknown number");
break;
}
}
}
Output:
Number is 2
Number is 3
Explanation:
- The
number
is2
, so it starts executing atcase 2
. - Since there is no
break
, it also executescase 3
.
Example 4: Switch with Multiple Case Labels
You can group multiple cases together if they share the same code:
using System;
class Program
{
static void Main()
{
int number = 4;
switch (number)
{
case 1:
case 2:
case 3:
Console.WriteLine("Number is between 1 and 3");
break;
case 4:
case 5:
Console.WriteLine("Number is either 4 or 5");
break;
default:
Console.WriteLine("Number is out of range");
break;
}
}
}
Output:
Number is either 4 or 5
Explanation:
case 4
andcase 5
are grouped together and share the same code block.
Switch Statement vs. If…Else
Feature | Switch Statement | If…Else Statement |
---|---|---|
Readability | Cleaner for multiple fixed conditions | Better for complex conditions |
Performance | Faster when handling many cases | Slightly slower in some cases |
Use Case | When checking a single variable | When multiple conditions exist |
Best Practices for Using Switch
- Use Break Statements: Always include a
break
to avoid fall-through issues. - Include Default Case: The
default
ensures your program handles unexpected values. - Group Cases: Combine cases with shared logic to reduce redundancy.
- Avoid Overusing Switch: Use
switch
when dealing with fixed, known values.
Conclusion
The switch
statement in C# is a powerful tool for handling multiple conditions in a clean and organized way. By understanding its syntax and usage, you can simplify your code and improve its readability.
For more tutorials and hands-on examples, visit The Coding College—your one-stop destination to learn coding and programming.