Switch Case In Dart(flutter)

Switch Case In Dart(flutter)

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 :-

 ( expression ) { 
   case value1: { 
      // Body of value1
   } break; 
   case value2: { 
      //Body of value2 
   } break; 
   .
   .
   .
   default: { 
      //Body of default case  
   } break; 
}

The default case is the case whose body is executed if none of the above cases matches the condition.

Rules to follow in switch case:

  1. There can be any number of cases. But values should not be repeated.

  2. The case statements can include only constants. It should not be a variable or an expression.

  3. There should be a flow control i.e break within cases. If it is omitted than it will show error.

  4. The default case is optional.

  5. Nested switch is also there thus you can have switch inside the switch.

THANKYOU!