thumbnail

Switch Case Statement In Dart

Published on 29 March 2020
Last Updated on 29 March 2020

Switch case is one of the control flow statements in Dart.

A switch statement can be used as an alternative for if else ladder. Multiple if else statements can be replaced with single switch statement. Using a switch case statement in place of if else statement can make program look more concise.

General syntax of switch case statement

switch (expression) {
  case a:
    {
      // statement when case is a
      statement;
    }
  break;

  case b:
    {
      // statement when case is b
      statement;
    }
  break;

  case c:
    {
      // statement when case is c
      statement;
    }
  break;

  case d:
    {
      // statement when case is d
      statement;
    }
  break;

  default:
    {
      // default statement
      statement;
    }
}

Use of break keyword in switch case statement

break keyword in switch statement blocks is compulsory or it can result in a Dart analysis error.

Only in default case, break statement is optional, otherwise in all cases break keyword is mandatory.

As shown in above program, a switch case statement makes use of break keyword to exit from the block.

Proper way to define a switch case statement

  • Default blocks in switch case statements are optional.
  • Case expressions can only include constants.
  • All case constants must be unique.
  • Data type of all case expressions must be same.
  • Dart does support empty case clause.

Example using switch case statement

printPlayerLevel(int level) {
  switch (level) {
    case 1:
      {
        print('player is level 1');
      }
      break;
    case 2:
      {
        print('player is level 2');
      }
      break;
    case 3:
      {
        print('player is level 3');
      }
      break;
    case 4:
      {
        print('player is level 4');
      }
      break;
    default:
      {
        print('player level is unknown');
      }
  }
}

// demonstrating syntax of switch case statement
main(List<String> args) {
  printPlayerLevel(3);
  printPlayerLevel(1);
  printPlayerLevel(0);
  printPlayerLevel(-10);
}

Above program produces following output:

player is level 3
player is level 1
player level is unknown
player level is unknown

Example that demonstrates empty case clause

Dart does support empty case clause. Given below is an example that demonstrates how an empty case clause can be used in Dart.

// empty case clause example
main(List<String> args) {
  var level = 1;
  switch (level) {
    case 1:
    case 2:
    default:
  }
}

However, if case block is not empty but does not include break statement then it can produce an analysis error:

// no break statement error example
main(List<String> args) {
  var level = 1;
  switch (level) {
    case 1:
    case 2: // error because of no break clause
      print('hello');
    default:
  }
}

We hope that you found this guide useful for learning switch case statements in Dart programming language.