栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

详解C++编程中标记语句与复合语句的写法

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

详解C++编程中标记语句与复合语句的写法

标记语句
标签用于将程序控制权直接转交给特定语句。

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

标签的范围为整个函数,已在其中声明该标签。
备注
有三种标记语句。它们全都使用冒号将某种标签与语句隔开。case 和 default 标签特定于 case 语句。
#include  
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 
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 
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 handlen";
  alert();
}
else
  Balance -= Amount;

注意
由于声明是一个语句,因此声明可以是 statement-list 内的某个语句。因此,复合语句内声明的名称(而不是显式声明为静态的名称)具有局部范围和(对于对象)生存期。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/64673.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号