【学习笔记】C++ 程序选择结构

目录

1.三目运算符

2.敲桌子案例

3.continue

4. goto


1.三目运算符

语法: 表达式1 ? 表达式2 :表达式3

含义: 若1的值为真,则执行2

若1的值为假则执行3.

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int a = 100;

    int b = 200;

    int c = 0;

    a > b ? c = 1 : c = 0;//比较ab的值,成立c=1 不成立c=0

    std::cout << "c的值为 =" << c << std::endl;

//三目运算符返回值为变量,可以继续赋值


std::cout << "ab中最大的数 =" <<(a > b ? a : b) << std::endl;

    system("pause");

    return 0;
}

2.敲桌子案例

#include <iostream>
#include <string>
using namespace std;

int main()
{
    for (int i = 0; i <=100; i++)
    {
        if (i % 7 == 0 || i % 7 ==7 || i / 10 == 7)//7的倍数,各位或者十位有7
        {
            std::cout << "敲桌子" << std::endl;
        }
        else
        {
            std::cout << i << std::endl;
        }
    }

    system("pause");

    return 0;
}

3.continue

作用:直接进入新的循环。

#include <iostream>
#include <string>
using namespace std;

int main()
{
    for (int i = 0; i <= 100; i++)
    {
        if (i % 7 == 0 || i % 7 == 7 || i / 10 == 7) //7的倍数,各位或者十位有7
        {
            continue;
            std::cout << "敲桌子" << std::endl;
        }
        else
        {
            std::cout << i << std::endl;
        }
    }

    system("pause");

    return 0;
}

4. goto

无条件跳到标记位置

goto 标记名;

标记名:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值