Differences Between the Switch Statement and Switch Expression
- Expression Result: The switch statement does not return a value, while the switch expression returns a value.
- Syntax: The switch statement uses the colon (`:`) notation, while the switch expression uses the arrow (`->`) notation.
- `break` Statement: The `break` statement is required in each `case` block in the switch statement, while it is optional in the switch expression.
- `default` Block: The `default` block is required in the switch statement, while it is optional in the switch expression.
- Fall-Through: Fall-through is not allowed in the switch expression, while it is allowed in the switch statement.
- Multiple Constants: Multiple `case` constants can be in a single `case` label in the switch statement, while the switch expression does not allow this.
- Data Types: The switch statement allows primitive data types, wrapper classes, Enums, and Strings as `selector_expression`, while the switch expression only supports Enums and Strings.
- Scope: The variables defined in each `case` block in the switch expression are scoped to that block only, while they are accessible in the entire `switch` statement in the switch statement.
Read
this article