Dart switch statement


The switch statement in Dart is a control flow statement that allows you to execute different blocks of code based on the value of a variable or an expression. It is a more organized way to handle multiple conditions compared to using a series of if-else statements, particularly when you need to evaluate the same variable against different values.

Syntax

The basic syntax of a switch statement is as follows:

switch (expression) { case value1: // Code to execute if expression equals value1 break; case value2: // Code to execute if expression equals value2 break; // You can have any number of case statements default: // Code to execute if expression matches none of the cases }

Components of a switch Statement

  • Expression: The expression is evaluated once and compared with the values in each case.
  • Case Statements: Each case checks if the expression matches a specific value. If it matches, the corresponding block of code is executed.
  • Break Statement: The break statement is used to exit the switch block. If you omit the break, execution will "fall through" to the next case.
  • Default Case: The default case is optional and is executed if none of the specified cases match the expression.

Example

Here’s a simple example to illustrate how the switch statement works:

void main() { String fruit = 'banana'; switch (fruit) { case 'apple': print('You chose apple.'); break; case 'banana': print('You chose banana.'); // This will be executed break; case 'orange': print('You chose orange.'); break; default: print('Unknown fruit.'); } }

Fall-Through Behavior

If you do not use a break statement, Dart will continue executing the subsequent case statements until it hits a break or the end of the switch. This is known as "fall-through" behavior.

Example:

void main() { int number = 2; switch (number) { case 1: print('Number is 1'); break; case 2: case 3: print('Number is either 2 or 3'); // This will be executed break; default: print('Number is something else'); } }

Using Multiple Cases

You can group multiple cases together if they should execute the same block of code:

void main() { int day = 5; switch (day) { case 1: case 2: case 3: case 4: case 5: print('It\'s a weekday.'); // This will be executed for days 1 to 5 break; case 6: case 7: print('It\'s the weekend.'); break; default: print('Invalid day.'); } }

Important Notes

  • Expression Type: The expression used in a switch statement must evaluate to an integer, string, or a compile-time constant expression.
  • Performance: In some cases, switch statements can be more efficient than multiple if-else statements, particularly when dealing with many possible conditions.
  • Readability: Using a switch statement can make your code easier to read and maintain when you have multiple values to compare against a single variable.

Conclusion

The switch statement is a powerful tool in Dart for handling multiple conditions based on the value of an expression. It enhances code readability and organization, particularly when compared to using multiple if-else statements. By understanding how to use switch, you can effectively control the flow of your Dart applications based on various input values.