Skip to content

Commit 33c49bc

Browse files
author
Colin Robertson
authored
Correct labeled statements for cpp-docs 3428 (#3814)
1 parent d8070eb commit 33c49bc

File tree

1 file changed

+20
-83
lines changed

1 file changed

+20
-83
lines changed

docs/cpp/labeled-statements.md

Lines changed: 20 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
---
2-
description: "Learn more about: Labeled Statements"
3-
title: "Labeled Statements"
4-
ms.date: "11/04/2016"
2+
description: "Learn more about: Labeled statements"
3+
title: "Labeled statements"
4+
ms.date: 10/04/2021
55
helpviewer_keywords: ["labeled statement", "statements, labeled"]
6-
ms.assetid: 456a26d5-0fcc-4d1a-b71f-fa9ff3d73b91
76
---
8-
# Labeled Statements
7+
# Labeled statements
98

109
Labels are used to transfer program control directly to the specified statement.
1110

12-
```
13-
identifier : statement
14-
case constant-expression : statement
15-
default : statement
16-
```
11+
## Syntax
1712

18-
The scope of a label is the entire function in which it is declared.
13+
> *`labeled-statement`*:\
14+
>   *`identifier`* **`:`** *`statement`*\
15+
>   **`case`** *`constant-expression`* **`:`** *`statement`*\
16+
>   **`default`** **`:`** *`statement`*
17+
18+
The scope of a label is the entire function in which it's declared.
1919

2020
## Remarks
2121

22-
There are three types of labeled statements. All use a colon to separate some type of label from the statement. The case and default labels are specific to case statements.
22+
There are three types of labeled statements. All use a colon (**`:`**) to separate some type of label from the statement. The **`case`** and **`default`** labels are specific to case statements.
2323

2424
```cpp
2525
#include <iostream>
@@ -47,13 +47,13 @@ int main() {
4747
}
4848
```
4949
50-
**The goto statement**
50+
## Labels and the `goto` statement
5151
52-
The appearance of an *identifier* label in the source program declares a label. Only a [goto](../cpp/goto-statement-cpp.md) statement can transfer control to an *identifier* label. The following code fragment illustrates use of the **`goto`** statement and an *identifier* label:
52+
The appearance of an *`identifier`* label in the source program declares a label. Only a [`goto`](../cpp/goto-statement-cpp.md) statement can transfer control to an *`identifier`* label. The following code fragment illustrates use of the **`goto`** statement and an *`identifier`* label:
5353
54-
A label cannot appear by itself but must always be attached to a statement. If a label is needed by itself, place a null statement after the label.
54+
A label can't appear by itself but must always be attached to a statement. If a label is needed by itself, place a null statement after the label.
5555
56-
The label has function scope and cannot be redeclared within the function. However, the same name can be used as a label in different functions.
56+
The label has function scope and can't be redeclared within the function. However, the same name can be used as a label in different functions.
5757
5858
```cpp
5959
// labels_with_goto.cpp
@@ -72,9 +72,9 @@ int main() {
7272
//Output: At Test2 label.
7373
```
7474

75-
**The case statement**
75+
## Labels in the `case` statement
7676

77-
Labels that appear after the **`case`** keyword cannot also appear outside a **`switch`** statement. (This restriction also applies to the **`default`** keyword.) The following code fragment shows the correct use of **`case`** labels:
77+
Labels that appear after the **`case`** keyword can't also appear outside a **`switch`** statement. (This restriction also applies to the **`default`** keyword.) The following code fragment shows the correct use of **`case`** labels:
7878

7979
```cpp
8080
// Sample Microsoft Windows message processing loop.
@@ -93,45 +93,7 @@ switch( msg )
9393
EndPaint( hWnd, &ps );
9494
break;
9595

96-
default:
97-
// This choice is taken for all messages not specifically
98-
// covered by a case statement.
99-
100-
return DefWindowProc( hWnd, Message, wParam, lParam );
101-
break;
102-
}
103-
```
104-
105-
## Labels in the case statement
106-
107-
Labels that appear after the **`case`** keyword cannot also appear outside a **`switch`** statement. (This restriction also applies to the **`default`** keyword.) The following code fragment shows the correct use of **`case`** labels:
108-
109-
```cpp
110-
// Sample Microsoft Windows message processing loop.
111-
switch( msg )
112-
{
113-
case WM_TIMER: // Process timer event.
114-
SetClassWord( hWnd, GCW_HICON, ahIcon[nIcon++] );
115-
ShowWindow( hWnd, SW_SHOWNA );
116-
nIcon %= 14;
117-
Yield();
118-
break;
119-
120-
case WM_PAINT:
121-
// Obtain a handle to the device context.
122-
// BeginPaint will send WM_ERASEBKGND if appropriate.
123-
124-
memset( &ps, 0x00, sizeof(PAINTSTRUCT) );
125-
hDC = BeginPaint( hWnd, &ps );
126-
127-
// Inform Windows that painting is complete.
128-
129-
EndPaint( hWnd, &ps );
130-
break;
131-
13296
case WM_CLOSE:
133-
// Close this window and all child windows.
134-
13597
KillTimer( hWnd, TIMER1 );
13698
DestroyWindow( hWnd );
13799
if ( hWnd == hWndMain )
@@ -141,37 +103,12 @@ switch( msg )
141103
default:
142104
// This choice is taken for all messages not specifically
143105
// covered by a case statement.
144-
145106
return DefWindowProc( hWnd, Message, wParam, lParam );
146107
break;
147108
}
148109
```
149110

150-
## Labels in the goto statement
151-
152-
The appearance of an *identifier* label in the source program declares a label. Only a [goto](../cpp/goto-statement-cpp.md) statement can transfer control to an *identifier* label. The following code fragment illustrates use of the **`goto`** statement and an *identifier* label:
153-
154-
A label cannot appear by itself but must always be attached to a statement. If a label is needed by itself, place a null statement after the label.
155-
156-
The label has function scope and cannot be redeclared within the function. However, the same name can be used as a label in different functions.
157-
158-
```cpp
159-
// labels_with_goto.cpp
160-
// compile with: /EHsc
161-
#include <iostream>
162-
int main() {
163-
using namespace std;
164-
goto Test2;
165-
166-
cout << "testing" << endl;
167-
168-
Test2:
169-
cerr << "At Test2 label." << endl;
170-
// At Test2 label.
171-
}
172-
```
173-
174111
## See also
175112

176-
[Overview of C++ Statements](../cpp/overview-of-cpp-statements.md)<br/>
177-
[switch Statement (C++)](../cpp/switch-statement-cpp.md)
113+
[Overview of C++ statements](../cpp/overview-of-cpp-statements.md)\
114+
[`switch` statement (C++)](../cpp/switch-statement-cpp.md)

0 commit comments

Comments
 (0)