Skip to content

Commit a124d74

Browse files
author
Colin Robertson
committed
Fix ru-91 with the right file this time
1 parent c7ae9be commit a124d74

1 file changed

Lines changed: 27 additions & 25 deletions

File tree

‎docs/c-language/switch-statement-c.md‎

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@ ms.date: "11/04/2016"
44
f1_keywords: ["switch"]
55
helpviewer_keywords: ["switch keyword [C]"]
66
ms.assetid: fbede014-23bd-4ab1-8094-c8d9d9cb963a
7+
no-loc: [ switch, case, default, break ]
78
---
89
# switch Statement (C)
910

10-
The `switch` and **case** statements help control complex conditional and branching operations. The `switch` statement transfers control to a statement within its body.
11+
The **switch** and **case** statements help control complex conditional and branching operations. The **switch** statement transfers control to a statement within its body.
1112

1213
## Syntax
1314

1415
*selection-statement*:<br/>
15-
&nbsp;&nbsp;&nbsp;&nbsp;**switch (** *expression* **)** *statement*
16+
&nbsp;&nbsp;&nbsp;&nbsp;**`switch (`** *expression* **`)`** *statement*
1617

1718
*labeled-statement*:<br/>
18-
&nbsp;&nbsp;&nbsp;&nbsp;**case** *constant-expression* **:** *statement*<br/>
19-
&nbsp;&nbsp;&nbsp;&nbsp;**default :** *statement*
19+
&nbsp;&nbsp;&nbsp;&nbsp;**`case`** *constant-expression* **`:`** *statement*<br/>
20+
&nbsp;&nbsp;&nbsp;&nbsp;**`default :`** *statement*
2021

21-
Control passes to the statement whose **case** *constant-expression* matches the value of **switch (** *expression* **)**. The `switch` statement can include any number of **case** instances, but no two case constants within the same `switch` statement can have the same value. Execution of the statement body begins at the selected statement and proceeds until the end of the body or until a **break** statement transfers control out of the body.
22+
Control passes to the statement whose **case** *constant-expression* matches the value of **switch (** *expression* **)**. The **switch** statement can include any number of **case** instances. However, no two case constants within the same **switch** statement can have the same value. Execution of the statement body begins at the selected statement. It proceeds until the end of the body, or until a **break** statement transfers control out of the body.
2223

23-
Use of the `switch` statement usually looks something like this:
24+
Use of the **switch** statement usually looks something like this:
2425

2526
```C
2627
switch ( expression )
@@ -37,18 +38,18 @@ switch ( expression )
3738
}
3839
```
3940

40-
You can use the **break** statement to end processing of a particular case within the `switch` statement and to branch to the end of the `switch` statement. Without **break**, the program continues to the next case, executing the statements until a **break** or the end of the statement is reached. In some situations, this continuation may be desirable.
41+
You can use the **break** statement to end processing of a particular labeled statement within the **switch** statement. It branches to the end of the **switch** statement. Without **break**, the program continues to the next labeled statement, executing the statements until a **break** or the end of the statement is reached. This continuation may be desirable in some situations.
4142

42-
The **default** statement is executed if no **case** *constant-expression* is equal to the value of **switch (** *expression* **)**. If the **default** statement is omitted, and no **case** match is found, none of the statements in the `switch` body are executed. There can be at most one **default** statement. The **default** statement need not come at the end; it can appear anywhere in the body of the `switch` statement. A **case** or **default** label can only appear inside a `switch` statement.
43+
The **default** statement is executed if no **case** *constant-expression* is equal to the value of **switch (** *expression* **)**. If there's no **default** statement, and no **case** match is found, none of the statements in the **switch** body get executed. There can be at most one **default** statement. The **default** statement doesn't have to come at the end. It may appear anywhere in the body of the **switch** statement. A **case** or **default** label can only appear inside a **switch** statement.
4344

44-
The type of `switch` *expression* and **case** *constant-expression* must be integral. The value of each **case** *constant-expression* must be unique within the statement body.
45+
The type of **switch** *expression* and **case** *constant-expression* must be integral. The value of each **case** *constant-expression* must be unique within the statement body.
4546

