Kotlin,continue语句用于重复循环。它继续程序的当前流程,并在指定条件下跳过其余代码。
嵌套循环中的continue语句仅影响内部循环。
例如
for(..){ //判断是否满足条件 if(checkCondition){ continue } }
在上面的示例中,if条件执行继续,则for循环重复其循环。 continue语句重复执行循环,而不执行下面的if条件代码。
kotlin continue 示例
fun main(args: Array) { for (i in 1..3) { println("i = $i") if (j == 2) { continue } println("this is below if") } }
输出:
i = 1 this is below if i = 2 i = 3 this is below if
标记(Labeled)的是标识符的形式,后跟@符号,例如abc@,test@。为了使表达式成为标签,无涯教程只需在表达式前面放置一个标签。
kotlin labeled continue例子
fun main(args: Array<String>) { labelname@ for (i in 1..3) { for (j in 1..3) { println("i = $i and j = $j") if (i == 2) { continue@labelname } println("this is below if") } } }
输出:
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
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)