In Dart, when we handles multiple conditions that time we can use either multiple if-else statements or we can use switch statements also.

When a variable can have multiple values and for different values it will executes different statements, in that case switch statements. A switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via search and map.

Syntax –

switch(expression){
case label-1:
            statements;
            break;
case label-2:
            statements;
            break;
default:
            statements;
}

Notes:

  • this expression can be any integer or character variable.
  • label-n -> constant integer/character.(n=0,1,2,…)
  • default block is not mandatory.
  • if ‘expression’ in switch statement get evaluated, then other case blocks will gets ready for execution. If the required value finds into cases then that block will executed.
  • if any case block does not executed, then default block will executed.
  • After every case block or even default block always end with ‘break’ statement.
  • The break statement to end processing of a particular labeled statement within the switch statement.

In the label-n we can’t use any relational, logical operators. We will discuss about this below..

Let’s understand through below code example…

Code 1:

void main(){
  int a=40;
  switch (a) {
    case 10:
      print(‘a is 10’);
      break;
    case 20:
      print(‘a is 20’);
      break;
    case 30:
      print(‘a is 30’);
      break;
    case 40:
      print(‘a is 40’);
      break;
  }
}
output

From the upper code, you see case 40 get matched. Because integer variable ‘a’ , stored value ‘40’.

** Error code, let’s see if we use relational operation in the section of label-n.

Code 2:

void main(){
  int a=40;
  switch (a) {
    case a==40:
      print(‘a is 10’);
      break;
    case a==20:
      print(‘a is 20’);
      break;
    case a==30:
      print(‘a is 30’);
      break;
    case a==40:
      print(‘a is 40’);
      break;
  }
}
output

** if you do this same job with multiple if-else statements then it will

Code 3:

import ‘dart:io’;
 
void main(){
  String? str;var a;
  // getting user input
  print(‘Enter value: ‘);
  str=stdin.readLineSync();
  if (str!=null) {
    a=int.parse(str);
  }
  // checks conditions with multi-if_else
  if (a==10) {
    print(‘a is 10’);
  }
  if (a==20) {
    print(‘a is 20’);
  }
  if (a==30) {
    print(‘a is 30’);
  }
  if (a==40) {
    print(‘a is 40’);
  }
}
output

Let’s use switch statement for string variables

Code 4:

void main(){
  String a=”Dart”;
 
  switch (a) {
    case “java”:
      print(‘lang is java’);
      break;
    case “python”:
      print(‘lang is python’);
      break;
    case “Dart”:
      print(‘lang is Dart’);
      break;
    case “C”:
      print(‘lang is C/C++’);
      break;
  }
}
output

Let’s understand uses of default block. As previous we know, when any cases not satisfies then default block get executed. Let’s see the usage below…

Code 5:

void main(){
  int a=30;
 
  switch (a) {
    case 10:
      print(‘a is 10’);
      break;
    case 20:
      print(‘a is 20’);
      break;
    default:
      print(‘a is 30’);
      print(‘default block executed!’);
      break;
  }
}
output

Let’s make a code, which tells grades according to marks. Conditions are –

  • >90  excellent
  • 70-90  very good
  • 60-70  good
  • 50-60  moderate
  • <50  need to improve.

Let’s see below code…

Code 6:

import ‘dart:io’;
 
void main(){
  String? str;
  late int a;
 
  print(‘enter value: ‘);
  str=stdin.readLineSync();
  if (str!=null) {
    a=int.parse(str);
  }
  // converts double to int using .round() function
  a=((a/10).round());
 
  switch (a) {
    case 10:
      print(‘excellent’);
      break;
    case 9:
      print(‘excellent’);
      break;
    case 8:
      print(‘very good’);
      break;
    case 7:
      print(‘very good’);
      break;
    case 6:
      print(‘good’);
      break;
    case 5:
      print(‘moderate’);
      break;
    case 4:
      print(‘need to improve’);
      break;
    default:
      print(‘need to improve’);
      break;
  }
}
output

* remember: any relational operators (> < >= <= ==) along with logical operators (AND OR NOT) also not allowed into case block value (constant variable) place.