What is "case" in Dart?

Mon, Dec 12, 2022

Read in 3 minutes

In this post we take a look at "case" keyword with some example codes.

In Dart, the case keyword is used as part of a switch statement to define a specific value or set of values that will be matched against the switch expression. The case keyword is followed by the value or values that are being matched, and a colon (:) to indicate the code block that should be executed if the value or values match.

Here’s the syntax for the case keyword in Dart:

switch (expression) {
  case value1:
    // code to execute if expression == value1
    break;
  case value2:
    // code to execute if expression == value2
    break;
  ...
  default:
    // code to execute if expression doesn't match any case
}

The expression is the value being evaluated in the switch statement, while the case statements define the specific values or ranges of values that the expression can match. When the expression matches a case value, the code block following that case statement is executed, until a break statement is encountered.

Here are a few examples to help illustrate how case works in Dart:

Example 1: A simple switch statement with three cases:

int day = 3;
switch (day) {
  case 1:
    print('Monday');
    break;
  case 2:
    print('Tuesday');
    break;
  case 3:
    print('Wednesday');
    break;
  default:
    print('Invalid day');
    break;
}

In this example, the day variable is evaluated in the switch statement. When day is equal to 3, the case 3 statement matches and the code block following that statement is executed. The output of this code would be 'Wednesday'.

Example 2: A switch statement with multiple cases and a default:

String fruit = 'banana';
switch (fruit) {
  case 'apple':
  case 'banana':
    print('This is a fruit.');
    break;
  case 'carrot':
    print('This is a vegetable.');
    break;
  default:
    print('I do not know what this is.');
    break;
}

In this example, the fruit variable is evaluated in the switch statement. The case 'apple' and case 'banana' statements match when fruit is equal to either of those values, and the code block following those statements is executed. The output of this code would be 'This is a fruit.'.

Example 3: A switch statement with a range of values:

int age = 22;
switch (age) {
  case 0:
  case 1:
  case 2:
  case 3:
    print('You are a toddler.');
    break;
  case 4:
  case 5:
    print('You are a preschooler.');
    break;
  case 6:
  case 7:
  case 8:
  case 9:
  case 10:
    print('You are in elementary school.');
    break;
  default:
    print('You are not a child.');
    break;
}

In this example, the age variable is evaluated in the switch statement. The case 0, case 1, case 2, and case 3 statements match when age is within that range of values, and the code block following those statements is executed. The output of this code would be 'You are a toddler.'.

In summary, the case keyword in Dart is used to match specific values or ranges of values in a `switch`


Shohruh AK



Category:

Dart

Tags:



See Also

What is "break" in Dart?
What is "async" in Dart?
What is "assert" in Dart?
What is "as" in Dart language?
What is "abstract" in Dart language?