Skip to content

流程控制语句

字数
4231 字
阅读时间
20 分钟

Java 控制语句

Java 控制语句大致可分为三大类:

  1. 选择语句
  2. 循环语句
  3. 终端语句

选择语句

if 语句

if 语句会判断括号中的条件是否成立,如果成立则执行 if 语句中的代码块,否则跳过代码块继续执行。

语法

java
if (布尔表达式) {
   // 如果布尔表达式为 true 将执行的语句
}

示例

java
public class IfDemo {
    public static void main(String args[]) {
        int x = 10;
        if (x < 20) {
            System.out.print("这是 if 语句");
        }
    }
}
// output:
// 这是 if 语句

if...else 语句

if 语句后面可以跟 else 语句,当 if 语句的布尔表达式值为 false 时,else 语句块会被执行。

语法

java
if (布尔表达式) {
   // 如果布尔表达式的值为 true
} else {
   // 如果布尔表达式的值为 false
}

示例

java
public class IfElseDemo {
    public static void main(String args[]) {
        int x = 30;
        if (x < 20) {
            System.out.print("这是 if 语句");
        } else {
            System.out.print("这是 else 语句");
        }
    }
}
// output:
// 这是 else 语句

if...else if...else 语句

  • if 语句至多有 1 个 else 语句,else 语句在所有的 else if 语句之后。
  • if 语句可以有若干个 else if 语句,它们必须在 else 语句之前。
  • 一旦其中一个 else if 语句检测为 true,其他的 else if 以及 else 语句都将跳过执行。

语法

java
if (布尔表达式 1) {
   // 如果布尔表达式 1 的值为 true 执行代码
} else if (布尔表达式 2) {
   // 如果布尔表达式 2 的值为 true 执行代码
} else if (布尔表达式 3) {
   // 如果布尔表达式 3 的值为 true 执行代码
} else {
   // 如果以上布尔表达式都不为 true 执行代码
}

示例

java
public class IfElseifElseDemo {
    public static void main(String args[]) {
        int x = 3;

        if (x == 1) {
            System.out.print("Value of X is 1");
        } else if (x == 2) {
            System.out.print("Value of X is 2");
        } else if (x == 3) {
            System.out.print("Value of X is 3");
        } else {
            System.out.print("This is else statement");
        }
    }
}
// output:
// Value of X is 3

嵌套的 if...else 语句

使用嵌套的 if else 语句是合法的。也就是说你可以在另一个 if 或者 else if 语句中使用 if 或者 else if 语句。

语法

java
if (布尔表达式 1) {
   // 如果布尔表达式 1 的值为 true 执行代码
   if (布尔表达式 2) {
      // 如果布尔表达式 2 的值为 true 执行代码
   }
}

示例

java
public class IfNestDemo {
    public static void main(String args[]) {
        int x = 30;
        int y = 10;

        if (x == 30) {
            if (y == 10) {
                System.out.print("X = 30 and Y = 10");
            }
        }
    }
}
// output:
// X = 30 and Y = 10

switch 语句

switch 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。

规则

  • switch 语句中的变量类型只能为 byteshortintchar 或者 String
  • switch 语句可以拥有多个 case 语句。每个 case 后面跟一个要比较的值和冒号。
  • case 语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量。
  • 当变量的值与 case 语句的值相等时,那么 case 语句之后的语句开始执行,直到 break 语句出现才会跳出 switch 语句。
  • 当遇到 break 语句时,switch 语句终止。程序跳转到 switch 语句后面的语句执行。
  • case 语句不必须要包含 break 语句。如果没有 break 语句出现,程序会继续执行下一条 case 语句,直到出现 break 语句。
  • switch 语句可以包含一个 default 分支,该分支必须是 switch 语句的最后一个分支。default 在没有 case 语句的值和变量值相等的时候执行。default 分支不需要 break 语句。

语法

java
switch (expression) {
    case value:
       // 语句
       break; // 可选
    case value:
       // 语句
       break; // 可选
    // 你可以有任意数量的 case 语句
    default: // 可选
       // 语句
       break; // 可选,但一般建议加上
}

