Summary: in this tutorial, you’ll learn how to use the Dart switch
statement to do something when an expression equals a value.
Introduction to the Dart switch statement
The switch
statement evaluates an expression and compares its result with a value in a set. If they are equal, the switch
statement will execute the statement in the matching case
branch.
Internally, the switch
statement uses the comparison operator (==) to compare integer, string, enumeration, or compile-time constants.
The following illustrates the syntax of the switch
statement:
switch (expression)
{
case value1:
// statement1;
break;
case value2:
// statement2;
break;
case value3:
// statement3;
break;
default:
// statementn;
break;
}
Code language: Dart (dart)
In this syntax, the switch
statement compares the result of the expression
with value1
, value2
, value3
, … in each case
clause from top to bottom.
If the result of the expression
equals a value
, the switch
statement executes the statement in the matched case branch.
If the expression
doesn’t equal any value, the switch
statement executes the statement in the default
branch.
The default
clause is optional. If you omit it and the expression
doesn’t match any value
, the switch
statement doesn’t execute any statement and passes the control to the statement after it.
The following flowchart illustrates how the switch
statement works:
Dart switch statement example
The following program uses the switch
statement to display the day name based on the day number:
void main() {
int dayNumber = 3;
String dayName = "";
switch (dayNumber) {
case 1:
dayName = "Sunday";
break;
case 2:
dayName = "Monday";
break;
case 3:
dayName = "Tuesday";
break;
case 4:
dayName = "Wednesday";
break;
case 5:
dayName = "Thursday";
break;
case 6:
dayName = "Friday";
break;
case 7:
dayName = "Saturday";
break;
default:
dayName = "Invalid day";
break;
}
print(dayName);
}
Code language: Dart (dart)
How it works.
First, declare the variable dayNumber
and initialize its value to 3. Also declare the variable dayName
and initialize its values to an empty string:
int dayNumber = 3;
String dayName = "";
Code language: JavaScript (javascript)
Second, use a switch statement to assign a day name to the dayName
variable based on the value of the dayNumber
:
switch (dayNumber) {
case 1:
dayName = "Sunday";
break;
case 2:
dayName = "Monday";
break;
case 3:
dayName = "Tuesday";
break;
case 4:
dayName = "Wednesday";
break;
case 5:
dayName = "Thursday";
break;
case 6:
dayName = "Friday";
break;
case 7:
dayName = "Saturday";
break;
default:
dayName = "Invalid day";
break;
}
Code language: JavaScript (javascript)
Third, display the value of the day name.
In this example, the dayNumber
is 3. Therefore, the switch
statement assigns the string "Tuesday"
to the dayName
.
If you change the dayNumber
to a value that is not between 1 and 7, the switch
statement will execute the default
branch that assigns the "Invalid day"
to the dayName
:
Dart switch statement with group cases
If you want to execute the same statement based on multiple values, you can group cases as follows:
switch (expression)
{
case value1:
case value2:
// statement1
break;
case value3:
case value4:
// statement2
break;
default:
// statement3
break;
}
Code language: Dart (dart)
In this syntax, the switch
statement will execute statement1
if the result of the expression
equals the value1
or value2
. Similarly, it’ll execute the statement2
if the result of the expression
equals the value3
and value4
.
If the result of the expression
doesn’t equal any values in the case
clauses, it’ll execute the statment n in the default
clause.
The following example uses the switch
statement with group cases to determine whether a day is a weekday or weekend:
void main() {
String dayName = "Monday";
String day = "";
switch (dayName) {
case "Monday":
case "Tuesday":
case "Wednesday":
case "Thursday":
case "Friday":
day = "Weekday";
break;
case "Saturday":
case "Sunday":
day = "Weekend";
break;
default:
day = "Invalid";
}
print(day);
}
Code language: Dart (dart)
How it works.
First, declare the dayName
variable and initialize it with the string "Monday"
:
String dayName = "Monday";
Code language: Dart (dart)
Second, use a switch
statement with group cases to determine if the day name is a weekday or weekend. If the dayName
is from Monday to Friday, assign the "Weekday"
to the day
variable.
If the day is Saturday or Sunday, assign the "Weekend"
to the day
variable. If the day
is not from Monday to Sunday, assing "invalid"
to the day in the default
branch.
String day = "";
switch (dayName) {
case "Monday":
case "Tuesday":
case "Wednesday":
case "Thursday":
case "Friday":
day = "Weekday";
break;
case "Saturday":
case "Sunday":
day = "Weekend";
break;
default:
day = "Invalid";
break;
}
Code language: Dart (dart)
Third, display the value of the day
variable:
print(day);
Code language: Dart (dart)
Summary
- Use the
switch
statement to execute a block if the result of an expression equals a value. - If the expression doesn’t equal any value in a set, the
switch
statement will execute thedefault
branch. Thedefault
branch is optional. - The switch statement uses the comparison operator (
==
) to compare integers, strings, and constants. - Use group cases to execute the same statement that corresponds to multiple values.