The switch
conditional is a control structure in C# that provides a way to execute different blocks of code depending on the value of an expression.
Sometimes it is used as a cleaner and more readable alternative to a series of nested if-else
statements. Although, many people think (myself included) that instead of improving readability, it usually worsens it.
The switch
conditional allows evaluating the value of an expression and executing specific code blocks based on different possible values of that expression.
The basic syntax of a switch
conditional in C# is as follows:
switch (expression)
{
case value1:
// Code to execute if expression is equal to value1
break;
case value2:
// Code to execute if expression is equal to value2
break;
...
default:
// Code to execute if expression does not match any case
break;
}
- expression: It is the expression whose value is evaluated in each case.
- case: Represents a specific value that is compared with the expression.
Default
: Optionally, adefault
block can be included that will be executed if none of the cases match the value of the expression.
The default
block will be executed if none of the previous cases match the value of the expression. It is optional, but it is most normal to have it.
Let’s see it with a simple example where a switch
conditional is used to print a number on 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 on 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 several code statements. Unlike other languages (such as C++), “fall-through” is intentionally limited.
In C# all blocks must
- Either be empty
- Or have a
break
statement.
That is, it is not possible to do this by omitting the break
in a case,
case 1:
Console.WriteLine("One");
// I can't not put `break`
case 2:
Console.Writeline("Two");
// more cases
It would be possible if the case
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 is most likely a bad idea to do so 😉.
Practical examples
Determine the name of a number
In this example, we use the switch
structure to determine the name of a number according to its value.
int number = 2;
// We use switch to determine the name of the number according to 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 according to the operator
This code shows how to use a switch
to determine the mathematical operation according to the provided operator.
char operator = '+';
// We use switch to determine the mathematical operation according to the operator
switch (operator)
{
case '+':
Console.WriteLine("Sum");
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 day of the week
This example shows how to determine the name of a day of the week using a switch structure.
int day = 3;
// We use switch to determine the name of the day according to its value
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
// 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 according to 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 according to its value in the enumeration
switch (month)
{
case Months.January:
Console.WriteLine("January");
break;
case Months.February:
Console.WriteLine("February");
break;
// 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 that it is the best way to solve the problem they address. It is normal for there to be better alternatives