Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara switch-case Statement in Dart | Conditional Statements
Introduction to Dart

bookswitch-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

main.dart

copy
12345678910111213141516171819202122232425262728
void 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

main.dart

copy
12345678
switch(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

main.dart

copy
1234567891011121314151617181920212223
void 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 switch variable is dayOfWeek. It’s evaluated once, and the matching code block runs.
  • Once a matching case executes, the remaining cases are skipped.
  • If dayOfWeek doesn’t match any case, the default block runs, in this case, it prints "Weekend".
question mark

What is default in a switch statement?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 4

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

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

bookswitch-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

main.dart

copy
12345678910111213141516171819202122232425262728
void 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

main.dart

copy
12345678
switch(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

main.dart

copy
1234567891011121314151617181920212223
void 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 switch variable is dayOfWeek. It’s evaluated once, and the matching code block runs.
  • Once a matching case executes, the remaining cases are skipped.
  • If dayOfWeek doesn’t match any case, the default block runs, in this case, it prints "Weekend".
question mark

What is default in a switch statement?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 4
some-alt