示例

java
public class SwitchDemo {
    public static void main(String args[]) {
        char grade = 'C';

        switch (grade) {
        case 'A':
            System.out.println("Excellent!");
            break;
        case 'B':
        case 'C':
            System.out.println("Well done");
            break;
        case 'D':
            System.out.println("You passed");
        case 'F':
            System.out.println("Better try again");
            break;
        default:
            System.out.println("Invalid grade");
            break;
        }
        System.out.println("Your grade is " + grade);
    }
}
// output:
// Well done
// Your grade is C

为什么switchfor

我们可以借助 Oracle 官方提供的 JMH(Java Microbenchmark Harness,JAVA 微基准测试套件)框架来进行测试,首先引入 JMH 框架:

java
<!-- https://mvnrepository.com/artifact/org.openjdk.jmh/jmh-core -->
<dependency>
   <groupId>org.openjdk.jmh</groupId>
   <artifactId>jmh-core</artifactId>
   <version>1.23</version>
</dependency>

然后编写测试代码:

java

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.AverageTime) // 测试完成时间
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 2, time = 1, timeUnit = TimeUnit.SECONDS) // 预热 2 轮,每次 1s
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) // 测试 5 轮,每次 1s
@Fork(1) // fork 1 个线程
@State(Scope.Thread) // 每个测试线程一个实例
public class SwitchOptimizeTest {

    static Integer _NUM = 9;

    public static void main(String[] args) throws RunnerException {
        // 启动基准测试
        Options opt = new OptionsBuilder()
                .include(SwitchOptimizeTest.class.getSimpleName()) // 要导入的测试类
                .output("/Users/admin/Desktop/jmh-switch.log") // 输出测试结果的文件
                .build();
        new Runner(opt).run(); // 执行测试
    }

    @Benchmark
    public void switchTest() {
        int num1;
        switch (_NUM) {
            case 1:
                num1 = 1;
                break;
            case 3:
                num1 = 3;
                break;
            case 5:
                num1 = 5;
                break;
            case 7:
                num1 = 7;
                break;
            case 9:
                num1 = 9;
                break;
            default:
                num1 = -1;
                break;
        }
    }

    @Benchmark
    public void ifTest() {
        int num1;
        if (_NUM == 1) {
            num1 = 1;
        } else if (_NUM == 3) {
            num1 = 3;
        } else if (_NUM == 5) {
            num1 = 5;
        } else if (_NUM == 7) {
            num1 = 7;
        } else if (_NUM == 9) {
            num1 = 9;
        } else {
            num1 = -1;
        }
    }
}

结果如下:

可以看到,switch的速度将近是if的2.33倍

为什么 switch 的性能会比 if 的性能高这么多? 这需要从他们字节码说起,我们把他们的代码使用 javac 生成字节码如下所示:

java
public class com.example.optimize.SwitchOptimize {
  static java.lang.Integer _NUM;

  public com.example.optimize.SwitchOptimize();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #7                  // Method switchTest:()V
       3: invokestatic  #12                 // Method ifTest:()V
       6: return

  public static void switchTest();
    Code:
       0: getstatic     #15                 // Field _NUM:Ljava/lang/Integer;
       3: invokevirtual #19                 // Method java/lang/Integer.intValue:()I
       6: tableswitch   { // 1 to 9
                     1: 56
                     2: 83
                     3: 61
                     4: 83
                     5: 66
                     6: 83
                     7: 71
                     8: 83
                     9: 77
               default: 83
          }
      56: iconst_1
      57: istore_0
      58: goto          85
      61: iconst_3
      62: istore_0
      63: goto          85
      66: iconst_5
      67: istore_0
      68: goto          85
      71: bipush        7
      73: istore_0
      74: goto          85
      77: bipush        9
      79: istore_0
      80: goto          85
      83: iconst_m1
      84: istore_0
      85: return

