if语句

一 if语句

#include <iostream>
#include <Windows.h>

using namespace std;

int main()
{
    int year = 0;

    cout << "你来公司几年了?";
    cin >> year;

    if (year > 5)
    {
        cout << "前辈,你好" << endl;
    }

    system("pause");
    return 0;
}

image

二 if...else语句

#include <iostream>
#include <Windows.h>

using namespace std;

int main()
{
    int year = 0;

    cout << "你来公司几年了?";
    cin >> year;

    if (year > 5)
    {
        cout << "你有10天年假" << endl;
    }
    else
    {
        cout << "你有3天年假" << endl;
    }

    system("pause");
    return 0;
}

image

三 if...else if...else语句

#include <iostream>
#include <Windows.h>

using namespace std;

int main()
{
    int year = 0;

    cout << "你来公司几年了?";
    cin >> year;

    if (year < 3)
    {
        cout << "你有3天年假" << endl;
    }
    else if(year > 10)
    {
        cout << "你有10天年假" << endl;
    }
    else
    {
        cout << "你有5天年假" << endl;
    }

    system("pause");
    return 0;
}

image

四 if语句的嵌套

#include <iostream>
#include <Windows.h>

using namespace std;

int main()
{
    int a, b, c;

    cout << "请输入3个整数:";
    cin >> a >> b >> c;

    if (a > b)
    {
        if (a > c)
        {
            cout << "最大值:" << a << endl;
        }
        else
        {
            cout << "最大值:" << c << endl;
        }
    }
    else
    {
        if (b > c)
        {
            cout << "最大值:" << b << endl;
        }
        else
        {
            cout << "最大值:" << c << endl;
        }
    }

    system("pause");
    return 0;
}

image

posted @ 2022-04-07 06:09  荒年、  阅读(25)  评论(0编辑  收藏  举报