控制流语句
if
java
public class IfStatement {
public static void main(String[] args) {
int key = 1;
if (key == 1) {
System.out.println("Hello World! 1");
} else if (key == 2) {
System.out.println("Hello World! 2");
} else {
System.out.println("Hello World! 3");
}
}
}
switch
语句
switch
语句接收一个 selector 变量作为参数,这个 selector 变量的类型必须是以下类型:
byte
,short
,char
,int
primitive data typesCharacter
,Byte
,Short
,Integer
wrapper types- enumerated types
String
注意不能用于 selector 变量的类型:boolean
, long
, float
, double
java
int quarter = ...; // any value
String quarterLabel = null;
switch (quarter) {
case 0: quarterLabel = "Q1 - Winter";
break;
case 1: quarterLabel = "Q2 - Spring";
break;
case 2: quarterLabel = "Q3 - Summer";
break;
case 3: quarterLabel = "Q3 - Summer";
break;
default: quarterLabel = "Unknown quarter";
};
switch
语句同样遵循遇到 break
语句时才会终止。
表达式
在 Java SE 14 ,支持 switch
表达式。
java
Day day = ...; // any day
int len =
switch (day) {
case MONDAY, FRIDAY, SUNDAY -> 6;
case TUESDAY -> 7;
case THURSDAY, SATURDAY -> 8;
case WEDNESDAY -> 9;
};
System.out.println("len = " + len);
相比于传统的 switch
语句,switch
表达式需要穷尽枚举类型的所有可能值,否则会抛出 IncompatibleClassChangeError
。
while
java
public class WhileStatement {
public static void main(String[] args) {
int i = 0;
while (i < 3) {
System.out.println("Hello World! " + i);
i++;
}
}
}
do-while
java
public class DoWhileStatement {
public static void main(String[] args) {
int i = 0;
do {
System.out.println("Hello World! " + i);
i++;
} while (i < 3);
}
}
for
java
public class ForStatement {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
// 第一种
for (int i = 0; i < 3; i++) {
System.out.println("Hello World! " + arr[i]);
}
// 第二种
for (int i : arr) {
System.out.println("Hello World! " + i);
}
}
}
其他
break
java
public class BreakStatement {
public static void main(String[] args) {
int i = 0;
while (i < 3) {
System.out.println("Hello World! " + i);
if (i == 1) {
break;
}
i++;
}
}
}
java
public class BreakStatement {
public static void main(String[] args) {
int i = 0;
search:
while (i < 3) {
System.out.println("Hello World! " + i);
if (i == 1) {
break search;
}
i++;
}
System.out.println("done " + i);
}
}
// 结果:
// Hello World! 0
// Hello World! 1
// done 1
continue
java
public class ContinueStatement {
public static void main(String[] args) {
int i = 0;
while (i < 3) {
i++;
if (i == 1) {
continue;
}
System.out.println("Hello World! " + i);
}
}
}
java
public class ContinueStatement {
public static void main(String[] args) {
int i = 0;
search:
while (i < 3) {
i++;
if (i == 1) {
continue search;
}
System.out.println("Hello World! " + i);
}
System.out.println("done " + i);
}
}
// 结果:
// Hello World! 2
// Hello World! 3
// done 3
return
从当前方法退出,控制流返回到调用方法的地方。
java
public class ReturnStatement {
public static void main(String[] args) {
int i = test(0);
}
public int test(int n) {
if (n == 0) {
return 0;
} else {
return n;
}
}
}
yield
- 从当前的
switch
语句中退出 yield
后紧跟返回值的表达式,表达式不能为void
java
public class YieldStatement {
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public int calculate(Day day) {
return switch (day) {
case SATURDAY, SUNDAY -> 0;
default -> {
int remainingDays = 5 - day.ordinal();
yield remainingDays;
}
}
}
}
在可读性上,如果
yield
换成return
可能是困惑的(不知道是退出方法还是该语句),所以有了yield
语句。