Tag statement
tags are used to directly transfer program control to specific statements.
Identifier:statement case
constant-expression:statement
default:statement
The label is scoped to the entire function in which the label is declared.
Note
There are three kinds of markup statements. All of them use a colon to separate a label from the statement. Case and default tags are specific to case statements.
#include <iostream>
using namespace std;
void Test_label (int x) {
if (x = = 1) {
goto label1;
}
Goto Label2;
Label1:
cout << "in Label1" << Endl;
return;
Label2:
cout << "in Label2" << Endl;
return;
}
int main () {
Test_label (1);//In Label1
Test_label (2);//In Label2
}
Goto statement
The appearance of the identifier label in the source program declares a label. Only goto statements can transfer control to the identifier label. The following code fragment illustrates the use of goto statements and identifier tags:
Labels cannot appear independently and must always be attached to statements. If the label needs to appear independently, a null statement must be placed after the label.
The label has a function range and cannot be declared in the function. However, the same name can be used as a label in a different function.
Labels_with_goto.cpp
//compile with:/EHsc
#include <iostream>
int main () {
using namespace STD;
Goto Test2;
cout << "Testing" << Endl;
Test2:
cerr << "at Test2 label." << Endl;
}
Output:at Test2 label.
Case statement
Labels that appear after the case keyword cannot be displayed outside the switch statement. (This restriction also applies to the default keyword.) The following code fragment demonstrates the correct use of the case label:
Sample Microsoft Windows message processing loop.
Switch (msg)
{case
Wm_timer: //Process TIMER event.
Setclassword (HWnd, Gcw_hicon, ahicon[nicon++]);
ShowWindow (HWnd, sw_showna);
Nicon%=;
Yield ();
break;
Case WM_PAINT:
memset (&ps, 0x00, sizeof (PAINTSTRUCT));
HDC = BeginPaint (hWnd, &ps);
EndPaint (HWnd, &ps);
break;
Default:
//This choice is taken to all messages not specifically
//covered by a case statement.
Return DefWindowProc (hWnd, message, WParam, lParam);
break;
The label in the case statement
Labels that appear after the case keyword cannot be displayed outside the switch statement. (This restriction also applies to the default keyword.) The following code fragment demonstrates the correct use of the case label:
//Sample Microsoft Windows message processing loop. Switch (msg) {case Wm_timer://
Process Timer event.
Setclassword (HWnd, Gcw_hicon, ahicon[nicon++]);
ShowWindow (HWnd, Sw_showna);
Nicon%= 14;
Yield ();
Break
Case WM_PAINT://obtain a handle to the device context.
BeginPaint'll send WM_ERASEBKGND if appropriate.
memset (&ps, 0x00, sizeof (PAINTSTRUCT));
HDC = BeginPaint (hWnd, &ps);
Inform Windows this painting is complete.
EndPaint (HWnd, &ps);
Break
Case WM_CLOSE://close this window and all child windows.
KillTimer (HWnd, TIMER1);
DestroyWindow (HWND); if (hWnd = = hwndmain) postquitmessage (0);
Quit the application.
Break
Default://This choice is taken to all messages not specifically//covered by a case statement.
Return DefWindowProc (hWnd, message, WParam, LParam);
Break }
The label in the GOTO statement
The appearance of the identifier label in the source program declares a label. Only goto statements can transfer control to the identifier label. The following code fragment illustrates the use of goto statements and identifier tags:
Labels cannot appear independently and must always be attached to statements. If the label needs to appear independently, a null statement must be placed after the label.
The label has a function range and cannot be declared in the function. However, the same name can be used as a label in a different function.
Labels_with_goto.cpp
//compile with:/EHsc
#include <iostream>
int main () {
using namespace STD;
Goto Test2;
cout << "Testing" << Endl;
Test2:
cerr << "at Test2 label." << Endl;
At Test2 label.
}
Compound statement (block)
A compound statement contains 0 or more statements that are enclosed in braces ({}). You can use compound statements where any expected statement appears. Compound statements are often referred to as "blocks."
Grammar
Note
The following example uses a compound statement as the statement part of an if statement (for more information about syntax, see if statements):
if (Amount >)
{
cout << "Amount is too large to handle\n";
Alert ();
}
else
Balance-= Amount;
Attention
Because a declaration is a statement, the declaration can be a statement within a statement-list. Therefore, the name declared within a compound statement (rather than explicitly declared as a static name) has a local scope and (for an object) lifetime.