详解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 语句
// 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 语句
// 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 语句中的标签
// Sample Microsoft Windows message processing loop.
switch( msg )
{
case WM_TIMER: // Process timer event.
SetClassWord( 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,&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,lParam );
break;
}
goto 语句中的标签
// 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( Amount > 100 )
{
cout << "Amount was too large to handlen";
Alert();
}
else
Balance -= Amount;
注意 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
