C++程序流程结构-跳转语句

目录

1.break语句

2 continue语句

3. goto语句


1.break语句

作用:用于跳出选择结构或者循环结构

break使用的时机:

○出现在switch条件语句中,作用是终止case并跳出switch

○出现在循环语句中,作用是跳出当前的循环语句

○出现在嵌套循环中,跳出最近的内层循环语句

#include<iostream>
using namespace std;

int main(){

    //break的使用时机
    
    //1、出现在switch语句中
    cout << "请选择副本难度" << endl;
    cout << "1、普通" << endl;
    cout << "2、中等" << endl;
    cout << "3、困难" << endl;

    int select = 0; //创建选择结果的变量

    cin >> select;//等待用户输入

    switch(select)
    {
    case 1:
        cout << "您选择的是普通难度" << endl;
        break;//退出switch语句
    case 2:
        cout << "您选择的是中等难度" << endl;
        break;//退出switch语句
    case 3:
        cout << "您选择的是困难难度" << endl;    
        break;//退出switch语句
    default:
        break;//退出switch语句
    }

    //2、出现在循环语句中
    for (int i = 0; i < 10; i++)
    {
        //如果i等于5,退出循环,不再打印
        if(i == 5)
        {
            break;//退出当前循环
        }
        cout << i << endl;
    }

    //3、出现在嵌套循环语句中
    for(int i = 0; i < 10; i++)
    {
        for(int j = 0; j < 10; j++)
        {
            if(j == 5)
            {
                break;//退出内层循环
            }
            cout << "*";
        }
    }

    system("pause");
    return 0;
}

2 continue语句

作用:在循环语句中,跳过本次循环中余下尚未执行的语句,继续下一次循环

#include<iostream>
using namespace std;

int main(){

    //continue 语句

    for(int i = 0; i <= 100; i++)
    {
        //如果是奇数输出,偶数部署出差
        if( i % 2 == 0)//0 2 4 6
        {
            continue;//可以筛选条件,执行到此就不再向下执行,执行下一次循环
            //break会退出循环,continue不会
        }
        cout << i << endl;
    }
    system("pause");

    return 0;
}

3. goto语句

作用:可以无条件跳转语句

语法:goto 标记;

解释:如果标记的名称存在,执行到goto语句时,会跳转到标记的位置

#include<iostream>
using namespace std;

int main(){

    //goto语句
    
    cout << "1、xxxx" << endl;

    cout << "2、xxxx" << endl;

    goto FLAG;

    cout << "3、xxxx" << endl;

    cout << "4、xxxx" << endl;

    FLAG:
    cout << "5、xxxx" << endl;

    system("pause");

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KevinJune

希望我的内容对你们有所帮助

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值