“21天好习惯“第一期——12

知识点 循环控制语句

1、goto语句

goto 语句允许把控制无条件转移到同一函数内的被标记的语句。

C++ 中 goto 语句的语法:

goto label;
..
.
label: statement;

goto 将会指定某个特定的语句。

例如:

#include <iostream>
using 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;

示例:

#include <iostream>
using 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语句都是循环中的重要语句,学会使用它们会让代码更简单。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值