C# – Switch Case

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.
A switch statement is often more concise and understandable than if…else if… else.. statements when testing multiple possible values for a single variable.
Syntax is as follows:

switch(expression) {
    case constant-expression:
	statement(s); 
	break;    
	case constant-expression:
	statement(s);
	break; 
 
   // you can have any number of case statements    
   default : // Optional       
   statement(s);      
   break;
}

There are sevaral things that have to consider while using the switch statement

  • The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.
  • You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon. The values to compare to have to be unique within each switch statement.
  • A switch statement can have an optional default case. The default case can be used for performing a task when none of the cases is true.
  • Each case has to end with a break statement unless it is an empty statement. In that case execution will continue at the case below it. The break statement can also be omitted when a return, throw or goto case statement is used.

Rules for Expressions in Switch

The switch statement is a clear way to implement selection among many options (namely, a choice among a few alternative ways for executing the code). It requires a selector, which is calculated to a certain value. The selector type could be an integer number, char, string or enum. If we want to use for example an array or a float as a selector, it will not work. For noninteger data types, we should use a series of if statements.

using System;
 class SwitchDemo {
 static void Main() {
 int i;    
 for(i=0; i<10; i++)
 switch(i) {
 case 0:
 Console.WriteLine("i is zero");
 break;
 case 1:
 Console.WriteLine("i is one");
 break; 
 case 2: 
 Console.WriteLine("i is two"); 
 break; 
 case 3:
 Console.WriteLine("i is three");
 break;
 case 4:
 Console.WriteLine("i is four"); 
 break; 
 default:
 Console.WriteLine("i is five or more");
 break; 
 } 
 } 
 }

In C#, it is an error for the statement sequence associated with one case to continue on into the next case. This is called the “no fall-through” rule. This is why case sequences end with a break statement. (You can avoid fall-through in other ways, such as by using the goto discussed later in this chapter, but break is by far the most commonly used approach.) When encountered within the statement sequence of a case, the break statement causes program flow to exit from the entire switch statement and resume at the next statement outside the switch. The default sequence also must not “fall through,” and it too usually ends with break.

Nested switch Statements

It is possible to have a switch as part of the statement sequence of an outer switch. This is called a nested switch. The case constants of the inner and outer switch can contain common values and no conflicts will arise. For example, the following code fragment is perfectly acceptable:

 switch(ch1) {
 case 'A':
 Console.WriteLine("This A is part of outer switch.");
 switch(ch2) { 
 case 'A': 
 Console.WriteLine("This A is part of inner switch");
 break;
 case 'B': // ...    }
 // 
 end of inner switch
 break; 
 case 'B': // ...
0 0 vote
Article Rating

Do you want to hire us for your Project Work? Then Contact US.
Subscribe
Notify of
guest
2 Comments
oldest
newest most voted
Inline Feedbacks
View all comments
Olen Polak
1 year ago

tҺe website іѕ really good, I enjoy your web site!

2
0
Would love your thoughts, please comment.x
()
x