  public static void ifTest();
    Code:
       0: getstatic     #15                 // Field _NUM:Ljava/lang/Integer;
       3: invokevirtual #19                 // Method java/lang/Integer.intValue:()I
       6: iconst_1
       7: if_icmpne     15
      10: iconst_1
      11: istore_0
      12: goto          81
      15: getstatic     #15                 // Field _NUM:Ljava/lang/Integer;
      18: invokevirtual #19                 // Method java/lang/Integer.intValue:()I
      21: iconst_3
      22: if_icmpne     30
      25: iconst_3
      26: istore_0
      27: goto          81
      30: getstatic     #15                 // Field _NUM:Ljava/lang/Integer;
      33: invokevirtual #19                 // Method java/lang/Integer.intValue:()I
      36: iconst_5
      37: if_icmpne     45
      40: iconst_5
      41: istore_0
      42: goto          81
      45: getstatic     #15                 // Field _NUM:Ljava/lang/Integer;
      48: invokevirtual #19                 // Method java/lang/Integer.intValue:()I
      51: bipush        7
      53: if_icmpne     62
      56: bipush        7
      58: istore_0
      59: goto          81
      62: getstatic     #15                 // Field _NUM:Ljava/lang/Integer;
      65: invokevirtual #19                 // Method java/lang/Integer.intValue:()I
      68: bipush        9
      70: if_icmpne     79
      73: bipush        9
      75: istore_0
      76: goto          81
      79: iconst_m1
      80: istore_0
      81: return

  static {};
    Code:
       0: iconst_1
       1: invokestatic  #25                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       4: putstatic     #15                 // Field _NUM:Ljava/lang/Integer;
       7: return
}

这些字节码中最重要的信息是“getstatic #15”,这段代码表示取出“_NUM”变量和条件进行判断。

从上面的字节码可以看出,在 switch 中只取出了一次变量和条件进行比较,而 if 中每次都会取出变量和条件进行比较,因此 if 的效率就会比 switch 慢很多

switch 的两种形态

对于 switch 来说,他最终生成的字节码有两种形态,一种是 tableswitch,另一种是 lookupswitch,决定最终生成的代码使用那种形态取决于 switch 的判断添加是否紧凑,例如到 case 是 1...2...3...4 这种依次递增的判断条件时,使用的是 tableswitch,而像 case 是 1...33...55...22 这种非紧凑型的判断条件时则会使用 lookupswitch,有兴趣可以编译成字节码看看。

当执行一次 tableswitch 时,堆栈顶部的 int 值直接用作表中的索引,以便抓取跳转目标并立即执行跳转。也就是说 tableswitch 的存储结构类似于数组,是直接用索引获取元素的,所以整个查询的时间复杂度是 O(1),这也意味着它的搜索速度非常快。

而执行 lookupswitch 时,会逐个进行分支比较或者使用二分法进行查询,因此查询时间复杂度是 O(log n),所以使用 lookupswitch 会比 tableswitch 慢


循环语句

while 循环

只要布尔表达式为 truewhile 循环体会一直执行下去。

语法

java
while (布尔表达式) {
    // 循环内容
}

示例

java
public class WhileDemo {
    public static void main(String args[]) {
        int x = 10;
        while (x < 20) {
            System.out.print("value of x : " + x);
            x++;
            System.out.print("\n");
        }
    }
}
// output:
// value of x : 10
// value of x : 11
// value of x : 12
// value of x : 13
// value of x : 14
// value of x : 15
// value of x : 16
// value of x : 17
// value of x : 18
// value of x : 19

do...while 循环

对于 while 语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次。

do...while 循环和 while 循环相似,不同的是,do...while 循环至少会执行一次。

语法

java
do {
    // 代码语句
} while (布尔表达式);

示例

java
public class DoWhileDemo {
    public static void main(String args[]) {
        int x = 10;

        do {
            System.out.print("value of x : " + x);
            x++;
            System.out.print("\n");
        } while (x < 20);
    }
}
// output:
// value of x:10
// value of x:11
// value of x:12
// value of x:13
// value of x:14
// value of x:15
// value of x:16
// value of x:17
// value of x:18
// value of x:19

