loading

Switch Case Statement

The lengthy if-else statement chain is avoided by using a dart switch case statement. It is an if-else statement with nested conditions simplified. The variable’s value is compared to each of the several scenarios; if a match is discovered, a block of statements related to that specific case is executed.

Up until a match is found, the assigned value is compared with each case. The code block that has to be executed is identified after a match is found.

Dart Switch Case Statement Flowchart

Switch Case Statement -

Syntax

				
					switch( expression )  
{  
    case value-1:{  
  
            //statement(s)  
            Block-1;  
                                         }  
                                            break;  
    case value-2:{             
                                                          //statement(s)  
            Block-2;  
                                          }  
                                           break;  
    case value-N:{             
                                                          //statement(s)  
            Block-N;  
                                          }  
                                           break;  
    default:    {  
            //statement(s);  
                                      }  
}  
				
			

In this case, the expression can be either a character or an integer. The case labels, which are used to specifically identify each case, are represented by the values 1, 2, and n. The colon(:) must come at the end of each label.

Because the same name label can cause issues while running the software, the labels must be unique.

There is a block linked to the case label. A block is only a collection of several statements made in a specific circumstance.

Following the evaluation of the switch expression, the expression value was compared to every case that has been defined inside the switch case. Assuming the expression has a value of 2, it will compare each case until it locates the label 2 in the program.

Utilizing the break statement at the conclusion of each case is crucial. Even if a specific case is detected, if the break statement is not included, the program will continue to go through all of the cases until the end. The break statement is declared using the break keyword.

When the expression’s value does not match one of the scenarios, the default case will be carried out. Writing in the program is optional.

Example

				
					void main() {  
        int n = 3;  
        switch (n) {  
            case 1:  
                print("Value is 1");  
                break;  
            case 2:  
                print("Value is 2");  
                break;  
            case 3:  
                print("Value is 3");  
                break;  
            case 4:  
                print("Value is 4");  
                break;  
            default:  
                print("Out of range");  
                break;  
        }  
    }
				
			

Output

				
					Value is 3
				
			

Explanation

The program mentioned above initializes the variable n to the value 3. In order to compare each instance with the variable n, we built the switch case using the expression. Given that the value is 3, the case-label 3 will be executed. Case-label 3 was successfully located, and the outcome was displayed on the screen.

Example

				
					void main()  
 {  
 // declaring a interger variable   
int Roll_num =  90014;  
   
// Evalaute the test-expression to find the match  
  switch (Roll_num) {  
  case 90009:  
    print("My name is Joseph");  
    break;  
  case 90010:  
    print("My name is Peter");  
    break;  
  case 090011:  
    print("My name is Devansh");  
    break;  
  
// default block  
  default:  
    print("Roll number is not found");  
}  
}   
				
			

Output

				
					Roll number is not found
				
			

Explanation

The variable Roll_num in the program above was initialized with the value 90014. Every case that is declared inside the switch statement was examined by the switch test-expression. When the test expression was unable to locate a match in the cases, the default case statement was produced.

Benefit of Switch case

The switch case, as we covered previously, is an if nested if-else sentence simplified. The issue with nested if-else is that as the number of pathways increases, it complicates the application. The program’s complexity is decreased by the switch case. It makes the software easier to read.

Share this Doc

Switch Case Statement

Or copy link

Explore Topic