詳解C++編程中標志語句與復合語句的寫法。本站提示廣大學習愛好者:(詳解C++編程中標志語句與復合語句的寫法)文章只能為提供參考,不一定能成為您想要的結果。以下是詳解C++編程中標志語句與復合語句的寫法正文
標志語句
標簽用於將法式掌握權直接轉交給特定語句。
identifier : statement case constant-expression : statement default : 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 標簽的外不雅聲清楚明了一個標簽。僅 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 內的某個語句。是以,復合語句內聲明的稱號(而不是顯式聲明為靜態的稱號)具有部分規模和(關於對象)生計期。