语句是程序组成的基本单位,必须是以“;”分号结束。
执行语句是为了完成某个操作或修改某个数据。语句的执行有一定的结果,但没有值(Statements are executed for their effect, and do not have values.)。
C++语句将按顺序执行,除非表达式语句、选择语句、迭代语句或跳转语句特意修改了顺序。
C++语句包含如下类型:
1、标记语句
2、表达式语句
3、空语句
4、复合语句
5、选择语句
6、迭代语句
7、跳转语句
8、声明语句
9、异常处理语句
5.1 标记语句
标记语句用于将程序控制权直接转交给特定语句。
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
}
5.1.1 goto语句
源程序中identifier标签的外观声明了一个标签。仅goto语句可将控制转移到identifier标签。以下代码片段阐释了goto语句和identifier标签的使用:
// 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;
}
标签无法独立出现,必须总是附加到语句。如果标签需要独立出现,则必须在标签后放置一个null语句。
标签具有函数范围,并且不能在函数中重新声明。 但是,相同的名称可用作不同函数中的标签。
goto语句的作用是从goto语句无条件的跳转到同一函数内的另一条语句。label是用于标识一条语句的标示符。和switch语句类似,goto语句不能将程序的控制权从变量的作用域之外转移到作用于之内。
goto label;
....
lable :
statement
goto 语句无条件传输控件到由指定的标识符标记的语句。
goto identifier;
identifier 指定的该标记语句必须位于当前函数。所有identifier名称是内部命名空间的成员,因此它们不影响其他标识符。
语句标签仅对 goto 语句有意义;否则,语句标签被忽略。标签不能重新声明。
尽可能使用 break、continue 和 return 语句来代替 goto 语句是好的编程样式。但是,因为break语句仅从循环的一个级别退出,可能必须使用 goto 语句退出一个深层嵌套循环。
// goto_statement.cpp
#include <stdio.h>
int main()
{
int i, j;
for ( i = 0; i < 10; i++ )
{
printf_s( "Outer loop executing. i = %d\n", i );
for ( j = 0; j < 2; j++ )
{
printf_s( " Inner loop executing. j = %d\n", j );
if ( i == 3 )
goto stop;
}
}
// This message does not print:
printf_s( "Loop exited. i = %d\n", i );
stop:
printf_s( "Jumped to stop. i = %d\n", i );
}
5.1.2 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;
}
5.1.3 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;
}
5.1.4 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.
}