Programming in Dart: Control Flow & Collections

May 3 2022 · Dart 2.15, DartPad, DartPad

Part 1: Control Flow

09. Use a Switch Statement

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 08. Understand Nested Loops Next episode: 10. Conclusion

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

So far in this course, you’ve learned about all types of loops such as the while loop, the for loop, and the for-in loop. But actually, Dart has another aspect of control flow that allows you to switch on a value.

var trafficLight = 'green';
switch (trafficLight) {

}
case 'green': 
    print('drive');
case 'green': 
    print('drive');
    break;
case 'yellow':
    print('slow down');
    break;
case 'red':
    print('stop');
    break;
    default:
      print('stop');
var trafficLight = 'flashing-yellow';
    case 'red':
    default:
      print('stop');
var trafficLight = 'red';
switch (trafficLight) {
    case 'flashing-red': 
      print('drive to line');
      break;    
redLight:
case 'red':
    print('stop');
    break;
switch (trafficLight) {
    case 'flashing-red': 
      continue redLight;    
var trafficLight = 'flashing-red';