Course Content
Introduction to Dart
1. First Acquaintance with Dart
2. Variables and data types
3. Conditional statements
4. List and String
Introduction to Dart
Switch-Case statement
Switch-Case statement
When we have many conditions to check, using multiple if-else statements may not be convenient.
Example
main.dart
The code looks confusing, but we can make it more readable.
For such cases, we can use the switch-case
statement.
Switch - case statement
The switch-case
statement consists of several parts:
main.dart
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
- In this example, the
switch
variable isdayOfWeek
. Theswitch
variable is evaluated once, and the corresponding code block is executed. - If one of the conditions is met, then after executing its code block, the subsequent conditions will not be checked, and the code blocks of those conditions will not be executed either.
- In this
case
, the code block fordayOfWeek
is"Friday"
. If the value ofdayOfWeek
does not match any of the cases, thedefault
code block is executed. In thiscase
, the default code block is"Weekend"
.
What is default?
Select the correct answer
Everything was clear?
Section 3. Chapter 4