您的位置:首页 > 编程语言 > C语言/C++

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

2016-01-15 16:36 423 查看

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

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

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ 语句