for 循环

虽然所有循环结构都可以用 while 或者 do...while 表示,但 Java 提供了另一种语句 —— for 循环,使一些循环结构变得更加简单。

for 循环执行的次数是在执行前就确定的。

语法

java
for (初始化; 布尔表达式; 更新) {
    // 代码语句
}

示例

java
public class ForDemo {
    public static void main(String args[]) {
        for (int x = 10; x < 20; x = x + 1) {
            System.out.print("value of x : " + x);
            System.out.print("\n");
        }
    }
}
// output:
// value of x : 10
// value of x : 11
// value of x : 12
// value of x : 13
// value of x : 14
// value of x : 15
// value of x : 16
// value of x : 17
// value of x : 18
// value of x : 19

for循环中,前后两个空可以省略,例如:

java
public class test {  
    public static void main(String[] args) {  
        int x = 0;  
        for (; x < 20; ) {  
            System.out.print("value of x : " + x);  
            System.out.print("\n");  
            x++;  
        }  
    }  
}

这样就能实现与while一样的效果了


foreach 循环

Java 5 引入了一种主要用于集合和数组的增强型 for 循环。

语法

java
for (声明语句 : 表达式) {
    // 代码句子
}

示例

java
public class ForeachDemo {
    public static void main(String args[]) {
        int[] numbers = { 10, 20, 30, 40, 50 };

        for (int x : numbers) {
            System.out.print(x);
            System.out.print(",");
        }

        System.out.print("\n");
        String[] names = { "James", "Larry", "Tom", "Lacy" };

        for (String name : names) {
            System.out.print(name);
            System.out.print(",");
        }
    }
}
// output:
// 10,20,30,40,50,
// James,Larry,Tom,Lacy,

它的实现原理是怎样的呢

我们对以下代码进行反编译:

java
for (Integer i : list) { System.out.println(i); }

反编译后:

java
    Integer i;
    for(Iterator iterator = list.iterator(); iterator.hasNext(); System.out.println(i)){
        i = (Integer)iterator.next();        
    }

反编译后的代码其实比较复杂,我们按照执行顺序拆解一下:

Integer i; 定义一个临时变量i

Iterator iterator = list.iterator(); 获取List的迭代器

iterator.hasNext(); 判断迭代器中是否有未遍历过的元素

i = (Integer)iterator.next(); 获取第一个未遍历的元素,赋值给临时变量i

System.out.println(i) 输出临时变量i的值

如此循环往复,直到遍历完List中的所有元素。 通过反编译,我们看到,其实JAVA中的增强for循环底层是通过迭代器模式来实现的。

既然增强for循环通过迭代器实现,那么必然有迭代器的特性。

Java中的Fail-Fast机制

在Java中,fail-fast机制是一种错误检测机制,主要用于在多线程环境下检测集合的并发修改。当多个线程对同一个集合进行操作时,可能会触发fail-fast机制,抛出ConcurrentModificationException异常。

Fail-Fast机制的原理

fail-fast机制是通过迭代器(Iterator)实现的。当使用迭代器遍历集合时,迭代器会维护一个指向集合的单链索引表。如果在迭代过程中,集合的结构发生了变化(例如添加或删除元素),迭代器会立即抛出ConcurrentModificationException异常,而不是继续遍历。

为什么会出现ConcurrentModificationException?

Iterator在工作时,会假设集合的内容不会被修改。如果集合在迭代过程中被修改,迭代器的索引表不会同步更新,导致迭代器在移动指针时找不到要迭代的对象,从而抛出ConcurrentModificationException异常。

如何避免ConcurrentModificationException?

为了避免ConcurrentModificationException,可以使用Iterator自身的remove()方法来删除元素。Iterator.remove()方法会在删除当前迭代对象的同时维护索引的一致性。

代码示例

错误的删除方式

