Kotlin - Continue继续

Kotlin - Continue继续 首页 / Kotlin入门教程 / Kotlin - Continue继续

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 continue 表达式

标记(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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Android开发高手课 -〔张绍文〕

许式伟的架构课 -〔许式伟〕

透视HTTP协议 -〔罗剑锋(Chrono)〕

Linux实战技能100讲 -〔尹会生〕

Redis源码剖析与实战 -〔蒋德钧〕

朱涛 · Kotlin编程第一课 -〔朱涛〕

说透元宇宙 -〔方军〕

后端工程师的高阶面经 -〔邓明〕

结构执行力 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)