You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The scope of a label is the entire function in which it's declared.
19
19
20
20
## Remarks
21
21
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.
23
23
24
24
```cpp
25
25
#include<iostream>
@@ -47,13 +47,13 @@ int main() {
47
47
}
48
48
```
49
49
50
-
**The goto statement**
50
+
## Labels and the `goto` statement
51
51
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:
53
53
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.
55
55
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.
57
57
58
58
```cpp
59
59
// labels_with_goto.cpp
@@ -72,9 +72,9 @@ int main() {
72
72
//Output: At Test2 label.
73
73
```
74
74
75
-
**The case statement**
75
+
## Labels in the `case` statement
76
76
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:
78
78
79
79
```cpp
80
80
// Sample Microsoft Windows message processing loop.
@@ -93,45 +93,7 @@ switch( msg )
93
93
EndPaint( hWnd, &ps );
94
94
break;
95
95
96
-
default:
97
-
// This choice is taken for all messages not specifically
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
-
132
96
case WM_CLOSE:
133
-
// Close this window and all child windows.
134
-
135
97
KillTimer( hWnd, TIMER1 );
136
98
DestroyWindow( hWnd );
137
99
if ( hWnd == hWndMain )
@@ -141,37 +103,12 @@ switch( msg )
141
103
default:
142
104
// This choice is taken for all messages not specifically
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
-
intmain() {
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
-
174
111
## See also
175
112
176
-
[Overview of C++ Statements](../cpp/overview-of-cpp-statements.md)<br/>
0 commit comments