Kotlin continue 跳转结构

2024 年 8 月 29 日 | 阅读 2 分钟

Kotlin, continue 语句用于重复循环。它继续程序的当前流程,并在指定条件处跳过剩余代码。

continue 语句在嵌套循环中仅影响内部循环。

例如

在上面的示例中,当 if 条件执行 continue 时,for 循环重复其循环。continue 语句使循环重复,而不执行 if 条件的以下代码。

Kotlin continue 示例

输出

i = 1
this is below if
i = 2
i = 3
this is below if

Kotlin 标签 continue 表达式

标签是标识符的形式,后跟 @ 符号,例如 abc@, test@。 要将表达式标记为标签,我们只需在表达式前面放置一个标签即可。

Kotlin,标签 continue 表达式用于重复特定循环(标签循环)。这是通过使用带有 @ 符号的 continue 表达式,后跟标签名称 (continue@labelname) 来完成的。

Kotlin 标签 continue 示例

输出

i = 1 and j = 1
this is below if
i = 1 and j = 2
this is below if
i = 1 and j = 3
this is below if
i = 2 and j = 1
i = 3 and j = 1
this is below if
i = 3 and j = 2
this is below if
i = 3 and j = 3
this is below if

下一主题Kotlin 函数