IF -else-条件语句问题请教

IF -else-条件语句问题请教
请帮忙看看以下作业题。谢谢!

考虑下面的代码片段,其中包含IF -否则IF -条件语句。重新编写代码,通过移除冗余的(不必要的)条件测试。

float income;
cout << "Enter your monthly income: ";
cin >> income;
if (income < 0.0)
cout << "Invalid." << endl;
else if (income >= 0.0 && income < 1200.00)
cout << "Poor." << endl;
else if (income >= 1200.00 && income < 2500.00)
cout << "Ok." << endl;
else if (income >= 2500.00)
cout << "Rich." << endl;

float income;
cout << "Enter your monthly income: ";
cin >> income;
if (income < 0.0)
cout << "Invalid." << endl;
else if (income < 1200.00) //此内容在else之后,表明income已经>=0.0,故income >= 0.0可省略
cout << "Poor." << endl;
else if (income < 2500.00) //此内容在else之后,表明income已经>=1200.00,故income >= 1200.00可省略
cout << "Ok." << endl;
else //此内容在else之后,表明income已经>=2500.00,故可省略
cout << "Rich." << endl;

符合else的条件说明一定不符合if后的条件,故else后有些不必要的条件可以省略

float income;

cout << "Enter your monthly income: ";
cin >> income;
if (income < 0.0)
    cout << "Invalid." << endl;
else if (income < 1200.00)
    cout << "Poor." << endl;
else if (income < 2500.00)
    cout << "Ok." << endl;
else
    cout << "Rich." << endl;

float income;
count << "Enter your monthly income:";
if (income >=2500.00)
cout << "Rich." << endl;
else if (income >= 1200.00)
cout << "Ok." << endl;
else if (income >= 0.0)
cout << "Poor." << endl;
else
cout << "Invalid." << endl;

//就用一边来比较就可以了

float income;
cout << "Enter your monthly income: ";
cin >> income;

if (income < 0.0)
    cout << "Invalid." << endl;
else if (income < 1200.00)  // 若程序运行到这,则上个判断语句为假,显然income不小于0.0,所以再判断income>=0.0是多余的
    cout << "Poor." << endl;
else if (income < 2500.00)  // 同上
    cout << "Ok." << endl;
else    // 同上
    cout << "Rich." << endl;

float income;
cout << "Enter your monthly income: ";
cin >> income;

if (income < 0.0 )
{
    cout << "Invalid." << endl;
}
else
{
    if ( income < 1200.00 )
    {
        cout << "Poor." << endl;
    }
    else
    {
        if ( income < 2500.00 )
        {
            cout << "Ok." << endl;
        }
        else
        {
            cout << "Rich." << endl;
        }
    }
}

string info[4] = { "Invalid.","Poor.", "Ok.","Rich." };
float income;
count << "Enter your monthly income:";
cin >> income;

int a = (income > 0) + (income > 1200) + (income > 2500);
cout << info[a] << endl;

float income;
cout << "Enter your monthly income: ";
cin >> income;
if (income < 0.0)
cout << "Invalid." << endl;
else if (income <= 1200.00)
cout << "Poor." << endl;
else if (income <= 2500.00)
cout << "Ok." << endl;
else
cout << "Rich." << endl;

感谢各位老师。谢谢!

elseif语句 一般都是分支 类似于switch case

float income;
cout << "Enter your monthly income: ";
cin >> income;

if (income < 0.0)
cout << "Invalid." << endl;
else if (income < 1200.00) // 若程序运行到这,则上个判断语句为假,显然income不小于0.0,所以再判断income>=0.0是多余的
cout << "Poor." << endl;
else if (income < 2500.00) // 同上
cout << "Ok." << endl;
else // 同上
cout << "Rich." << endl;

float income;
count << "Enter your monthly income:";
if (income >=2500.00)
cout << "Rich." << endl;
else if (income >= 1200.00)
cout << "Ok." << endl;
else if (income >= 0.0)
cout << "Poor." << endl;
else
cout << "Invalid." << endl;

//就用一边来比较就可以了