知识点 循环控制语句
1、goto语句
goto 语句允许把控制无条件转移到同一函数内的被标记的语句。
C++ 中 goto 语句的语法:
goto label; .. . label: statement;
goto 将会指定某个特定的语句。
例如:
#includeusing namespace std; int main () { int a = 10; LOOP:do { if( a == 15) { a = a + 1; goto LOOP; } cout << "a 的值:" << a << endl; a = a + 1; }while( a < 20 ); return 0; }
其运行结果为:
a 的值:10 a 的值:11 a 的值:12 a 的值:13 a 的值:14 a 的值:16 a 的值:17 a 的值:18 a 的值:19 -------------------------------- Process exited after 0.8758 seconds with return value 0 请按任意键继续. . .
2、continue语句
continue语句会直接跳过当前循环。
语法:continue;
示例:
#includeusing namespace std; { int a = 10; do { if( a == 15) { a = a + 1; continue; } cout << "a 的值:" << a << endl; a = a + 1; }while( a < 20 ); return 0; }
运行结果:
a 的值:10 a 的值:11 a 的值:12 a 的值:13 a 的值:14 a 的值:16 a 的值:17 a 的值:18 a 的值:19 -------------------------------- Process exited after 0.7291 seconds with return value 0 请按任意键继续. . .
学习心得:goto语句和continue语句都是循环中的重要语句,学会使用它们会让代码更简单。



