手机
当前位置:查字典教程网 >编程开发 >C语言 >详解C++编程中标记语句与复合语句的写法
详解C++编程中标记语句与复合语句的写法
摘要:标记语句标签用于将程序控制权直接转交给特定语句。identifier:statementcaseconstant-expression:st...

标记语句

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

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 handlen"; Alert(); } else Balance -= Amount;

注意

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

【详解C++编程中标记语句与复合语句的写法】相关文章:

C++中的函数指针与函数对象的总结

C++中this指针的用法及介绍

解析C++中虚析构函数的作用

C++中指向结构体变量的指针

C语言中打印特殊图案的实现代码

深入详解C编写Windows服务程序的五个步骤

C++调试追踪class成员变量的方法

C/C++中退出线程的四种解决方法

深入解析C++中的引用类型

operator new在C++中的各种写法总结

精品推荐
分类导航