Ad space (banner)
Lesson 17 / 20

The switch statement

This lesson covers the switch statement, which branches on several possible values.

Writing switch (value) { case value1: ... break; ... default: ... } lets you write several branches for one value.

The sample code prints a different message depending on a number representing the day of the week.

A common early stumble is forgetting break;. Leave it out by accident and execution falls through into the next case, which is a classic source of bugs.

In professional work, switch statements are often used to change what a Flutter app displays according to its state (loading, succeeded, failed, and so on).

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Dart directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)