今天来对逻辑控制进行对比学习。
在 Java 和 Python 中,条件判断(if-else)、模式匹配(switch-match)、循环(for/while) 是核心控制流结构。
Java 以严格的语法和强类型著称,而 Python 以简洁易读为特点。
1. 条件判断(if-else)
Java 和 Python 都使用 if-else 结构,但Java 需要使用大括号 {},Python 使用缩进。
Java 的 if-else
public class Main {
public static void main(String[] args) {
int num = 10;
if (num > 0) {
System.out.println("正数");
} else if (num < 0) {
System.out.println("负数");
} else {
System.out.println("零");
}
}
}
特点
- 必须使用 {}(除非 if 语句只有一行)。
- 条件必须是 boolean 类型(num 不能是非布尔类型)。
- else if 用于多个条件判断。
Python 的 if-else
num = 10
if num > 0:
print("正数")
elif num < 0:
print("负数")
else:
print("零")
特点
- 使用缩进代替 {}(没有 : 会报错)。
- elif 代替 Java 的 else if。
- 条件可以是任何表达式(甚至非 bool 类型,如 if num:)。
2. 模式匹配(Switch vs. Match)
Java 传统上使用 switch-case,Python 3.10+ 引入了 match-case,两者类似但语法不同。
Java 的 switch-case
public class Main {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
default:
System.out.println("未知");
}
}
}
特点
- switch 适用于离散值判断(如 int、String)。
- 需要 break,否则会继续执行下一个 case(fall-through)。
- default 处理未匹配的情况。
Python 的 match-case(Python 3.10+)
age = 15
args = ['gcc', 'hello.c', 'world.c']
# args = ['clean']
# args = ['gcc']
match age:
case x if x < 10:
print(f'< 10 years old: {x}')
case 10:
print('10 years old.')
case 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18:
print('11~18 years old.')
case 19:
print('19 years old.')
case _:
print('not sure.')
match args:
# 如果仅出现gcc,报错:
case ['gcc']:
print('gcc: missing source file(s).')
# 出现gcc,且至少指定了一个文件:
case ['gcc', file1, *files]:
print('gcc compile: ' + file1 + ', ' + ', '.join(files))
# 仅出现clean:
case ['clean']:
print('clean')
case _:
print('invalid command.')
特点
- match-case 语法更简洁,不需要 break。
- _ 代替 default 作为通配符。
- 支持模式匹配,可以匹配数据结构(如列表、字典等)。
3. 循环(for、while)
Java 和 Python 都支持 for 循环和 while 循环,但 Python 语法更简洁。
Java 的 for 循环
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
}
}
特点
- for 语法是 for (初始化; 条件; 递增/递减)。
- 变量类型必须声明(如 int i = 0)。
- 适用于计数循环。
Python 的 for 循环
for i in range(5):
print(i)
特点
- range(n) 生成 0 到 n-1 的数(类似 Java for)。
- i 无需声明类型,Python 自动推断。
- 更加简洁易读。
Java 的 while 循环
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
特点
- 适用于循环次数不确定的情况。
- 条件必须是 boolean 类型。
Python 的 while 循环
i = 0
while i < 5:
print(i)
i += 1
特点
- 语法比 Java 更简洁。
- Python 中 while True: 常用于无限循环。
4. Java vs Python 循环总结
循环类型 | Java | Python |
for | for (int i = 0; i < n; i++) | for i in range(n) |
while | while (condition) {} | while condition: |
遍历数组 | for (int x : array) {} | for x in array: |
遍历字典 | for (Map.Entry | for key, value in dict.items(): |
可变类型 | for (var x : list) {}(Java 10+) | for x in list:(自动推断类型) |
5. 总结
特性 | Java | Python |
条件判断 | if (条件) {},必须用 {} | if 条件:,使用缩进 |
else if | else if | elif |
switch-case | 需要 break,只能匹配基本数据类型 | match-case,更简洁,支持模式匹配 |
for 循环 | 经典 for (int i=0; i<n; i++) | for i in range(n) |
while 循环 | while (条件) {} | while 条件: |
遍历集合 | for (类型 x : list) {} | for x in list: |
整体来看,条件判断和循环java和python差别不大,只是python更加灵活。有不对或者漏掉的欢迎指正。