詳解C++編程中標記語句與復合語句的寫法
標記語句
標簽用于將程序控制權直接轉交給特定語句。
identifier : statement case constant-expression : statement default : statement
備注
有三種標記語句。它們全都使用冒號將某種標簽與語句隔開。case 和 default 標簽特定于 case 語句。
#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 語句
源程序中 identifier 標簽的外觀聲明了一個標簽。僅 goto 語句可將控制轉移到 identifier 標簽。以下代碼片段闡釋了 goto 語句和 identifier 標簽的使用:
標簽無法獨立出現,必須總是附加到語句。如果標簽需要獨立出現,則必須在標簽后放置一個 null 語句。
標簽具有函數范圍,并且不能在函數中重新聲明。但是,相同的名稱可用作不同函數中的標簽。
// 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 語句
在 case 關鍵字后顯示的標簽不能在 switch 語句的外部顯示。(此限制也適用于 default 關鍵字。) 下面的代碼片段演示了 case 標簽的正確用法:
// 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:
memset( &ps, 0x00, sizeof(PAINTSTRUCT) );
hDC = BeginPaint( hWnd, &ps );
EndPaint( hWnd, &ps );
break;
default:
// This choice is taken for all messages not specifically
// covered by a case statement.
return DefWindowProc( hWnd, Message, wParam, lParam );
break;
}
case 語句中的標簽
在 case 關鍵字后顯示的標簽不能在 switch 語句的外部顯示。(此限制也適用于 default 關鍵字。) 下面的代碼片段演示了 case 標簽的正確用法:
// 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 will send WM_ERASEBKGND if appropriate.
memset( &ps, 0x00, sizeof(PAINTSTRUCT) );
hDC = BeginPaint( hWnd, &ps );
// Inform Windows that 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 for all messages not specifically
// covered by a case statement.
return DefWindowProc( hWnd, Message, wParam, lParam );
break;
}
goto 語句中的標簽
源程序中 identifier 標簽的外觀聲明了一個標簽。僅 goto 語句可將控制轉移到 identifier 標簽。以下代碼片段闡釋了 goto 語句和 identifier 標簽的使用:
標簽無法獨立出現,必須總是附加到語句。如果標簽需要獨立出現,則必須在標簽后放置一個 null 語句。
標簽具有函數范圍,并且不能在函數中重新聲明。但是,相同的名稱可用作不同函數中的標簽。
// 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.
}
復合語句(塊)
復合語句包含封閉在大括號 ({ }) 中的零個或多個語句。可以在任何期望語句出現的位置使用復合語句。復合語句通常稱為“塊”。
語法
{ [ statement-list ] }
備注
以下示例使用復合語句作為 if 語句的 statement 部分(有關語法的詳細信息,請參閱 if 語句):
if( Amount > 100 )
{
cout << "Amount was too large to handle\n";
Alert();
}
else
Balance -= Amount;
注意
由于聲明是一個語句,因此聲明可以是 statement-list 內的某個語句。因此,復合語句內聲明的名稱(而不是顯式聲明為靜態(tài)的名稱)具有局部范圍和(對于對象)生存期。

