In the last tutorial chapter, we saw nested else-if keyword, which we use for checking multiple conditions and execute code, whichever condition met to true first, but if there are 10 conditions, or more and last conidtion 10th one get's to true, it still checks first 9 conditions, which is not efficient way of coding, this is where switch case comes useful.
A switch statement is used to check a variable equality against list of other values. The values are called cases, and can be of any datatype. Switch Statement are useful in programming when you want to make a decision based on a variable value.
Syntax
switch (expression)
{
case expression1:
statement 1;
break;
case expression2:
statement 2;
break;
.
.
.
case expressionn:
statement n;
break;
default:
statement d;
}
The switch statement is often used as an alternative to an if-else-if construct if a single expression is tested against three or more conditions.
The switch statement evaluates the main expression and compare its value with the expression of each case (expression 1, expression 2, …). When it finds the matching value, the statements inside that case are executed.
The switch statement can include expression or variable of any data type such as string, bool, int, enum, char etc.
Example:
using System;
public class SwithcCaseProgram
{
public static void Main()
{
var dayOfWeek = DateTime.Now.DayOfWeek;
//switch case main expression
switch (dayOfWeek)
{
case DayOfWeek.Sunday:
//if condition is met, execute this
Console.WriteLine("yes, it's Sunday");
// get out of the switch statement
break;
case DayOfWeek.Monday:
Console.WriteLine("Monday");
break;
case DayOfWeek.Tuesday:
Console.WriteLine("Tuesday");
break;
case DayOfWeek.Wednesday:
Console.WriteLine("Wednesday");
break;
case DayOfWeek.Thursday:
Console.WriteLine("Thursday");
break;
case DayOfWeek.Friday:
Console.WriteLine("Friday");
break;
case DayOfWeek.Saturday:
Console.WriteLine("Saturday");
break;
}
}
}
Output:
Thursday
Break;
keyword is used to get out of the switch statement, as, once a value is matched, switch statement executes all the code inside the matched expression value, until we force it to exit the code using break;
If there is no break keyword inside switch case statement, you will get an error "Control cannot fall through from one case label (<case label>) to another.", for example this code will not execute
using System;
public class Program
{
public static void Main()
{
var caseSwitch=1;
switch (caseSwitch)
{
// The following switch section causes an error.
case 1:
Console.WriteLine("Case 1...");
// Add a break or other jump statement here.
case 2:
Console.WriteLine("... and/or Case 2");
break;
}
}
}
However, the following code is valid, because it ensures that program control can't fall through to the default switch section.
switch (caseSwitch)
{
case 1:
Console.WriteLine("Case 1...");
break;
case 2:
case 3:
Console.WriteLine("Case2");
break;
case 4:
Console.WriteLine("Case4");
default:
Console.WriteLine("Default value...");
break;
}
We can also use Enums in C# for switch case, if you are not aware about Enums, go through this article, here is the short explanation.
Enums are strongly typed names constants and it has it's own value, General syntax of it is like
enum <enum_name> {
your enumeration list
};
enum_name= name of your enumeration type & enumeration list is comma-separated list of identifiers.
By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.
Example:
public enum attandance
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday
}
Considering above enumeration list, Monday=0, Tuesday=1, Wedenesday=2, Thursday=3, Friday=4
Now, we have basic understanding of what is Enum, we can check how to work with Enums in Switch case
using System;
public class SwitchCaseEnumProgram
{
//decalre Enum
public enum Operator
{
PLUS, MINUS, MULTIPLY, DIVIDE
}
public static void Main()
{
//decalre in variables
int left=10 ;
int right=10;
//declare Enum value as PLUS
Operator op=Operator.PLUS;
//swicth case
switch (op)
{
default:
case Operator.PLUS:
Console.WriteLine("Total ="+ (left + right));
break;
case Operator.MINUS:
Console.WriteLine("Total = "+ (left - right));
break;
case Operator.MULTIPLY:
Console.WriteLine("Total = "+( left * right));
break;
case Operator.DIVIDE:
Console.WriteLine("Total = "+(left / right));
break;
}
}
}
Output:
Total =20
We can use goto
keyword in C# switch case, with the help of goto, we can run multiple switch cases at a time.
Let's take a look at an example with Goto in Switch
using System;
public class Program
{
public static void Main()
{
string val = "Gotoinswitch";
switch (val)
{
case "Switch1":
Console.WriteLine("Inside Switch1 after Goto called it.");
break;
case "Switch2":
Console.WriteLine("Switch2");
break;
case "SimpleSwitch":
Console.WriteLine("Simple Switch");
break;
case "Gotoinswitch":
Console.WriteLine("Goto in switch statement will execute next");
goto case "Switch1";
}
}
}
Output:
Goto in switch statement will execute next
Inside Switch1 after Goto called it.
Yes, you have read that right, we can have Switch case inside another switch case, which can be useful sometimes.
using System;
public class NestedSwitchCaseProgram
{
public static void Main()
{
int MainVal = 5;
switch (MainVal)
{
case 5:
Console.WriteLine(5);
//another siwtch
switch (MainVal - 1)
{
case 4:
Console.WriteLine(4);
// third switch case
switch (MainVal - 2)
{
case 3:
Console.WriteLine(3);
break;
}
break;
}
break;
case 8:
Console.WriteLine(8);
break;
case 9:
Console.WriteLine(9);
break;
default:
Console.WriteLine(100);
break;
}
}
}
Output:
5
4
3