Dart by Example: Switch

  1. main() {
  2. // a typical switch statement
  3. var piece = 'knight';
  4. switch(piece) {
  5. case 'bishop':
  6. print('diagonal');
  7. break;
  8. case 'knight':
  9. print('L-shape');
  10. break;
  11. default:
  12. print('checkmate');
  13. }
  14. // cases can only fall through if they are empty:
  15. piece = 'queen';
  16. switch(piece) {
  17. case 'queen':
  18. case 'bishop':
  19. print('diagonal');
  20. break;
  21. }
  22. }
  23.  
  1. $ dart switch.dart
  2. L-shape
  3. diagonal

by @jryanio | source | license