46-
The **case** and **default** labels of the `switch` statement body are significant only in the initial test that determines where execution starts in the statement body. Switch statements can be nested. Any static variables are initialized before executing into any `switch` statements.
47+
The **case** and **default** labels of the **switch** statement's body are significant only in the initial test that determines where execution starts in the statement body. **switch** statements can be nested. Any static variables are initialized before executing into any **switch** statements.
4748

4849
> [!NOTE]
49-
> Declarations can appear at the head of the compound statement forming the `switch` body, but initializations included in the declarations are not performed. The `switch` statement transfers control directly to an executable statement within the body, bypassing the lines that contain initializations.
50+
> Declarations can appear at the head of the compound statement forming the **switch** body, but initializations included in the declarations are not performed. The **switch** statement transfers control directly to an executable statement within the body, bypassing the lines that contain initializations.
5051
51-
The following examples illustrate `switch` statements:
52+
The following examples illustrate **switch** statements:
5253

5354
```C
5455
switch( c )
@@ -62,7 +63,7 @@ switch( c )
6263
}
6364
```
6465

65-
All three statements of the `switch` body in this example are executed if `c` is equal to `'A'` since a **break** statement does not appear before the following case. Execution control is transferred to the first statement (`capa++;`) and continues in order through the rest of the body. If `c` is equal to `'a'`, `lettera` and `total` are incremented. Only `total` is incremented if `c` is not equal to `'A'` or `'a'`.
66+
All three statements of the **switch** body in this example are executed if `c` is equal to `'A'`, since no **break** statement appears before the following case. Execution control is transferred to the first statement (`capa++;`) and continues in order through the rest of the body. If `c` is equal to `'a'`, `lettera` and `total` are incremented. Only `total` is incremented when `c` doesn't equal `'A'` or `'a'`.
6667

6768
```C
6869
switch( i )
@@ -79,28 +80,29 @@ switch( i )
7980
}
8081
```
8182

82-
In this example, a **break** statement follows each statement of the `switch` body. The **break** statement forces an exit from the statement body after one statement is executed. If `i` is equal to -1, only `n` is incremented. The **break** following the statement `n++;` causes execution control to pass out of the statement body, bypassing the remaining statements. Similarly, if `i` is equal to 0, only `z` is incremented; if `i` is equal to 1, only `p` is incremented. The final **break** statement is not strictly necessary, since control passes out of the body at the end of the compound statement, but it is included for consistency.
83+
In this example, a **break** statement follows each statement of the **switch** body. The **break** statement forces an exit from the statement body after one statement is executed. If `i` is equal to -1, only `n` is incremented. The **break** following the statement `n++;` causes execution control to pass out of the statement body, bypassing the remaining statements. Similarly, if `i` is equal to 0, only `z` is incremented; if `i` is equal to 1, only `p` is incremented. The final **break** statement isn't strictly necessary, since control passes out of the body at the end of the compound statement. It's included for consistency.
8384

8485
A single statement can carry multiple **case** labels, as the following example shows:
8586

8687
```C
87-
case 'a' :
88-
case 'b' :
89-
case 'c' :
90-
case 'd' :
91-
case 'e' :
92-
case 'f' : hexcvt(c);
88+
switch( c )
89+
{
90+
case 'a' :
91+
case 'b' :
92+
case 'c' :
93+
case 'd' :
94+
case 'e' :
95+
case 'f' : hexcvt(c);
96+
}
9397
```
9498

9599
In this example, if *constant-expression* equals any letter between `'a'` and `'f'`, the `hexcvt` function is called.
96100

97-
**Microsoft Specific**
98-
99-
Microsoft C does not limit the number of case values in a `switch` statement. The number is limited only by the available memory. ANSI C requires at least 257 case labels be allowed in a `switch` statement.
101+
### Microsoft-specific
100102

101-
The default for Microsoft C is that the Microsoft extensions are enabled. Use the /Za compiler option to disable these extensions.
103+
Microsoft C doesn't limit the number of case values in a **switch** statement. The number is limited only by the available memory. ANSI C requires at least 257 case labels be allowed in a **switch** statement.
102104

103-
**END Microsoft Specific**
105+
The default for Microsoft C is that the Microsoft extensions are enabled. Use the [/Za](../build/reference/za-ze-disable-language-extensions.md) compiler option to disable these extensions.
104106

105107
## See also
106108

0 commit comments

Comments
 (0)