Skip to content

Commit 9266fc7

Browse files
authored
Merge pull request #2824 from MicrosoftDocs/master
4/16/2020 AM Publish
2 parents 0e4feb3 + c766aac commit 9266fc7

File tree

5 files changed

+51
-51
lines changed

5 files changed

+51
-51
lines changed

docs/c-language/range-of-char-values.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ ms.assetid: 15ae9781-ec21-4333-bba8-6d2383bbf7f1
55
---
66
# Range of char Values
77

8-
**ANSI 3.2.1.1** Whether a "plain" **char** has the same range of values as a **signed char** or an `unsigned char`
8+
**ANSI 3.2.1.1** Whether a "plain" **char** has the same range of values as a **signed char** or an **unsigned char**
99

1010
All signed character values range from -128 to 127. All unsigned character values range from 0 to 255.
1111

12-
The /J compiler option changes the default from **signed** to `unsigned`.
12+
The [`/J`](../build/reference/j-default-char-type-is-unsigned.md) compiler option changes the default type for **char** from **signed char** to **unsigned char**.
1313

1414
## See also
1515

docs/c-language/truncation-of-floating-point-values.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.assetid: 051a6e22-c636-4af8-9ac4-40160f4affca
88

99
**ANSI 3.2.1.4** The direction of truncation or rounding when a floating-point number is converted to a narrower floating-point number
1010

11-
When an underflow occurs, the value of a floating-point variable is rounded down to zero. An overflow may cause a run-time error or it may produce an unpredictable value, depending on the optimizations specified.
11+
When an underflow occurs, the value of a floating-point variable is rounded to zero. An overflow may cause a run-time error or it may produce an unpredictable value, depending on the optimizations specified.
1212

1313
## See also
1414

docs/c-language/values.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The **float** type contains 32 bits: 1 for the sign, 8 for the exponent, and 23
1111

1212
The **double** type contains 64 bits: 1 for the sign, 11 for the exponent, and 52 for the mantissa. Its range is +/- 1.7E308 with at least 15 digits of precision.
1313

14-
The **long double** type contains 80 bits: 1 for the sign, 15 for the exponent, and 64 for the mantissa. Its range is +/- 1.2E4932 with at least 19 digits of precision. Note that with the Microsoft C compiler, the representation of type **long double** is identical to type **double**.
14+
The **long double** type is distinct, but has the same representation as type **double** in the Microsoft C compiler.
1515

1616
## See also
1717

docs/cpp/switch-statement-cpp.md

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,74 @@
11
---
2-
title: "switch Statement (C++)"
3-
ms.date: "05/06/2019"
2+
title: "switch statement (C++)"
3+
description: "Reference to the Standard C++ switch statement in Microsoft Visual Studio C++."
4+
ms.date: "04/15/2020"
45
f1_keywords: ["default_cpp", "switch_cpp", "case_cpp"]
56
helpviewer_keywords: ["switch keyword [C++]", "case keyword [C++], in switch statements", "default keyword [C++]"]
7+
no-loc: [switch, case, default, break, while]
68
ms.assetid: 6c3f3ed3-5593-463c-8f4b-b33742b455c6
79
---
8-
# switch Statement (C++)
10+
# switch statement (C++)
911

1012
Allows selection among multiple sections of code, depending on the value of an integral expression.
1113

1214
## Syntax
1315

14-
```
15-
switch ( init; expression )
16-
case constant-expression : statement
17-
[default : statement]
18-
```
16+
> **`switch (`** \[*initialization* **`;`**] *expression* **`)`**\
17+
> **`{`**\
18+
>     **`case`** *constant-expression* **`:`** *statement*\
19+
>     \[**`default :`** *statement*]\
20+
> **`}`**
1921
2022
## Remarks
2123

22-
The *expression* must be of an integral type or of a class type for which there is an unambiguous conversion to integral type. Integral promotion is performed as described in [Standard Conversions](standard-conversions.md).
24+
The *expression* must have an integral type, or be a class type that has an unambiguous conversion to integral type. Integral promotion takes place as described in [Standard conversions](standard-conversions.md).
2325

