第4章 - 条件判断
嗨,朋友!我是长安。
到目前为止,我们写的程序都是"直线型"的——从头到尾按顺序执行。但现实中,我们经常需要根据不同情况做出不同决定:
- 如果下雨,就带伞;如果不下雨,就不带
- 如果考试及格,就奖励自己;如果不及格,就继续努力
- 如果余额充足,就购买;如果余额不足,就提示充值
这一章,我要教你如何让程序变得"聪明",能够根据条件做出判断和选择!
🤔 什么是条件判断?
想象你是一个门卫:
如果(访客有门卡) {
放行
} 否则 {
不放行
}
这就是条件判断!在 C++ 中,我们用 if-else 语句来实现。
📝 if 语句
基本格式
if (条件) {
// 如果条件为真,执行这里的代码
}
第一个示例
#include <iostream>
using namespace std;
int main() {
int age = 20;
if (age >= 18) {
cout << "你是成年人了!" << endl;
}
return 0;
}
输出:
你是成年人了!
解释:
age >= 18是条件(年龄大于等于18)- 如果条件为真,就执行大括号
{}里的代码 - 如果条件为假,就跳过大括号里的代码
🔀 if-else 语句
如果条件不满足时,我们想做别的事情,就用 if-else。
格式
if (条件) {
// 条件为真时执行
} else {
// 条件为假时执行
}
示例
#include <iostream>
using namespace std;
int main() {
int score;
cout << "请输入你的考试分数:";
cin >> score;
if (score >= 60) {
cout << "恭喜你,考试及格了!" << endl;
} else {
cout << "很遗憾,考试不及格。加油!" << endl;
}
return 0;
}
运行效果1:
请输入你的考试分数:85
恭喜你,考试及格了!
运行效果2:
请输入你的考试分数:45
很遗憾,考试不及格。加油!
🌳 if-else if-else 语句(多条件判断)
如果有多个条件需要判断,就用 else if。
格式
if (条件1) {
// 条件1为真时执行
} else if (条件2) {
// 条件1为假且条件2为真时执行
} else if (条件3) {
// 条件1、2为假且条件3为真时执行
} else {
// 所有条件都为假时执行
}
示例:成绩等级判断
#include <iostream>
using namespace std;
int main() {
int score;
cout << "请输入你的分数:";
cin >> score;
if (score >= 90) {
cout << "等级:优秀 A" << endl;
} else if (score >= 80) {
cout << "等级:良好 B" << endl;
} else if (score >= 70) {
cout << "等级:中等 C" << endl;
} else if (score >= 60) {
cout << "等级:及格 D" << endl;
} else {
cout << "等级:不及格 F" << endl;
}
return 0;
}
运行效果:
请输入你的分数:85
等级:良好 B
🔗 比较运算符
条件判断需要用到比较运算符:
| 运算符 | 含义 | 示例 | 结果 |
|---|---|---|---|
== | 等于 | 5 == 5 | true |
!= | 不等于 | 5 != 3 | true |
> | 大于 | 5 > 3 | true |
< | 小于 | 5 < 3 | false |
>= | 大于等于 | 5 >= 5 | true |
<= | 小于等于 | 3 <= 5 | true |
注意
判断相等用 ==(两个等号),不是 =(一个等号)!
if (age == 18) // ✅ 正确:判断 age 是否等于 18
if (age = 18) // ❌ 错误:将 18 赋值给 age
示例
#include <iostream>
using namespace std;
int main() {
int a = 10;
int b = 20;
cout << "a = " << a << ", b = " << b << endl;
cout << "a == b: " << (a == b) << endl; // 0 (false)
cout << "a != b: " << (a != b) << endl; // 1 (true)
cout << "a > b: " << (a > b) << endl; // 0 (false)
cout << "a < b: " << (a < b) << endl; // 1 (true)
cout << "a >= b: " << (a >= b) << endl; // 0 (false)
cout << "a <= b: " << (a <= b) << endl; // 1 (true)
return 0;
}
🧩 逻辑运算符
有时我们需要组合多个条件:
| 运算符 | 含义 | 示例 | 说明 |
|---|---|---|---|
&& | 逻辑与(且) | a > 0 && b > 0 | 两个条件都为真才为真 |
| ` | ` | 逻辑或 | |
! | 逻辑非(取反) | !(a > 0) | 将真变假,假变真 |
逻辑与 (&&) 示例
两个条件都为真,结果才为真。
#include <iostream>
using namespace std;
int main() {
int age;
cout << "请输入你的年龄:";
cin >> age;
if (age >= 18 && age <= 60) {
cout << "你是成年人,且在工作年龄范围内" << endl;
} else {
cout << "你不在工作年龄范围内" << endl;
}
return 0;
}
逻辑或 (||) 示例
两个条件任意一个为真,结果就为真。
#include <iostream>
using namespace std;
int main() {
char vip;
cout << "你是VIP会员吗?(Y/N):";
cin >> vip;
if (vip == 'Y' || vip == 'y') {
cout << "欢迎VIP会员!享受9折优惠" << endl;
} else {
cout << "欢迎普通用户!" << endl;
}
return 0;
}
逻辑非 (!) 示例
取反操作,真变假,假变真。
#include <iostream>
using namespace std;
int main() {
bool isRaining = false;
if (!isRaining) {
cout << "没下雨,不用带伞" << endl;
} else {
cout << "下雨了,记得带伞" << endl;
}
return 0;
}
🔄 switch 语句
当需要根据一个变量的不同值执行不同代码时,用 switch 更清晰。
格式
switch (变量) {
case 值1:
// 代码
break;
case 值2:
// 代码
break;
default:
// 默认代码
break;
}
示例:星期查询
#include <iostream>
using namespace std;
int main() {
int day;
cout << "请输入星期几(1-7):";
cin >> day;
switch (day) {
case 1:
cout << "星期一 Monday" << endl;
break;
case 2:
cout << "星期二 Tuesday" << endl;
break;
case 3:
cout << "星期三 Wednesday" << endl;
break;
case 4:
cout << "星期四 Thursday" << endl;
break;
case 5:
cout << "星期五 Friday" << endl;
break;
case 6:
cout << "星期六 Saturday" << endl;
break;
case 7:
cout << "星期日 Sunday" << endl;
break;
default:
cout << "输入错误!请输入1-7之间的数字" << endl;
break;
}
return 0;
}
不要忘记 break
每个 case 后面都要加 break,否则会继续执行下一个 case!
switch (day) {
case 1:
cout << "星期一";
// 没有 break,会继续执行 case 2
case 2:
cout << "星期二";
break;
}
🌟 实战案例
案例1:登录验证
#include <iostream>
#include <string>
using namespace std;
int main() {
string username, password;
cout << "===== 用户登录 =====" << endl;
cout << "请输入用户名:";
cin >> username;
cout << "请输入密码:";
cin >> password;
if (username == "admin" && password == "123456") {
cout << "\n登录成功!欢迎管理员" << endl;
} else {
cout << "\n用户名或密码错误!" << endl;
}
return 0;
}
案例2:BMI 计算器
#include <iostream>
using namespace std;
int main() {
double height, weight;
cout << "===== BMI 计算器 =====" << endl;
cout << "请输入你的身高(米):";
cin >> height;
cout << "请输入你的体重(公斤):";
cin >> weight;
double bmi = weight / (height * height);
cout << "\n你的BMI指数是:" << bmi << endl;
if (bmi < 18.5) {
cout << "体重过轻" << endl;
} else if (bmi < 24) {
cout << "体重正常" << endl;
} else if (bmi < 28) {
cout << "体重过重" << endl;
} else {
cout << "肥胖" << endl;
}
return 0;
}
案例3:简易计算器(带选择)
#include <iostream>
using namespace std;
int main() {
double num1, num2;
char op;
cout << "===== 简易计算器 =====" << endl;
cout << "请输入第一个数字:";
cin >> num1;
cout << "请输入运算符(+、-、*、/):";
cin >> op;
cout << "请输入第二个数字:";
cin >> num2;
cout << "\n计算结果:";
switch (op) {
case '+':
cout << num1 << " + " << num2 << " = " << (num1 + num2) << endl;
break;
case '-':
cout << num1 << " - " << num2 << " = " << (num1 - num2) << endl;
break;
case '*':
cout << num1 << " * " << num2 << " = " << (num1 * num2) << endl;
break;
case '/':
if (num2 != 0) {
cout << num1 << " / " << num2 << " = " << (num1 / num2) << endl;
} else {
cout << "错误:除数不能为0!" << endl;
}
break;
default:
cout << "错误:不支持的运算符!" << endl;
break;
}
return 0;
}
案例4:购票系统
#include <iostream>
using namespace std;
int main() {
int age;
char isStudent;
double price = 100.0; // 原价
cout << "===== 电影票购买系统 =====" << endl;
cout << "票价:100元" << endl;
cout << "请输入你的年龄:";
cin >> age;
cout << "你是学生吗?(Y/N):";
cin >> isStudent;
// 计算折扣
if (age < 12) {
price *= 0.5; // 儿童半价
cout << "\n儿童票,享受5折优惠" << endl;
} else if (age >= 60) {
price *= 0.7; // 老年人7折
cout << "\n老年票,享受7折优惠" << endl;
} else if (isStudent == 'Y' || isStudent == 'y') {
price *= 0.8; // 学生8折
cout << "\n学生票,享受8折优惠" << endl;
} else {
cout << "\n成人票,原价" << endl;
}
cout << "应付金额:" << price << " 元" << endl;
return 0;
}
案例5:猜数字游戏
#include <iostream>
using namespace std;
int main() {
int secret = 42; // 神秘数字
int guess;
cout << "===== 猜数字游戏 =====" << endl;
cout << "我心里想了一个1-100之间的数字" << endl;
cout << "请猜猜是多少:";
cin >> guess;
if (guess == secret) {
cout << "恭喜你,猜对了!" << endl;
} else if (guess > secret) {
cout << "太大了!正确答案是 " << secret << endl;
} else {
cout << "太小了!正确答案是 " << secret << endl;
}
return 0;
}
🎯 小结
这一章我们学习了条件判断,让程序能够根据不同情况做出决定:
| 概念 | 说明 | 示例 |
|---|---|---|
| if | 单条件判断 | if (age >= 18) { ... } |
| if-else | 双分支判断 | if (...) { ... } else { ... } |
| if-else if-else | 多分支判断 | 多个条件依次判断 |
| switch | 多值判断 | switch (day) { case 1: ... } |
| == | 等于 | a == b |
| != | 不等于 | a != b |
| >、<、>=、<= | 大小比较 | a > b |
| && | 逻辑与(且) | a && b 都为真 |
| || | 逻辑或 | a || b 任一为真 |
| ! | 逻辑非 | !a 取反 |
💪 练习题
动手练习巩固知识:
- 写一个程序,判断一个数是正数、负数还是零
- 写一个程序,输入三个数,找出最大的那个
- 写一个程序,判断一个年份是否是闰年(能被4整除但不能被100整除,或能被400整除)
- 写一个程序,根据月份(1-12)输出该月的天数
- 写一个程序,输入考试分数,判断是否及格,并给出鼓励或建议
点击查看参考答案
题目1 参考答案:
#include <iostream>
using namespace std;
int main() {
int num;
cout << "请输入一个数:";
cin >> num;
if (num > 0) {
cout << num << " 是正数" << endl;
} else if (num < 0) {
cout << num << " 是负数" << endl;
} else {
cout << "这个数是零" << endl;
}
return 0;
}
题目3 参考答案:
#include <iostream>
using namespace std;
int main() {
int year;
cout << "请输入年份:";
cin >> year;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
cout << year << " 是闰年" << endl;
} else {
cout << year << " 不是闰年" << endl;
}
return 0;
}
🚀 下一步
太棒了!你的程序现在能"思考"了!
下一章,我们要学习循环语句,让程序能够自动重复执行某些操作,效率倍增!
➡️ 第5章 - 循环语句