java
for (Student stu : students) {    
    if (stu.getId() == 2)     
        students.remove(stu);  // 会抛出ConcurrentModificationException
}

正确的删除方式

java
Iterator<Student> stuIter = students.iterator();    
while (stuIter.hasNext()) {    
    Student student = stuIter.next();    
    if (student.getId() == 2)    
        stuIter.remove();  // 使用Iterator的remove方法移除当前对象
}

其他遍历删除方式

使用增强的for循环删除元素

java
public void listRemove() {    
    List<Student> students = this.getStudents();    
    for (Student stu : students) {    
        if (stu.getId() == 2)     
            students.remove(stu);  // 会抛出ConcurrentModificationException
    }    
}

使用增强的for循环删除元素后立即跳出

java
public void listRemoveBreak() {    
    List<Student> students = this.getStudents();    
    for (Student stu : students) {    
        if (stu.getId() == 2) {    
            students.remove(stu);    
            break;  // 删除后立即跳出,不会抛出异常
        }    
    }    
}

使用普通的for循环删除元素

java
public void listRemove2() {    
    List<Student> students = this.getStudents();    
    for (int i=0; i<students.size(); i++) {    
        if (students.get(i).getId()%3 == 0) {    
            Student student = students.get(i);    
            students.remove(student);  // 不会抛出异常,但可能导致数据不一致
        }    
    }    
}

使用Iterator删除元素

java
public void iteratorRemove() {    
    List<Student> students = this.getStudents();    
    System.out.println(students);    
    Iterator<Student> stuIter = students.iterator();    
    while (stuIter.hasNext()) {    
        Student student = stuIter.next();    
        if (student.getId() % 2 == 0)    
            stuIter.remove();  // 使用Iterator的remove方法移除当前对象
    }    
    System.out.println(students);    
}

Fail-Fast机制的总结

  • fail-fast机制是为了在多线程环境下检测集合的并发修改。
  • 使用Iterator遍历集合时,如果集合被修改,会抛出ConcurrentModificationException异常。
  • 为了避免异常,应该使用Iteratorremove()方法来删除元素。
  • fail-fast机制并不能保证在所有情况下都会抛出异常,因此不应该依赖它来保证程序的正确性。

中断语句

break 关键字

break 主要用在循环语句或者 switch 语句中,用来跳出整个语句块。

break 跳出最里层的循环,并且继续执行该循环下面的语句。

示例

java
public class BreakDemo {
    public static void main(String args[]) {
        int[] numbers = { 10, 20, 30, 40, 50 };

        for (int x : numbers) {
            if (x == 30) {
                break;
            }
            System.out.print(x);
            System.out.print("\n");
        }

        System.out.println("break 示例结束");
    }
}
// output:
// 10
// 20
// break 示例结束

continue 关键字

continue 适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。

示例

java
public class ContinueDemo {
    public static void main(String args[]) {
        int[] numbers = { 10, 20, 30, 40, 50 };

        for (int x : numbers) {
            if (x == 30) {
                continue;
            }
            System.out.print(x);
            System.out.print("\n");
        }
    }
}
// output:
// 10
// 20
// 40
// 50

return 关键字

跳出整个函数体,函数体后面的部分不再执行。

示例

java
public class ReturnDemo {
    public static void main(String args[]) {
        int[] numbers = { 10, 20, 30, 40, 50 };

        for (int x : numbers) {
            if (x == 30) {
                return;
            }
            System.out.print(x);
            System.out.print("\n");
        }

        System.out.println("return 示例结束");
    }
}
// output:
// 10
// 20

最佳实践

  1. 选择分支特别多的情况下,switch 语句优于 if...else if...else 语句。
  2. switch 语句不要吝啬使用 default
  3. switch 语句中的 default 要放在最后。
  4. foreach 循环优先于传统的 for 循环。
  5. 不要循环遍历容器元素,然后删除特定元素。正确姿势应该是遍历容器的迭代器(Iterator),删除元素。

贡献者

文件历史