24-
The **switch** statement body consists of a series of **case** labels and an optional **default** label. No two constant expressions in **case** statements can evaluate to the same value. The **default** label can appear only once. The labeled statements are not syntactic requirements, but the **switch** statement is meaningless without them. 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.
26+
The **switch** statement body consists of a series of **case** labels and an optional **default** label. Collectively, the statements that follow the labels are called *labeled* statements. The labeled statements aren't syntactic requirements, but the **switch** statement is meaningless without them. No two constant expressions in **case** statements may evaluate to the same value. The **default** label may appear only once. The **default** statement is often placed at the end, but it can appear anywhere in the body of the **switch** statement. A **case** or **default** label can only appear inside a **switch** statement.
2527

26-
The *constant-expression* in each **case** label is converted to the type of *expression* and compared with *expression* for equality. Control passes to the statement whose **case** *constant-expression* matches the value of *expression*. The resulting behavior is shown in the following table.
28+
The *constant-expression* in each **case** label is converted to the type of *expression*. Then, it's compared with *expression* for equality. Control passes to the statement whose **case** *constant-expression* matches the value of *expression*. The resulting behavior is shown in the following table.
2729

28-
### Switch Statement Behavior
30+
### Switch statement behavior
2931

30-
|Condition|Action|
31-
|---------------|------------|
32-
|Converted value matches that of the promoted controlling expression.|Control is transferred to the statement following that label.|
33-
|None of the constants match the constants in the **case** labels; a **default** label is present.|Control is transferred to the **default** label.|
34-
|None of the constants match the constants in the **case** labels; **default** label is not present.|Control is transferred to the statement after the **switch** statement.|
32+
| Condition | Action |
33+
|--|--|
34+
| Converted value matches that of the promoted controlling expression. | Control is transferred to the statement following that label. |
35+
| None of the constants match the constants in the **case** labels; a **default** label is present. | Control is transferred to the **default** label. |
36+
| None of the constants match the constants in the **case** labels; no **default** label is present. | Control is transferred to the statement after the **switch** statement. |
3537

36-
If a matching expression is found, control is not impeded by subsequent **case** or **default** labels. The [break](../cpp/break-statement-cpp.md) statement is used to stop execution and transfer control to the statement after the **switch** statement. Without a **break** statement, every statement from the matched **case** label to the end of the **switch**, including the **default**, is executed. For example:
38+
If a matching expression is found, execution can continue through later **case** or **default** labels. The [`break`](../cpp/break-statement-cpp.md) statement is used to stop execution and transfer control to the statement after the **switch** statement. Without a **break** statement, every statement from the matched **case** label to the end of the **switch**, including the **default**, is executed. For example:
3739

3840
```cpp
3941
// switch_statement1.cpp
4042
#include <stdio.h>
4143

4244
int main() {
43-
char *buffer = "Any character stream";
44-
int capa, lettera, nota;
45+
const char *buffer = "Any character stream";
46+
int uppercase_A, lowercase_a, other;
4547
char c;
46-
capa = lettera = nota = 0;
48+
uppercase_A = lowercase_a = other = 0;
4749

4850
while ( c = *buffer++ ) // Walks buffer until NULL
4951
{
5052
switch ( c )
5153
{
5254
case 'A':
53-
capa++;
55+
uppercase_A++;
5456
break;
5557
case 'a':
56-
lettera++;
58+
lowercase_a++;
5759
break;
5860
default:
59-
nota++;
61+
other++;
6062
}
6163
}
62-
printf_s( "\nUppercase a: %d\nLowercase a: %d\nTotal: %d\n",
63-
capa, lettera, (capa + lettera + nota) );
64+
printf_s( "\nUppercase A: %d\nLowercase a: %d\nTotal: %d\n",
65+
uppercase_A, lowercase_a, (uppercase_A + lowercase_a + other) );
6466
}
6567
```
6668

