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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-20 我要评论
想了解详解C++编程中标记语句与复合语句的写法的相关内容吗,在本文为您仔细讲解C++的标记与复合语句的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C++,标记语句,复合语句,下面大家一起来学习吧。

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

identifier : statement
case constant-expression : statement
default : 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 内的某个语句。因此,复合语句内声明的名称(而不是显式声明为静态的名称)具有局部范围和(对于对象)生存期。

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们