Skip to content

Commit 62f8723

Browse files
Update selection-statements.md - fixed example meaning, highlighted difference between switch statement and switch expression (#44945)
* Update selection-statements.md - fixed example meaning, highlighted difference between switch statement and switch expression Update selection-statements.md - fixed example meaning, highlighted difference between switch statement and switch expression * Update selection-statements.md Included is a description of intentional case sections that fall back if they share the same or interoperable logic. * Update docs/csharp/language-reference/statements/selection-statements.md --------- Co-authored-by: Bill Wagner <[email protected]>
1 parent 3d848a0 commit 62f8723

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

docs/csharp/language-reference/statements/selection-statements.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,21 @@ You can specify multiple case patterns for one section of a `switch` statement,
6666

6767
Within a `switch` statement, control can't fall through from one switch section to the next. As the examples in this section show, typically you use the `break` statement at the end of each switch section to pass control out of a `switch` statement. You can also use the [return](jump-statements.md#the-return-statement) and [throw](exception-handling-statements.md#the-throw-statement) statements to pass control out of a `switch` statement. To imitate the fall-through behavior and pass control to other switch section, you can use the [`goto` statement](jump-statements.md#the-goto-statement).
6868

69-
In an expression context, you can use the [`switch` expression](../operators/switch-expression.md) to evaluate a single expression from a list of candidate expressions based on a pattern match with an expression.
69+
> [!IMPORTANT]
70+
> Every *switch section* must end with a `break`, `goto` or `return`. Falling through from one switch section to the next generates a compiler error. However, multiple switch labels can be applied to the same switch section, like `case < 0:` in example above. This deliberate design choice allows for concisely handling multiple cases that share the same or interdependent logic.
71+
72+
In an expression context, you can use the [`switch` expression](../operators/switch-expression.md) to evaluate a single expression from a list of candidate expressions based on a pattern match with an expression.<br>
73+
74+
> [!IMPORTANT]
75+
> Differences between **switch expression** and **switch statement**:
76+
>
77+
> - **switch statement** is used to control the execution flow within a block of code.
78+
> - **switch expression** is typically used in contexts of value return and value assignment, often as a [expression-bodied members](../../programming-guide/statements-expressions-operators/expression-bodied-members.md).
79+
> - a **switch expression** case section cannot be empty, a **switch statement** can.
7080
7181
### Case guards
7282

73-
A case pattern may be not expressive enough to specify the condition for the execution of the switch section. In such a case, you can use a *case guard*. That is an additional condition that must be satisfied together with a matched pattern. A case guard must be a Boolean expression. You specify a case guard after the `when` keyword that follows a pattern, as the following example shows:
83+
A case pattern may not be expressive enough to specify the condition for the execution of the switch section. In such a case, you can use a *case guard*. That is an additional condition that must be satisfied together with a matched pattern. A case guard must be a Boolean expression. You specify a case guard after the `when` keyword that follows a pattern, as the following example shows:
7484

7585
:::code language="csharp" source="snippets/selection-statements/SwitchStatement.cs" id="WithCaseGuard":::
7686

0 commit comments

Comments
 (0)