欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

詳解C++編程中標記語句與復合語句的寫法

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

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

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

標簽的范圍為整個函數(shù),已在其中聲明該標簽。
備注
有三種標記語句。它們?nèi)际褂妹疤枌⒛撤N標簽與語句隔開。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 語句可將控制轉(zhuǎn)移到 identifier 標簽。以下代碼片段闡釋了 goto 語句和 identifier 標簽的使用:
標簽無法獨立出現(xiàn),必須總是附加到語句。如果標簽需要獨立出現(xiàn),則必須在標簽后放置一個 null 語句。
標簽具有函數(shù)范圍,并且不能在函數(shù)中重新聲明。但是,相同的名稱可用作不同函數(shù)中的標簽。

// 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)鍵字后顯示的標簽不能在 switch 語句的外部顯示。(此限制也適用于 default 關(guān)鍵字。) 下面的代碼片段演示了 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 關(guān)鍵字后顯示的標簽不能在 switch 語句的外部顯示。(此限制也適用于 default 關(guān)鍵字。) 下面的代碼片段演示了 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 語句可將控制轉(zhuǎn)移到 identifier 標簽。以下代碼片段闡釋了 goto 語句和 identifier 標簽的使用:
標簽無法獨立出現(xiàn),必須總是附加到語句。如果標簽需要獨立出現(xiàn),則必須在標簽后放置一個 null 語句。
標簽具有函數(shù)范圍,并且不能在函數(shù)中重新聲明。但是,相同的名稱可用作不同函數(shù)中的標簽。

// 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.
}


復合語句(塊)
復合語句包含封閉在大括號 ({ }) 中的零個或多個語句??梢栽谌魏纹谕Z句出現(xiàn)的位置使用復合語句。復合語句通常稱為“塊”。
語法

{ [ statement-list ] }

備注
以下示例使用復合語句作為 if 語句的 statement 部分(有關(guān)語法的詳細信息,請參閱 if 語句):

if( Amount > 100 )
{
  cout << "Amount was too large to handle\n";
  Alert();
}
else
  Balance -= Amount;

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

上一篇:講解C++編程中Address-of運算符&amp;的作用及用法

欄    目:C語言

下一篇:C++編程中逗號運算符和條件運算符的使用方法講解

本文標題:詳解C++編程中標記語句與復合語句的寫法

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/2572.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有