📜  Kotlin 中的返回、跳转和标签

📅  最后修改于: 2022-05-13 01:55:39.364000             🧑  作者: Mango

Kotlin 中的返回、跳转和标签

Kotlin 是由 JetBrains 开发的一种静态类型的通用编程语言,它构建了 IntelliJ IDEA、PhpStorm、Appcode 等世界一流的 IDE。它于 2011 年由 JetBrains 首次引入,是一种用于 JVM 的新语言。 Kotlin 是一种面向对象的语言,是一种比Java“更好的语言”,但仍可与Java代码完全互操作。正如 Kotlin 所说:Kotlin 具有以下三种结构跳转表达式:

  1. 返回
  2. 休息
  3. 继续

正如 Kotlin 所说,这些可以是大型表达式的一部分。所以,让我们从“返回”开始。

1.返回

这是我们通常在声明期间在函数中使用的语句,用于在函数执行后返回值。默认情况下,它从最近的封闭函数或匿名函数返回。我们举个例子,

例子:

Kotlin
fun add(a:Int,b:Int) : Int {
   // ans having final value after execution
   val ans = a + b 
   // here we have returned it
   return ans 
 }
 fun main(args: Array) {
  
    val first: Int = 10
    val second: Int = 20
    // called function and 
    // Collected the returned value in sum
    val sum = add(first,second)
    println("The sum is: $sum")
}


Kotlin
fun GfG() {
    listOf(1, 2, 3, 4, 5).forEach {
        // non-local return directly
          // to the caller of GfG()
        if (it == 3) return 
        print(it)
    }
    println("this point is unreachable")
}


Kotlin
fun main(args : Array){
    for(ch in 'A'..'C'){    // Outer loop
        for (n in 1..4){     // Inner loop
            println("processing...")
            if(n == 2)     // it will terminate Inner loop
                break
        }
        if(ch == B)
            break     // but it will terminate Outer loop
    }
}


Kotlin
fun main(args : Array){
   Outerloop@ for(ch in 'A'..'C'){    // Outer loop
        for (n in 1..4){     // Inner loop
            println("processing...")
            if(n == 2)     // it will terminate Outerloop directly
                break@Outerloop
        }
        // here we don't need it
       /*   
         if(ch==B)
                break
        */
    }
}


Kotlin
fun main(args: Array) {
    // outerloop is label name 
    outerloop@ for (i in 1..5) {
        for (j in 1..4) {
            if (i == 3 || j == 2)
                // here we have used that
                continue@outerloop 
            println("Happy Diwali!")
        }
    }
}


return 的另一种用法

科特林

fun GfG() {
    listOf(1, 2, 3, 4, 5).forEach {
        // non-local return directly
          // to the caller of GfG()
        if (it == 3) return 
        print(it)
    }
    println("this point is unreachable")
}

所以,这就是 return 语句的工作方式。

2. 跳转和标签

2.1。休息

break 语句用于终止循环流。但仅终止最近的封闭循环,即如果您有两个嵌套的for循环并且内部for循环中存在break语句,则内部for循环将首先终止,然后,如果添加另一个break,则外部 for 循环也将终止。

例子:

科特林

fun main(args : Array){
    for(ch in 'A'..'C'){    // Outer loop
        for (n in 1..4){     // Inner loop
            println("processing...")
            if(n == 2)     // it will terminate Inner loop
                break
        }
        if(ch == B)
            break     // but it will terminate Outer loop
    }
}

我们还可以使用标签优化上述代码或减少代码行。

2.2.标签

Kotlin 中的任何表达式都可以用标签进行标记。标签的形式为标识符后跟@ 符号,例如name@ 或xyz@。要标记表达式,只需在其前面添加一个标签。

例子:

科特林

fun main(args : Array){
   Outerloop@ for(ch in 'A'..'C'){    // Outer loop
        for (n in 1..4){     // Inner loop
            println("processing...")
            if(n == 2)     // it will terminate Outerloop directly
                break@Outerloop
        }
        // here we don't need it
       /*   
         if(ch==B)
                break
        */
    }
}

2.3.继续

它与 break 语句相同,但唯一的区别是,break 语句终止循环的整个迭代,而 continue 跳过当前迭代,我们也可以在这里使用标签。

例子:

科特林

fun main(args: Array) {
    // outerloop is label name 
    outerloop@ for (i in 1..5) {
        for (j in 1..4) {
            if (i == 3 || j == 2)
                // here we have used that
                continue@outerloop 
            println("Happy Diwali!")
        }
    }
}

这就是返回跳跃 标签。