Programming in Dart: Control Flow & Collections

May 3 2022 Dart 2.15, DartPad, DartPad

Part 1: Control Flow

9. Use a Switch Statement

Episode complete

Play next episode

Next
Save for later
About this episode
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 8. Understand Nested Loops Next episode: 10. Conclusion

This video Use a Switch Statement was last updated on May 3 2022

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

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';