Learn switch case in C# language with simple explanation, doubts and code example.

Switch case statement in C# is also conditional statement. In switch condition e.g. switch(value), the value of variable is matched to case written in it body. Whatever case matches the value of variable, that block get executed and exit from the switch case block on break statement.

Syntax of Switch Case in C#

switch(variable){
 
 	case value-1 :
 		//Statements
 		break; 
 		
 	case value-2 :
 		//Statements
 		break;
 		
 	case value-3 :
 		//Statements
 		
 		break; 
 		//If no case match the default
 	default : 
 		//Statements
 		break;
} 

Focus-1:

Notice the colon : symbol after case value e.g. case value-2 : to start the block of a case.

Focus-2:

In a case block, you can write many statements e.g. using if else conditions, calling methods etc. Example below.

switch(variable){
	
 	case value-1 :
 		
 		//Write Statements
 		//you can write many statements
 		//You can use if else
 		//you can call methods e.g.
 		// xyz(); etc. 		
 		
 		break; 
 		
 	
 	default : 
 		//Statements
 		break;
}

 

Code Example of Switch Case in C#

In below switch case code example, value of variable var is 2. So, control will go directly to case 2: and executes its block and exit the entire switch case block on break statement. So, output of the program will be “Case -2 block”.

using System;
    public class Sample
    {

        public static void Main(String[] args) {
 
		int var = 2;
		switch (var) {
 
		case 1:
			Console.WriteLine("Case -1 block");
			break;
 
		case 2:
			Console.WriteLine("Case -2 block");
			break;
 
		case 4:
			Console.WriteLine("Case -4 block");
 
			break;
		// If no case match the default
		default:
            Console.WriteLine("Default block");
			break;
		}
 
	}
    }

If no value is matched with any case e.g. if value of variable var you set to 5. there is no case 5, so control will execute default block.

Doubt-1:

What if I remove break from case 1, as given below in code?

public class Sample
    {

        public static void Main(String[] args) {
 
		int var = 1;
		
		switch (var) {
 
		case 1:
			Console.WriteLine("Case -1 block");	
 
		case 2:
			Console.WriteLine("Case -2 block");
			break;
		
		
		default:
            Console.WriteLine("Default block");
			break;
		}
 
	}
    }

Answer: If we remove break from case 1 and if supply value of var to 1, then after compiling the code an error will raise stating that “Control cannot fall through from one case label (‘case 1:’) to another”.

Doubt-2:

What if I remove all breaks from switch case block?

using System;
    public class Sample
    {

        public static void main(String[] args) {
 
		int var = 2;
 
		switch (var) {
 
		case 1:
			Console.WriteLine("Case -1 block");			
		case 2:
			Console.WriteLine("Case -2 block");			
			
		case 3:
			Console.WriteLine("Case -3 block");			
			
		default:
            Console.WriteLine("Default block");
					
		
		}
 
	}
    }

Answer:
If you remove all breaks, compiler will throw an error stating that Control cannot fall through from one case label (‘case 1:’) to another.

Doubt-3:

DOUBT: Is it necessary to use break statement after last case e.g. default, in switch case block?

for example,

switch (var) {
 
	case 1:
		System.out.println("Case -1 block");
		break;
		
	default:
		System.out.println("Default block");
		break;
	}

Answer: must required a break statement for any case in switch. other wise control cannot flow from one control to another.
even if default case is first case or last case in switch it must required a break statement to avoid compilation error.

However, it is good to use break in default to maintain consistency. Meaning, use break, for all cases in switch. It avoids the compilation error.

Code example:
Writing case after default. So, good to use break in default.

using System;
    public class Sample
    {

        public static void main(String[] args) {
 
		int var = 1;
 
		switch (var) {
 
		case 1:
			Console.WriteLine("Case -1 block");
			break;
			
		default:
			Console.WriteLine(("Default block");
			break;
			
		case 2:
			Console.WriteLine(("Case -2 block");
			break;
		}
 
	}
    }

Related Posts