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.

Transcript: 09. Use a Switch Statement

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.

A switch statement is very much like an if-statement. You provide an expression and then provide all the cases for your code to run. Now things are little different than an if statement. The expression must evaluate to a compile time constant. After which, you provide several cases.

The code flow will jump to the appropriate case and run any code in it. When you have finished your statements, you must signify the end by providing a break keyword. If you don’t add a break keyword, Dart will report an error.

Finally, you can also provide a default case. This is like the else in an if-statement. If nothing matches the previous case, the code will run a default. Let’s play around with a switch statement.

Start by opening a new page on DartPad.dev. We will start by defining a traffic light color. Let’s start by making it green.

var trafficLight = 'green';

Now we switch on the traffic light. For this, we use the switch keyword with the variable in parenthesis followed by braces. We put our cases in the braces.

switch (trafficLight) {

}

Now we add all our cases. First, we add the green case. In this case, we simply print out to drive.

case 'green': 
    print('drive');

Now we add a break statement.

case 'green': 
    print('drive');
    break;

Now we add the other cases.

case 'yellow':
    print('slow down');
    break;
case 'red':
    print('stop');
    break;

Great - we have three cases. This only works for red, green, and yellow. There are other states such as flashing yellow or flashing red. To be safe, let’s have the car stop.

    default:
      print('stop');

Since the default is the final case, we don’t have to break. Now run the program. Because the traffic light is set to green, we are told to drive. Great! Now let’s switch it to a flashing yellow.

var trafficLight = 'flashing-yellow';

Run the program. It stops just like the red light. Of course, if we wanted to car to drive slowly, we’d need to add the case. Feel free to do that.

Now notice that our red light is matching or default case. We can actually have the red light use our default case. We do this buy putting the case over the default.

    case 'red':
    default:
      print('stop');

No code can go underneath the red case for the red case to use the default case. Mind you, you can do this with any case, not just with the default case. Now change the traffic light to red.

var trafficLight = 'red';

Run the program and we are told to stop. If you need to jump around, you can use labels. Let’s add a new case for a flashing red with a little instruction.

switch (trafficLight) {
    case 'flashing-red': 
      print('drive to line');
      break;    

This doesn’t do anything just yet. We want the flashing red light to jump to the red light. First let’s define a label.

redLight:
case 'red':
    print('stop');
    break;

Now we can continue to the label.

switch (trafficLight) {
    case 'flashing-red': 
      continue redLight;    

Now we change the traffic light to a flashing red.

var trafficLight = 'flashing-red';

Run the program. Here we get the order to stop. Nice work!