詳解C++編程中標(biāo)記語句與復(fù)合語句的寫法
標(biāo)記語句
標(biāo)簽用于將程序控制權(quán)直接轉(zhuǎn)交給特定語句。
identifier : statement case constant-expression : statement default : statement
備注
有三種標(biāo)記語句。它們?nèi)际褂妹疤枌⒛撤N標(biāo)簽與語句隔開。case 和 default 標(biāo)簽特定于 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 標(biāo)簽的外觀聲明了一個標(biāo)簽。僅 goto 語句可將控制轉(zhuǎn)移到 identifier 標(biāo)簽。以下代碼片段闡釋了 goto 語句和 identifier 標(biāo)簽的使用:
標(biāo)簽無法獨(dú)立出現(xiàn),必須總是附加到語句。如果標(biāo)簽需要獨(dú)立出現(xiàn),則必須在標(biāo)簽后放置一個 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)簽的外觀聲明了一個標(biāo)簽。僅 goto 語句可將控制轉(zhuǎn)移到 identifier 標(biāo)簽。以下代碼片段闡釋了 goto 語句和 identifier 標(biāo)簽的使用:
標(biāo)簽無法獨(dú)立出現(xiàn),必須總是附加到語句。如果標(biāo)簽需要獨(dú)立出現(xiàn),則必須在標(biāo)簽后放置一個 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ù)合語句包含封閉在大括號 ({ }) 中的零個或多個語句??梢栽谌魏纹谕Z句出現(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;
注意
由于聲明是一個語句,因此聲明可以是 statement-list 內(nèi)的某個語句。因此,復(fù)合語句內(nèi)聲明的名稱(而不是顯式聲明為靜態(tài)的名稱)具有局部范圍和(對于對象)生存期。
相關(guān)文章
C++實(shí)現(xiàn)日期計算器詳細(xì)代碼示例
這篇文章主要給大家介紹了關(guān)于C++實(shí)現(xiàn)日期計算器的相關(guān)資料,基于C++編寫的簡單的日期計算器,供大家參考,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03
C++實(shí)現(xiàn)尋找最低公共父節(jié)點(diǎn)的方法
這篇文章主要介紹了C++實(shí)現(xiàn)尋找最低公共父節(jié)點(diǎn)的方法,是數(shù)據(jù)結(jié)構(gòu)中二叉樹的一個經(jīng)典算法,有一定的借鑒價值,需要的朋友可以參考下2014-09-09
C語言實(shí)現(xiàn)中綴表達(dá)式轉(zhuǎn)換為后綴表達(dá)式
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)中綴表達(dá)式轉(zhuǎn)換為后綴表達(dá)式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-04-04

