標(biāo)記語句
標(biāo)簽用于將程序控制權(quán)直接轉(zhuǎn)交給特定語句。
identifier : statementcase constant-expression : statementdefault : statement
#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 標(biāo)簽的外觀聲明了一個(gè)標(biāo)簽。僅 goto 語句可將控制轉(zhuǎn)移到 identifier 標(biāo)簽。以下代碼片段闡釋了 goto 語句和 identifier 標(biāo)簽的使用:
標(biāo)簽無法獨(dú)立出現(xiàn),必須總是附加到語句。如果標(biāo)簽需要獨(dú)立出現(xiàn),則必須在標(biāo)簽后放置一個(gè) null 語句。
標(biāo)簽具有函數(shù)范圍,并且不能在函數(shù)中重新聲明。但是,相同的名稱可用作不同函數(shù)中的標(biāo)簽。
// 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 關(guān)鍵字后顯示的標(biāo)簽不能在 switch 語句的外部顯示。(此限制也適用于 default 關(guān)鍵字。) 下面的代碼片段演示了 case 標(biāo)簽的正確用法:
// 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 語句中的標(biāo)簽
在 case 關(guān)鍵字后顯示的標(biāo)簽不能在 switch 語句的外部顯示。(此限制也適用于 default 關(guān)鍵字。) 下面的代碼片段演示了 case 標(biāo)簽的正確用法:
// 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 語句中的標(biāo)簽
源程序中 identifier 標(biāo)簽的外觀聲明了一個(gè)標(biāo)簽。僅 goto 語句可將控制轉(zhuǎn)移到 identifier 標(biāo)簽。以下代碼片段闡釋了 goto 語句和 identifier 標(biāo)簽的使用:
標(biāo)簽無法獨(dú)立出現(xiàn),必須總是附加到語句。如果標(biāo)簽需要獨(dú)立出現(xiàn),則必須在標(biāo)簽后放置一個(gè) null 語句。
標(biāo)簽具有函數(shù)范圍,并且不能在函數(shù)中重新聲明。但是,相同的名稱可用作不同函數(shù)中的標(biāo)簽。
// 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.}
復(fù)合語句(塊)
復(fù)合語句包含封閉在大括號(hào) ({ }) 中的零個(gè)或多個(gè)語句。可以在任何期望語句出現(xiàn)的位置使用復(fù)合語句。復(fù)合語句通常稱為“塊”。
語法
{ [ statement-list ] }備注
以下示例使用復(fù)合語句作為 if 語句的 statement 部分(有關(guān)語法的詳細(xì)信息,請參閱 if 語句):
if( Amount > 100 ){ cout << "Amount was too large to handle/n"; Alert();}else Balance -= Amount;注意
由于聲明是一個(gè)語句,因此聲明可以是 statement-list 內(nèi)的某個(gè)語句。因此,復(fù)合語句內(nèi)聲明的名稱(而不是顯式聲明為靜態(tài)的名稱)具有局部范圍和(對于對象)生存期。
新聞熱點(diǎn)
疑難解答