在编程中,流程控制是程序根据特定条件选择执行路径的核心机制。Java 提供了多种条件语句,帮助开发者实现灵活的逻辑分支,使程序能够“智能”地响应不同输入或状态。
掌握 if、if-else、nested-if 和 if-else-if 梯形结构的使用
理解 switch-case 语句的适用场景与语法细节
能够在合适场景选择 if-else 或 switch
了解三元运算符作为轻量级条件表达式的用途
避免常见陷阱(如遗漏 break、悬空 else 等)
if 语句最简单的条件判断,详情请参考《Java 中的 if 语句》
当某个条件为 true 时,执行一段代码。
if (condition) {
// 条件为真时执行的代码
}⚠️ 注意:若省略
{},则只有紧随其后的一行属于if块。
int i = 10;
if (i < 15) {
System.out.println("Condition is True");
}
// 输出: Condition is True
if-else 语句二选一决策,详情请参考《Java 中的 if-else 语句》
提供两个互斥的执行路径:条件为真执行 A,否则执行 B。
if (condition) {
// 条件为真
} else {
// 条件为假
}int i = 10;
if (i < 15)
System.out.println("i is smaller than 15");
else
System.out.println("i is greater than or equal to 15");
// 输出: i is smaller than 15💡 即使只有一行,也建议使用
{},提升可读性并避免逻辑错误。
if多层条件判断,详情请参考《Java 中的嵌套 if 语句》
在一个 if 块内部再包含另一个 if,适用于条件依赖的场景。

int i = 10;
if (i < 15) {
System.out.println("i is smaller than 15");
if (i == 10) {
System.out.println("i is exactly 10");
}
}输出:
i is smaller than 15
i is exactly 10✅ 适用场景:先判断大类,再细化子条件(如:先判断用户登录,再判断权限等级)
if-else-if 梯形结构多条件顺序判断
用于检查多个互斥条件,按顺序匹配,一旦命中即停止后续判断。

if (condition1) {
// ...
} else if (condition2) {
// ...
} else if (condition3) {
// ...
} else {
// 默认情况
}int score = 85;
if (score >= 90) {
System.out.println("Grade: A");
} else if (score >= 80) {
System.out.println("Grade: B");
} else if (score >= 70) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: F");
}
// 输出: Grade: B🔍 执行特点:从上到下逐个检查,仅执行第一个为真的分支。
switch-case 语句基于值的多路分支,详情请参考《Java 中的 switch 语句》
当需要根据一个变量的精确值选择执行路径时,switch 更清晰高效。

switch (expression) {
case value1:
// 代码
break;
case value2:
// 代码
break;
default:
// 默认处理
}基本整型:byte、short、int、char
枚举类型(enum)
String(JDK 7+)
Integer、Character 等包装类(自动拆箱)
int day = 3;
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
case 3: System.out.println("Wednesday"); break;
default: System.out.println("Invalid day");
}
// 输出: Wednesdayint num = 2;
switch (num) {
case 1: System.out.print("One ");
case 2: System.out.print("Two ");
case 3: System.out.print("Three");
}
// 输出: Two Three (因为缺少 break!)✅ 现代替代方案(Java 14+):使用
switch表达式(带->和yield),天然避免穿透。
? :)简洁的条件表达式
适用于简单赋值或返回场景,是 if-else 的紧凑写法。
variable = condition ? valueIfTrue : valueIfFalse;int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Maximum is " + max); // Maximum is 20适用场景:初始化变量、方法参数、返回值等
不适用:复杂逻辑、多语句块、有副作用的操作
if-else vs switch-case使用 switch:处理菜单选项、状态码、枚举值等离散常量
使用 if-else:需要范围判断、复合条件、动态计算
elseif (x > 0)
if (y > 0)
System.out.println("Both positive");
else
System.out.println("x <= 0"); // 实际绑定到内层 if!✅ 解决方案:始终使用
{}明确作用域。
break 导致穿透switch (status) {
case ERROR:
logError();
// 忘记 break → 继续执行 WARNING 分支!
case WARNING:
sendAlert();
break;
}✅ 解决方案:每个
case结尾加break,或使用现代switch表达式。
if-else-if 条件顺序错误if (score >= 60) System.out.println("Pass");
else if (score >= 80) System.out.println("Good"); // 永远不会执行!✅ 解决方案:从最严格条件开始写(如先
>=90,再>=80)
if:单条件判断
if-else:二选一分支
nested-if:条件嵌套,适合层级判断
if-else-if ladder:多条件顺序匹配,仅执行第一个真分支
switch-case:基于精确值的高效多路分支,记得加 break
三元运算符:简单条件赋值的快捷方式
优先使用 switch 处理离散常量,用 if-else 处理复杂逻辑
以下代码输出什么?为什么?
int x = 5;
switch (x) {
case 5: System.out.print("Five");
case 10: System.out.print("Ten");
default: System.out.print("Other");
}
如何用 if-else-if 实现成绩等级(A/B/C/D/F)?条件顺序应该如何安排?
在什么情况下,即使 switch 的 expression 是 String,也可能比 if-else 更高效?