欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解C++編程中標(biāo)記語句與復(fù)合語句的寫法

 更新時(shí)間:2016年01月15日 16:36:51   投稿:goldensun  
這篇文章主要介紹了C++編程中標(biāo)記語句與復(fù)合語句的寫法,是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下

標(biāo)記語句
標(biāo)簽用于將程序控制權(quán)直接轉(zhuǎn)交給特定語句。

identifier : statement
case constant-expression : statement
default : statement

標(biāo)簽的范圍為整個(gè)函數(shù),已在其中聲明該標(biāo)簽。
備注
有三種標(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)簽的外觀聲明了一個(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ù)合語句包含封閉在大括號 ({ }) 中的零個(gè)或多個(gè)語句??梢栽谌魏纹谕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;

注意
由于聲明是一個(gè)語句,因此聲明可以是 statement-list 內(nèi)的某個(gè)語句。因此,復(fù)合語句內(nèi)聲明的名稱(而不是顯式聲明為靜態(tài)的名稱)具有局部范圍和(對于對象)生存期。

相關(guān)文章

最新評論