Kotlin 简明教程

Kotlin - Break and Continue

Kotlin Break Statement

Kotlin break 语句用于在满足特定条件后退出循环。该循环可以是 forwhiledo…​while 循环。

Kotlin break statement is used to come out of a loop once a certain condition is met. This loop could be a for, while or do…​while loop.

Syntax

让我们检查终止各种类型的循环以退出它们的语法:

Let’s check the syntax to terminate various types of loop to come out of them:

// Using break in for loop
for (...) {
   if(test){
      break
   }
}

// Using break in while loop
while (condition) {
   if(test){
      break
   }
}

// Using break in do...while loop
do {
   if(test){
      break
   }
}while(condition)

如果 test 表达式计算为 true,将执行 break 语句,该语句将终止循环,程序继续执行循环语句之后。

If test expression is evaluated to true, break statment is executed which terminates the loop and program continues to execute just after the loop statment.

Example

以下是 while 循环在计数器变量 i 值变为 3 时终止的示例:

Following is an example where the while loop terminates when counter variable i value becomes 3:

fun main(args: Array<String>) {
   var i = 0;
   while (i++ < 100) {
      println(i)
      if( i == 3 ){
         break
      }

   }
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

1
2
3

Kotlin Labeled Break Statement

Kotlin label 是标识符后跟 @ 标志的形式,例如 test@ 或 outer@。要将任何 Kotlin 表达式标记为一个已标记的表达式,我们只需要在表达式的前面放置一个标签。

Kotlin label is the form of identifier followed by the @ sign, for example test@ or outer@. To make any Kotlin Expression as labeled one, we just need to put a label in front of the expression.

outerLoop@ for (i in 1..100) {
    // ...
}

Kotlin labeled break 语句用于终止特定循环。这是通过使用带有 @ 符号的 break 表达式完成的,后面跟标签名称 (break@LabelName)。

Kotlin labeled break statement is used to terminate the specific loop. This is done by using break expression with @ sign followed by label name (break@LabelName).

fun main(args: Array<String>) {
    outerLoop@ for (i in 1..3) {
        innerLoop@ for (j in 1..3) {
            println("i = $i and j = $j")
            if (i == 2){
                break@outerLoop
            }
        }
    }
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

i = 1 and j = 1
i = 1 and j = 2
i = 1 and j = 3
i = 2 and j = 1

Kotlin Continue Statement

Kotlin continue 语句中断循环迭代(跳过 continue 语句旁边的部分,直到循环结束),然后继续循环中的下一个迭代。

The Kotlin continue statement breaks the loop iteration in between (skips the part next to the continue statement till end of the loop) and continues with the next iteration in the loop.

Syntax

让我们检查终止各种类型的循环以退出它们的语法:

Let’s check the syntax to terminate various types of loop to come out of them:

// Using continue in for loop
for (...) {
   if(test){
      continue
   }
}

// Using continue in while loop
while (condition) {
   if(test){
      continue
   }
}

// Using continue in do...while loop
do {
   if(test){
      continue
   }
}while(condition)

如果 test 表达式计算为 true,将执行 continue 语句,该语句将跳过循环的剩余部分并跳转到循环语句的下一个迭代。

If test expression is evaluated to true, continue statment is executed which skips remaning part of the loop and jump to the next iteration of the loop statment.

Example

以下是 while 循环在变量 i 的值为 3 时跳过打印该变量的示例:

Following is an example where the while loop skips printing variable i when its value is 3:

fun main(args: Array<String>) {
   var i = 0;
   while (i++ < 6) {
      if( i == 3 ){
         continue
      }
      println(i)
   }
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

1
2
4
5
6

Kotlin Labeled Continue Statement

Kotlin labeled continue 语句用于跳过特定循环的一部分。这是通过使用带有 @ 符号的 continue 表达式来完成的,后面跟标签名称 (continue@LabelName)。

Kotlin labeled continue statement is used to skip the part of a specific loop. This is done by using continue expression with @ sign followed by label name (continue@LabelName).

fun main(args: Array<String>) {
    outerLoop@ for (i in 1..3) {
        innerLoop@ for (j in 1..3) {
            if (i == 2){
                continue@outerLoop
            }
            println("i = $i and j = $j")
        }
    }
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

i = 1 and j = 1
i = 1 and j = 2
i = 1 and j = 3
i = 3 and j = 1
i = 3 and j = 2
i = 3 and j = 3