Home Basics C# Switch Case Statement

C# Switch Case Statement

A Switch is a selection statement, and it will execute a single case statement from the list of multiple case statements based on the pattern match with the defined expression.

Using the switch statement in c#, we can replace the functionality of the if…else if statement. A switch is easier to read (in most cases).

Each case statement must finish with a jump statement (that can be break or goto or return).

In other words, C# does not support “fall through” from one case statement to the next (which is a common source of unexpected behaviour (bugs) in other languages).

The default label is optional. If no default case is defined, then the default behaviour is to do nothing.

When C# reaches a break keyword, it breaks out of the switch block. This will stop the execution of more code and case testing inside the block.

You can also nest switch statements, although these can get hard to read.

Here is the basic syntax of the switch statement

Table of Contents

Syntax

switch(expression)
{    
	case value1:    
		//Statements to Execute  
		break;  
	case value2:    
		//Statements to Execute  
		break; 
	case value3:    
		//Statements to Execute   
		break 
	......    
		
	default:     
		//code to be executed if all cases are not matched;    
		break;  
}

Lets see a simple example of this

Example

In this example we create a console program that will ask a user to enter a number, depending on the number we will display a message.

We use a switch statement for this

using System;

public class Program
{
	public static void Main()
	{  
		Console.WriteLine("Enter a number:");  
		int num = Convert.ToInt32(Console.ReadLine());  

		switch (num)  
		{  
			case 10: 
				Console.WriteLine("It is 10"); 
				break;  
			case 20: 
				Console.WriteLine("It is 20"); 
				break;  
			case 30: 
				Console.WriteLine("It is 30"); 
				break;  
			default: 
				Console.WriteLine("Another number");
				break;  
		}  
	}  
}

Here is a trial run

Enter a number:
20
It is 20

You can actually group cases together, lets adapt the previous example so that when the user enters 10, 20 or 30 the same message is displayed

using System;

public class Program
{
	public static void Main()
	{  
		Console.WriteLine("Enter a number:");  
		int num = Convert.ToInt32(Console.ReadLine());  

		switch (num)  
		{  
			case 10:  
			case 20: 
			case 30: 
				Console.WriteLine("You entered 10, 20 or 30"); 
				break;  
			default: 
				Console.WriteLine("Another number");
				break;  
		}  
	}  
}

 

We can use enum values with Switch case statements to perform required operations. Here is an example of this

 

using System;

public class Program
{
	public static void Main()
	{
		Season myseason = Season.Spring;
		switch (myseason)
		{
			case Season.Spring:
				Console.WriteLine("Spring");
				break;
			case Season.Summer:
				Console.WriteLine("Summer");
				break;
			case Season.Autumn:
				Console.WriteLine("Autumn");
				break;
			default:
				Console.WriteLine("Winter");
				break;
		}
		Console.WriteLine("Press Enter Key to Exit..");
		Console.ReadLine();
	}
	
	public enum Season
	{
		Spring,
		Summer,
		Autumn,
		Winter
	}
}

 

You may also like