Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

switch case

  • switch
  • case
  • break

Only works on numbers and strings. Last statement of each non-empty case must be one of: break, continue, return or throw.

main() {
  var operator = '+';
  var a = 23;
  var b = 19;
  
  var c;
  switch(operator) {
    case '+' :
      c = a + b;
      break;
    case '-' :
      c = a - b;
      break;
    default:
      throw("Unknown operator $operator");
  }
  print(c);
}