The switch conditional is a control structure in C# that provides a way to execute different blocks of code based on the value of an expression.
The switch conditional allows you to evaluate the value of an expression and execute specific code blocks based on different possible values of that expression.
If you want to learn more, check out the Introduction to Programming Course
It is sometimes used as a cleaner and more readable alternative to a series of nested if-else statements. Although, many people (myself included) think that instead of improving readability, it often worsens it.
Basic Syntax
The basic syntax of a switch conditional in C# is as follows:
switch (expression)
{
case value1:
// Code to execute if expression equals value1
break;
case value2:
// Code to execute if expression equals value2
break;
...
default:
// Code to execute if expression does not match any case
break;
}
- expression: The expression whose value is evaluated in each case.
- case: Represents a specific value that is compared with the expression.
- default: Optionally, a
defaultblock can be included.
The default block will execute if none of the previous cases match the value of the expression (it is optional, but it’s most common to have it).
Let’s see a simple example where a switch conditional is used to print a number to the screen.
int number = 2;
switch (number)
{
case 1:
Console.WriteLine("One");
break;
case 2:
Console.WriteLine("Two");
break;
case 3:
Console.WriteLine("Three");
break;
default:
Console.WriteLine("Invalid number");
break;
}
In this case, depending on the value of number
- 1, 2, and 3 will print the corresponding number to the screen.
- Any other number will show “Invalid number”.
Fall-Through Between Cases
Fall-Through is the possibility of “falling” from one case to another and executing multiple code statements. Unlike other languages (like C++), “fall-through” in C# is intentionally limited.
In C#, all blocks must
- Either be empty
- Or have a
breakstatement
That is, it is not possible to do this, omitting the break in a case:
case 1:
Console.WriteLine("One");
// I cannot omit `break`
case 2:
Console.WriteLine("Two");
// more cases
It would be possible if the case statements are completely empty. Like this:
switch (number)
{
case 1:
case 2:
case 3:
Console.WriteLine("Less than 3");
break;
default:
Console.WriteLine("Greater than 3");
break;
}
Although it’s very likely a bad idea to do it 😉.
Practical Examples
Determine the Name of a Number
In this example, we use the switch structure to determine the name of a number based on its value.
int number = 2;
// We use switch to determine the name of the number based on its value
switch (number)
{
case 1:
Console.WriteLine("One");
case 2:
Console.WriteLine("Two");
break;
case 3:
Console.WriteLine("Three");
break;
default:
Console.WriteLine("Invalid number");
break;
}
Determine the Mathematical Operation Based on the Operator
This code shows how to use a switch to determine the mathematical operation based on the provided operator.
char operator = '+';
// We use switch to determine the mathematical operation based on the operator
switch (operator)
{
case '+':
Console.WriteLine("Addition");
break;
case '-':
Console.WriteLine("Subtraction");
break;
case '*':
Console.WriteLine("Multiplication");
break;
case '/':
Console.WriteLine("Division");
break;
default:
Console.WriteLine("Invalid operator");
break;
}
Determine the Name of a Weekday
This example shows how to determine the name of a weekday using a switch structure.
int day = 3;
// We use switch to determine the name of the day based on its value
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
// We add cases for the remaining days...
default:
Console.WriteLine("Invalid day");
break;
}
Determine the Name of a Month (with Enumeration)
Here, a switch is used to determine the name of a month based on its value in an enumeration.
// We define an enumeration for the months of the year
enum Months { January = 1, February, March, April, May, June, July, August, September, October, November, December }
// We declare a variable of the enumeration to represent the month
Months month = Months.May;
// We use switch to determine the name of the month based on its value in the enumeration
switch (month)
{
case Months.January:
Console.WriteLine("January");
break;
case Months.February:
Console.WriteLine("February");
break;
// We add cases for the remaining months...
default:
Console.WriteLine("Invalid month");
break;
}
These examples are intended to show how to use the Switch conditional. It does not mean it’s the best way to solve the problem they address. Usually, there are better alternatives.
