国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

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

2020-05-23 14:10:29
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了C++編程中標記語句與復合語句的寫法,是C++入門學習中的基礎知識,需要的朋友可以參考下
 

標記語句
標簽用于將程序控制權直接轉交給特定語句。

identifier : statementcase constant-expression : statementdefault : statement
標簽的范圍為整個函數,已在其中聲明該標簽。
備注
有三種標記語句。它們全都使用冒號將某種標簽與語句隔開。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 語句可將控制轉移到 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 內的某個語句。因此,復合語句內聲明的名稱(而不是顯式聲明為靜態的名稱)具有局部范圍和(對于對象)生存期。



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 金华市| 小金县| 金山区| 靖西县| 贞丰县| 深州市| 延庆县| 阿勒泰市| 通州市| 蒲江县| 池州市| 武宣县| 临邑县| 文化| 陕西省| 汝城县| 沁源县| 岗巴县| 昌平区| 普兰县| 资兴市| 咸宁市| 通辽市| 淄博市| 象州县| 房产| 枣强县| 石棉县| 安阳县| 晋江市| 泌阳县| 湘潭县| 民乐县| 胶州市| 德庆县| 辰溪县| 车致| 姜堰市| 东源县| 安吉县| 得荣县|