if语句


目录
  • 一 if语句
  • 二 if...else语句
  • 三 if...else if...else语句
  • 四 if语句的嵌套

一 if语句

#include 
#include 

using namespace std;

int main()
{
    int year = 0;

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

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

    system("pause");
    return 0;
}

二 if...else语句

#include 
#include 

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;
}

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

#include 
#include 

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;
}

四 if语句的嵌套

#include 
#include 

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;
}