⏱ 70 分钟 | 前置:第 3 课(关系与逻辑表达式)| 工具:VSCode + g++
if / if-else / else-if 链让程序"看情况办事"if 嵌套解决多条件问题,理解"悬空 else"并养成加大括号的习惯switch-case 与 break,知道什么时候用 switch 比 if 清爽? : 写简洁的二选一,避开 =/==、if 后加分号等经典坑| 时间 | 环节 |
|---|---|
| 0–5 分钟 | 复习第 3 课 + 作业讲评(重点讲零钱兑换和链式比较改错) |
| 5–20 分钟 | 知识点1:if / if-else / else-if 链 |
| 20–30 分钟 | 知识点2:if 嵌套与两大经典坑 |
| 30–43 分钟 | 知识点3:switch-case + 综合示例(简易计算器) |
| 43–50 分钟 | 知识点4:三目运算符 |
| 50–67 分钟 | 课堂练习 |
| 67–70 分钟 | 总结 + 布置作业 |
之前的程序都是"一条道走到黑":从上到下每句都执行。但现实需求是"如果下雨就带伞,否则戴帽子"——程序也要会看情况办事。
#include <iostream>
using namespace std;
int main() {
int score;
cout << "请输入分数:";
cin >> score;
if (score >= 60) { // 条件为真 → 执行大括号里的内容
cout << "及格了,恭喜!" << endl;
}
cout << "程序结束" << endl; // 不管及不及格都会执行
return 0;
}() 里,可以是任何关系/逻辑表达式(上节课学的全用得上)if (score >= 60) {
cout << "及格" << endl;
} else { // 否则(条件为假走这边)
cout << "不及格,继续加油" << endl;
}两条路必走且只走一条。else 后面不写条件——它就是"剩下的所有情况"。
#include <iostream>
using namespace std;
int main() {
int score;
cout << "请输入分数(0~100):";
cin >> score;
if (score < 0 || score > 100) {
cout << "分数不合法!" << endl; // 先排除非法输入(好习惯)
} else if (score >= 90) {
cout << "优秀 A" << endl;
} else if (score >= 80) {
cout << "良好 B" << endl; // 能走到这里,说明 score < 90 已隐含成立
} else if (score >= 70) {
cout << "中等 C" << endl;
} else if (score >= 60) {
cout << "及格 D" << endl;
} else {
cout << "不及格 E" << endl;
}
return 0;
}>= 80 不用写成 score >= 80 && score < 90,前面已经筛掉 90+ 了。条件顺序从大到小(或从小到大)排,逻辑最省。>= 60 再判 >= 90),输入 95 会输出什么?
>= 60 就退出了,永远轮不到 >= 90。顺序错了,逻辑全错但编译器不报错——这就是分支结构最阴险的地方。#include <iostream>
using namespace std;
int main() {
int age;
char hasTicket;
cout << "请输入年龄:";
cin >> age;
cout << "有票吗(y/n):";
cin >> hasTicket;
if (age >= 18) {
if (hasTicket == 'y') {
cout << "请入场" << endl;
} else {
cout << "请先购票" << endl;
}
} else {
cout << "未成年人谢绝入场" << endl;
}
return 0;
}嵌套能表达"先满足 A,再看 B"的层次逻辑。但嵌套别超过 3 层,能用 && 合并的条件就合并:if (age >= 18 && hasTicket == 'y')。
int score = 30; if (score >= 60); // ← 这个分号是一条"空语句",成了 if 的执行体! { cout << "及格" << endl; // 这个块和 if 已经无关,30 分也照样打印"及格" }
if (...); 编译不报错,运行结果全错。当堂演示 30 分打印"及格",让学生印象拉满。if (score >= 60)
cout << "及格" << endl;
cout << "奖励一朵小红花" << endl; // 缩进骗人!这句不在 if 里,人人有花不写大括号时,if 只管到下一句(分号为止),缩进只是给人看的,编译器根本不看。更阴险的是悬空 else:
if (age >= 18)
if (hasTicket == 'y')
cout << "请入场" << endl;
else // 看起来配外层 if,实际上配的是最近的里层 if!
cout << "请先购票" << endl; // 结果:未成年人什么都不输出#include <iostream>
using namespace std;
int main() {
int day;
cout << "请输入 1~7:";
cin >> day;
switch (day) { // 拿 day 的值去和各个 case 对号入座
case 1: cout << "星期一" << endl; break;
case 2: cout << "星期二" << endl; break;
case 3: cout << "星期三" << endl; break;
case 4: cout << "星期四" << endl; break;
case 5: cout << "星期五" << endl; break;
case 6:
case 7: cout << "周末快乐!" << endl; break; // 6 和 7 共用一个分支
default: cout << "输入不合法" << endl; // 都没对上走这里
}
return 0;
}break = "到此为止,跳出 switch"。忘写 break 会"穿透":从命中的 case 一路执行到后面所有 case(当堂把 case 1 的 break 注释掉演示)。score >= 90 这种还得用 else-if。离散值用 switch,范围用 if。#include <iostream>
using namespace std;
int main() {
double a, b;
char op;
cout << "请输入算式(如 3 + 4):";
cin >> a >> op >> b; // 输入 3 + 4,cin 自动拆给三个变量
switch (op) {
case '+': cout << a + b << endl; break;
case '-': cout << a - b << endl; break;
case '*': cout << a * b << endl; break;
case '/':
if (b == 0) {
cout << "除数不能为 0!" << endl; // switch 里嵌 if,守住除零
} else {
cout << a / b << endl;
}
break;
default: cout << "不认识的运算符" << endl;
}
return 0;
}运行:输入 3 + 4 → 7;输入 5 / 0 → 除数不能为 0!。这个例子把 cin 混合输入、switch、嵌套 if、double 除法全串起来了。
% 分支(提示:% 只能整数,需要 (int)a % (int)b),2 分钟当场做。#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "请输入两个整数:";
cin >> a >> b;
int max = (a > b) ? a : b; // 条件 ? 真取这个 : 假取这个
cout << "较大值 = " << max << endl;
cout << ((a % 2 == 0) ? "偶数" : "奇数") << endl; // 输出时记得整体加括号
return 0;
}读法:"a 大于 b 吗?是就取 a,不是就取 b"。适合简单的二选一取值;逻辑复杂就老老实实写 if-else,别硬塞一行。
max3.cpp —— 输入三个整数,输出最大值。#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "请输入三个整数:";
cin >> a >> b >> c;
int max;
if (a > b) {
max = a;
} else {
max = b;
}
if (c > max) {
max = c;
}
cout << "最大值 = " << max << endl;
return 0;
}sign.cpp —— 输入一个整数,输出"正数"、"负数"或"零"。用 else-if 链,测试 5、-3、0 三种情况。
#include <iostream>
using namespace std;
int main() {
int n;
cout << "请输入一个整数:";
cin >> n;
if (n > 0) {
cout << "正数" << endl;
} else if (n < 0) {
cout << "负数" << endl;
} else {
cout << "零" << endl;
}
return 0;
}taxi.cpp —— 起步价 8 元含 3 公里;超过 3 公里的部分每公里 2 元(不足 1 公里按 1 公里算,简化:输入整数公里)。输入公里数,输出车费。2→8 元,3→8 元,5→12 元。
#include <iostream>
using namespace std;
int main() {
int km;
cout << "请输入公里数(整数):";
cin >> km;
int fee;
if (km <= 3) {
fee = 8;
} else {
fee = 8 + (km - 3) * 2;
}
cout << "车费 = " << fee << " 元" << endl;
return 0;
}days.cpp —— 输入年份和月份,输出该月天数。#include <iostream>
using namespace std;
int main() {
int year, month;
cout << "请输入年份和月份:";
cin >> year >> month;
switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
cout << 31 << " 天" << endl;
break;
case 4: case 6: case 9: case 11:
cout << 30 << " 天" << endl;
break;
case 2: {
bool isLeap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
if (isLeap) {
cout << 29 << " 天" << endl;
} else {
cout << 28 << " 天" << endl;
}
break;
}
default:
cout << "月份不合法!" << endl;
}
return 0;
}leap.cpp —— 输入年份,用 if-else(不用上节课的单表达式)输出"XXXX 是闰年"或"XXXX 不是闰年"。#include <iostream>
using namespace std;
int main() {
int year;
cout << "请输入年份:";
cin >> year;
if (year % 400 == 0) {
cout << year << " 是闰年" << endl;
} else if (year % 100 == 0) {
cout << year << " 不是闰年" << endl; // 能被100整除但不能被400整除
} else if (year % 4 == 0) {
cout << year << " 是闰年" << endl;
} else {
cout << year << " 不是闰年" << endl;
}
return 0;
}sort3.cpp —— 输入三个整数,从小到大输出。#include <iostream>
using namespace std;
int main() {
int a, b, c, temp;
cout << "请输入三个整数:";
cin >> a >> b >> c;
if (a > b) { temp = a; a = b; b = temp; } // 保证 a <= b
if (a > c) { temp = a; a = c; c = temp; } // 保证 a 最小
if (b > c) { temp = b; b = c; c = temp; } // 保证 b <= c
cout << a << " " << b << " " << c << endl;
return 0;
}#include <iostream>
using namespace std;
int main() {
int age;
char ticket;
cin >> age >> ticket;
if (age >= 18);
{
if (ticket = 'y')
cout << "请入场" << endl;
cout << "祝观影愉快" << endl;
}
return 0;
}if (age >= 18); 多了分号,if 执行体变成空语句,删除分号;② ticket = 'y' 是赋值不是比较,改为 ticket == 'y'(赋值表达式值为 'y' 非 0,条件永远为真);③ 里层 if 没加大括号,"祝观影愉快"人人都会打印,给它加 {};④ 输入前缺提示语(习惯问题),补上 cout 提示。核心必改:①②③。if 单分支、if-else 二选一、else-if 链多选一:从上往下判,命中即退出条件 ? A : B:简单二选一取值;比较用 ==,= 是赋值