switch-case Statement in Dart
When there are many conditions to check, using multiple if-else statements can become inconvenient.
In this example, the code checks the value of the dayOfWeek variable and prints a message for the matching day. If it’s one of the weekdays, it prints the day’s name; otherwise, it prints "Weekend".
main.dart
12345678910111213141516171819202122232425262728void main() { String dayOfWeek = "Friday"; if (dayOfWeek == "Monday") { print("Today is Monday."); } else if (dayOfWeek == "Tuesday") { print("Today is Tuesday."); } else if (dayOfWeek == "Wednesday") { print("Today is Wednesday."); } else if (dayOfWeek == "Thursday") { print("Today is Thursday."); } else if (dayOfWeek == "Friday") { print("Today is Friday."); } else { print("Weekend"); } }
The code may look confusing, but it can be made more readable using a switch-case statement.
A switch-case statement includes several parts: the switch keyword, multiple case options to compare values, and an optional default block that runs if no case matches.
main.dart
12345678switch(expresion) { case value_1: // code to be executed case value_2: // code to be executed ............. default: // code to be executed if all cases are not matched }
-
switch(expression): evaluates the value of expression; -
case value_1: if expression equals value_1, the corresponding code runs; -
default: executes if none of the case values match.
A switch-case statement is a construct that allows you to execute a block of code based on the value of a variable. The variable is called the switch variable. The switch variable is evaluated once, and the corresponding block of code is executed.
main.dart
1234567891011121314151617181920212223void main() { String dayOfWeek = "Monday"; switch (dayOfWeek) { case "Monday": print("Today is Monday."); case "Tuesday": print("Today is Tuesday."); case "Wednesday": print("Today is Wednesday."); case "Thursday": print("Today is Thursday."); case "Friday": print("Today is Friday."); default: print("Weekend"); } }
- In this example, the
switchvariable isdayOfWeek. It’s evaluated once, and the matching code block runs. - Once a matching case executes, the remaining cases are skipped.
- If
dayOfWeekdoesn’t match any case, thedefaultblock runs, in this case, it prints"Weekend".
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Can you show me an example of a switch-case statement for this scenario?
What are the advantages of using switch-case over if-else statements?
Are there any limitations to using switch-case statements?
Awesome!
Completion rate improved to 4.55
switch-case Statement in Dart
Scorri per mostrare il menu
When there are many conditions to check, using multiple if-else statements can become inconvenient.
In this example, the code checks the value of the dayOfWeek variable and prints a message for the matching day. If it’s one of the weekdays, it prints the day’s name; otherwise, it prints "Weekend".
main.dart
12345678910111213141516171819202122232425262728void main() { String dayOfWeek = "Friday"; if (dayOfWeek == "Monday") { print("Today is Monday."); } else if (dayOfWeek == "Tuesday") { print("Today is Tuesday."); } else if (dayOfWeek == "Wednesday") { print("Today is Wednesday."); } else if (dayOfWeek == "Thursday") { print("Today is Thursday."); } else if (dayOfWeek == "Friday") { print("Today is Friday."); } else { print("Weekend"); } }
The code may look confusing, but it can be made more readable using a switch-case statement.
A switch-case statement includes several parts: the switch keyword, multiple case options to compare values, and an optional default block that runs if no case matches.
main.dart
12345678switch(expresion) { case value_1: // code to be executed case value_2: // code to be executed ............. default: // code to be executed if all cases are not matched }
-
switch(expression): evaluates the value of expression; -
case value_1: if expression equals value_1, the corresponding code runs; -
default: executes if none of the case values match.
A switch-case statement is a construct that allows you to execute a block of code based on the value of a variable. The variable is called the switch variable. The switch variable is evaluated once, and the corresponding block of code is executed.
main.dart
1234567891011121314151617181920212223void main() { String dayOfWeek = "Monday"; switch (dayOfWeek) { case "Monday": print("Today is Monday."); case "Tuesday": print("Today is Tuesday."); case "Wednesday": print("Today is Wednesday."); case "Thursday": print("Today is Thursday."); case "Friday": print("Today is Friday."); default: print("Weekend"); } }
- In this example, the
switchvariable isdayOfWeek. It’s evaluated once, and the matching code block runs. - Once a matching case executes, the remaining cases are skipped.
- If
dayOfWeekdoesn’t match any case, thedefaultblock runs, in this case, it prints"Weekend".
Grazie per i tuoi commenti!