In the previous tutorials, we discuss C# if statement, C# if-else statement ,if ..else if ..else statement and nested if and else if statement . In this tutorial, we are going to learn the switch statement in C# programming. The C# language supports the switch statement or switch case statement. The switch statement is used to avoid the block of if..else.. elseif. Switch statement bases on the case. A statement can be executed on multiples condition using the C# switch statement.
switch (expression)
{
case 1:
code to be executed if the expression is matched
break;
case 2:
code to be executed if the expression is matched
break;
/*We can create many case statements*/
default:
code to be executed
if the expression does not match with any case.
}
In the above syntax, you can see that if case 1 is occurred(matched) then the first statement will print(first code will execute). If the second case is occurred(matched) then the second statement will print(second code will execute) and if no case occurred(no matched) then default code will be executed.
1. The expression used in a switch statement must have an integral or enumerated type.
2. You can create many case statements within a switch.
3. When the case is matched to variable then the case code executes until the break.
4. A break works like termination of the block.
5. In the if-else, the else part also works as a default part. In the C# switch statement, the default part works when any case does not match with switch expression.
Let's create an example of a C# switch case statement.
using System;
namespace SwitchStatement{
public class Program {
public static void Main(string[] args) {
/* local variable definition */
int day=25;
switch (day) {
case 1:
Console.WriteLine("Festival : Holi :Holiday ");
break;
case 2:
case 3:
Console.WriteLine("Festival : Diwali :Holiday ");
break;
case 25:
Console.WriteLine("Festival : Christmas day:Holiday");
break;
case 28:
Console.WriteLine("Festival : Ram Navami :Holiday");
break;
default:
Console.WriteLine("No festival or check date");
break;
}
Console.ReadLine();
}
}
}
Output-
Festival : Christmas day:Holiday
In the above C# example, we created logic for festivals using C# switch case statement. You can modify according to your need and try this example of C# console.
Let's create another example to find out the day name using C# programming with the switch case statement.
using System;
using System;
namespace SwitchStatement{
public class Program {
public static void Main(string[] args) {
/* local variable definition */
int day=2;
switch (day) {
case 1:
Console.WriteLine("The First day of the week is Mon.");
break;
case 2:
Console.WriteLine("The second day of the week is Tue.");
break;
case 3:
Console.WriteLine("The third day of the week is wed. ");
break;
case 4:
Console.WriteLine("The fourth day of the week is Thurs.");
break;
case 5:
Console.WriteLine("The fifth day of the week is Fri.");
break;
case 6:
Console.WriteLine("The sixth day of the week is Sat.");
break;
case 7:
Console.WriteLine("The seventh day of the week is Sun.");
break;
default:
Console.WriteLine("Invalid day");
break;
}
Console.WriteLine("Day of the week ={0}",day);
Console.ReadLine();
}
}
}
Output -
The second day of the week is Tue. Day of the week =2
The second day of the week is Tue.
Day of week =2
In the above example, we created a logic to get week name using the C# switch statement.