Introduction
The switch
statement in C# provides a way to execute one out of multiple code blocks based on the value of a variable or expression. It is a cleaner and more efficient alternative to using multiple if-else
statements when you have to compare a variable against multiple constant values.
How the switch Statement Works
The switch
statement evaluates a variable or expression and matches its value against a series of case
labels. When a match is found, the code block associated with that case
is executed. If no match is found, the default
case (if provided) is executed. The break
statement is used to terminate a case
and prevent "fall-through" to subsequent cases.
Syntax
switch (expression)
{
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
// More cases...
default:
// Code to execute if no case matches
break;
}
Example
Here’s a complete example demonstrating the use of the switch
statement in C#:
using System;
namespace SwitchStatementExample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number between 1 and 5:");
int number = Convert.ToInt32(Console.ReadLine());
// Using switch to check the value of number
switch (number)
{
case 1:
Console.WriteLine("You entered one.");
break;
case 2:
Console.WriteLine("You entered two.");
break;
case 3:
Console.WriteLine("You entered three.");
break;
case 4:
Console.WriteLine("You entered four.");
break;
case 5:
Console.WriteLine("You entered five.");
break;
default:
Console.WriteLine("Invalid number. Please enter a number between 1 and 5.");
break;
}
// This will always be executed
Console.WriteLine("Program finished.");
}
}
}
Output
For an input of 3
:
Enter a number between 1 and 5:
3
You entered three.
Program finished.
For an input of 7
:
Enter a number between 1 and 5:
7
Invalid number. Please enter a number between 1 and 5.
Program finished.
Explanation
- Expression Evaluation: The
switch
statement evaluates the expression (number
in this case). - Case Matching: The value of
number
is compared against the values specified in thecase
labels. - Executing Code: When a match is found, the corresponding code block is executed.
- Break Statement: The
break
statement terminates thecase
block, preventing fall-through to subsequent cases. - Default Case: If no match is found, the
default
case is executed (if provided).
Using switch with Strings
The switch
statement can also be used with strings. Here’s an example:
using System;
namespace SwitchStringExample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a day of the week:");
string day = Console.ReadLine();
// Using switch to check the value of day
switch (day.ToLower())
{
case "monday":
Console.WriteLine("Start of the work week.");
break;
case "friday":
Console.WriteLine("End of the work week.");
break;
case "saturday":
case "sunday":
Console.WriteLine("It's the weekend!");
break;
default:
Console.WriteLine("Invalid day.");
break;
}
// This will always be executed
Console.WriteLine("Program finished.");
}
}
}
Output
For an input of Friday
:
Enter a day of the week:
Friday
End of the work week.
Program finished.
For an input of Holiday
:
Enter a day of the week:
Holiday
Invalid day.
Program finished.
Explanation
- Case Sensitivity:
day.ToLower()
ensures the input is compared in lowercase, making theswitch
case-insensitive. - Multiple Cases: The
saturday
andsunday
cases are grouped together, allowing the same code block to execute for both values.
Conclusion
The switch
statement is used for controlling the flow of a program based on the value of a variable or expression. It is more readable and efficient than multiple if-else
statements when dealing with multiple constant values. By understanding and using the switch
statement, you can write cleaner and more maintainable code in your C# programs.