67-
In the above example, `capa` is incremented if `c` is an uppercase `A`. The **break** statement after `capa++` terminates execution of the **switch** statement body and control passes to the **while** loop. Without the **break** statement, execution would "fall through" to the next labeled statement, so that `lettera` and `nota` would also be incremented. A similar purpose is served by the **break** statement for `case 'a'`. If `c` is a lowercase `a`, `lettera` is incremented and the **break** statement terminates the **switch** statement body. If `c` is not an `a` or `A`, the **default** statement is executed.
69+
In the above example, `uppercase_A` is incremented if `c` is an uppercase `'A'`. The **break** statement after `uppercase_A++` terminates execution of the **switch** statement body and control passes to the **while** loop. Without the **break** statement, execution would "fall through" to the next labeled statement, so that `lowercase_a` and `other` would also be incremented. A similar purpose is served by the **break** statement for `case 'a'`. If `c` is a lowercase `'a'`, `lowercase_a` is incremented and the **break** statement terminates the **switch** statement body. If `c` isn't an `'a'` or `'A'`, the **default** statement is executed.
6870

69-
**Visual Studio 2017 and later:** (available with [/std:c++17](../build/reference/std-specify-language-standard-version.md)) The `[[fallthrough]]` attribute is specified in the C++17 standard. It can be used in a **switch** statement as a hint to the compiler (or to anyone reading the code) that fall-through behavior is intended. The Microsoft C++ compiler currently does not warn on fallthrough behavior, so this attribute has no effect on compiler behavior. Note that the attribute is applied to an empty statement within the labeled statement; in other words the semicolon is necessary.
71+
**Visual Studio 2017 and later:** (available with [/std:c++17](../build/reference/std-specify-language-standard-version.md)) The `[[fallthrough]]` attribute is specified in the C++17 standard. You can use it in a **switch** statement. It's a hint to the compiler, or anyone who reads the code, that fall-through behavior is intentional. The Microsoft C++ compiler currently doesn't warn on fallthrough behavior, so this attribute has no effect on compiler behavior. In the example, the attribute gets applied to an empty statement within the unterminated labeled statement. In other words, the semicolon is necessary.
7072

7173
```cpp
7274
int main()
@@ -94,7 +96,7 @@ int main()
9496
}
9597
```
9698

97-
**Visual Studio 2017 version 15.3 and later** (available with [/std:c++17](../build/reference/std-specify-language-standard-version.md)): A switch statement may introduce and initialize a variable whose scope is limited to the block of the switch statement:
99+
**Visual Studio 2017 version 15.3 and later** (available with [/std:c++17](../build/reference/std-specify-language-standard-version.md)). A switch statement may have an *initialization* clause. It introduces and initializes a variable whose scope is limited to the block of the switch statement:
98100

99101
```cpp
100102
switch (Gadget gadget(args); auto s = gadget.get_status())
@@ -107,7 +109,7 @@ int main()
107109
};
108110
```
109111
110-
An inner block of a **switch** statement can contain definitions with initializations as long as they are reachable that is, not bypassed by all possible execution paths. Names introduced using these declarations have local scope. For example:
112+
An inner block of a **switch** statement can contain definitions with initializations as long as they're *reachable*, that is, not bypassed by all possible execution paths. Names introduced using these declarations have local scope. For example:
111113
112114
```cpp
113115
// switch_statement2.cpp
@@ -142,16 +144,14 @@ int main(int argc, char *argv[])
142144
}
143145
```
144146

145-
A **switch** statement can be nested. In such cases, **case** or **default** labels associate with the closest **switch** statement that encloses them.
147+
A **switch** statement can be nested. When nested, the **case** or **default** labels associate with the closest **switch** statement that encloses them.
146148

147-
**Microsoft Specific**
149+
### Microsoft-specific behavior
148150

149-
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.
151+
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.
150152

151153
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.
152154

153-
**END Microsoft Specific**
154-
155155
## See also
156156

157157
[Selection Statements](../cpp/selection-statements-cpp.md)<br/>

0 commit comments

Comments
 (0)