if 语句

1. 基本if-else语句

当条件成立时,执行某些语句;否则执行另一些语句。

完整示例

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int a;
    cin >> a;

    if (a > 5)
    {
        printf("%d is big!\n", a);
        printf("%d + 1 = %d\n", a, a + 1);
    }
    else
    {
        printf("%d is small!\n", a);
        printf("%d - 1 = %d\n", a, a - 1);
    }

    return 0;
}

简化形式

else 语句可以省略:

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int a;
    cin >> a;

    if (a > 5)
    {
        printf("%d is big!\n", a);
        printf("%d + 1 = %d\n", a, a + 1);
    }

    return 0;
}

当只有一条语句时,大括号可以省略:

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int a;
    cin >> a;

    if (a > 5)
        printf("%d is big!\n", a);
    else
        printf("%d is small!\n", a);

    return 0;
}

2. 实践练习

练习1:求绝对值

输入一个整数,输出这个数的绝对值。

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int x;

    cin >> x;

    if (x > 0) cout << x << endl;
    else cout << -x << endl;

    return 0;
}

练习2:比较大小

输入两个整数,输出两个数中较大的那个。

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int a, b;
    cin >> a >> b;

    if (a > b)
        cout << a << endl;
    else
        cout << b << endl;

    return 0;
}

练习3:三个数最大值

输入三个整数,输出三个数中最大的那个。

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int a, b, c;
    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;
    }

    return 0;
}

3. 嵌套if语句

if-else语句内部也可以是if-else语句。

4. 常用比较运算符

运算符 含义 示例
> 大于 a > b
< 小于 a < b
>= 大于等于 a >= b
<= 小于等于 a <= b
== 等于 a == b
!= 不等于 a != b

比较运算符示例

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int a, b;
    cin >> a >> b;

    if (a > b) printf("%d > %d\n", a, b);
    if (a >= b) printf("%d >= %d\n", a, b);
    if (a < b) printf("%d < %d\n", a, b);
    if (a <= b) printf("%d <= %d\n", a, b);
    if (a == b) printf("%d == %d\n", a, b);
    if (a != b) printf("%d != %d\n", a, b);

    return 0;
}

5. if-else连写

成绩等级判断

输入一个0到100之间的分数:

  • 如果大于等于85,输出A
  • 如果大于等于70并且小于85,输出B
  • 如果大于等于60并且小于70,输出C
  • 如果小于60,输出D
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int s;
    cin >> s;

    if (s >= 85)
    {
        printf("A");
    }
    else if (s >= 70)
    {
        printf("B");
    }
    else if (s >= 60)
    {
        printf("C");
    }
    else
    {
        printf("D");
    }

    return 0;
}

三、条件表达式

逻辑运算符

运算符 含义 说明
&& 两个条件都为真时为真
|| 两个条件中有一个为真时为真
! 取反,真变假,假变真

条件表达式示例

输入三个数,输出三个数中的最大值:

#include <iostream>
#include <cstdio>

using namespace std;

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

    if (a >= b && a >= c) cout << a << endl;
    else if (b >= a && b >= c) cout << b << endl;
    else cout << c << endl;

    return 0;
}

四、综合练习

练习1:简单计算器

输入两个数,以及一个运算符+, -, *, /,输出这两个数运算后的结果。

  • 当运算符是/,且除数是0时,输出"Divided by zero!"
  • 当输入的字符不是+, -, *, /时,输出"Invalid operator!"
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int a, b;
    char c;
    cin >> a >> b >> c;

    if (c == '+') cout << a + b << endl;
    else if (c == '-') cout << a - b << endl;
    else if (c == '*') cout << a * b << endl;
    else if (c == '/')
    {
        if (b != 0)
        {
            cout << a / b << endl;
        }
        else
        {
            cout << "Divided by zero!" << endl;
        }
    }
    else
    {
        cout << "Invalid operator!" << endl;
    }

    return 0;
}

练习2:判断闰年

闰年有两种情况:

  1. 能被100整除时,必须能被400整除
  2. 不能被100整除时,被4整除即可

输入一个年份,如果是闰年输出yes,否则输出no。

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int year;
    cin >> year;

    if (year % 100 == 0)
    {
        if (year % 400 == 0) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    else
    {
        if (year % 4 == 0) cout << "yes" << endl;
        else cout << "no" << endl;
    }

    return 0;
}

练习3:用一条if语句判断闰年

使用逻辑运算符简化闰年判断:

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int year;

    cin >> year;

    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
        cout << "yes" << endl;
    else
        cout << "no" << endl;

    return 0;
}

if条件语句

  • 理解条件判断的基本语法
  • 掌握if-else结构
  • 学会嵌套if语句
  • 能够解决实际问题(比较大小、求绝对值等)

实践建议

  • 多练习格式化输出,提高输出美观度
  • 通过条件语句练习逻辑思维
  • 将printf和if语句结合使用,解决更复杂的问题

0 条评论

目前还没有评论...