Switches in Flutter Dart

When if/else statements have too many blocks, it is easier to use switch/case statements. switch/case statement in Dart looks just like JavaScript syntax.

How does switch-case statement work in dart

  • The expression is evaluated once and compared with each case value.
  • If expression matches with case value1, the statements of case value1 are executed. Similarly, case value 2 will be executed if the expression matches case value2. If the expression matches the case value3, the statements of case value3 are executed.
  • The break keywords tell dart to exit the switch statement because the statements in the case block are finished.
  • If there is no match, default statements are executed.

 

Example:

The switch statement evaluates an expression, matches the expression’s value to a case clause and executes the statements associated with that case.

Following is the syntax.

switch(variable_expression) { 

   case constant_expr1: { 

      // statements; 

   } 

   break; 

   case constant_expr2: { 

      //statements; 

   } 

   break; 

   default: { 

      //statements;  

   }

   break; 

 

The value of the variable_expression is tested against all cases in the switch. If the variable matches one of the cases, the corresponding code block is executed. If no case expression matches the value of the variable_expression, the code within the default block is associated.

The following rules apply to a switch statement −

  • There can be any number of case statements within a switch.
  • The case statements can include only constants. It cannot be a variable or an expression.
  • The data type of the variable_expression and the constant expression must match.
  • Unless you put a break after each block of code, the execution flows into the next block.
  • The case expression must be unique.
  • The default block is optional.

The flow diagram of the switch…case statement is as follows −

 

Example - switch…case

void main() { 

   var grade = "A"; 

   switch(grade) { 

      case "A": {  print("Excellent"); } 

      break; 

      case "B": {  print("Good"); } 

      break; 

  case "C": {  print("Fair"); } 

      break; 

      case "D": {  print("Poor"); } 

      break; 

default: { print("Invalid choice"); } 

      break; 

   } 

The example verifies the value of the variable grade against the set of constants (A, B, C, D, and E) and executes the corresponding blocks. If the value in the variable doesn’t match any of the constants mentioned above, the default block will be executed.

The following output is displayed on successful execution on the above code.

Excellent

 

Example

main(List<String> args) {

  int day = 5;

  switch(age) {

    case 1:

      print('Sunday.');

      break;

    case 2:

print('Monday.');

      break;

    case 3:

      print('Tuesday');

      break;

    case 4:

      print('Wednesday');

      break;

    case 5:

      print('Thursday');

      break;

case 6:

      print('Friday');

      break;

case 7:

      print('Saturday');

      break;

  }

}

 


Advertisements