Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
switch-sase Statement | Conditional Statements
course content

Зміст курсу

Introduction to Dart

switch-sase Statementswitch-sase Statement

switch-sase Statement

When we have many conditions to check, using multiple if-else statements may not be convenient.

Example

dart

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:

dart

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.

dart

main.dart

  • In this example, the switch variable is dayOfWeek. The switch 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 for dayOfWeek is "Friday". If the value of dayOfWeek does not match any of the cases, the default code block is executed. In this case, the default code block is "Weekend".

What is default?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 3. Розділ 4
course content

Зміст курсу

Introduction to Dart

switch-sase Statementswitch-sase Statement

switch-sase Statement

When we have many conditions to check, using multiple if-else statements may not be convenient.

Example

dart

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:

dart

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.

dart

main.dart

  • In this example, the switch variable is dayOfWeek. The switch 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 for dayOfWeek is "Friday". If the value of dayOfWeek does not match any of the cases, the default code block is executed. In this case, the default code block is "Weekend".

What is default?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 3. Розділ 4
some-alt