Kotlin programming break label and return label and continue label

Source: Internet
Author: User
Tags foreach anonymous

Kotlin Programming related knowledge points : Kotlin programming using IntelliJ IED and understanding Source files (. kt) Kotlin programming and other properties Kotlin programming methods such as using Kotlin programming Kotlin programming of the parent class and the inherited parent class Kotlin programming interface and implementation interface Kotlin programming of the associated object, abstract class, sealed class Kotlin programming nested class, Inner class, Anonymous inner class Kotlin programming object expression and declaration Kotlin programming extension method Kotlin programming of extended attributes and extended companion objects Kotlin programming of generic Kotlin programming if statement, while loop, when expression, for loop Kotlin programmatic visibility modifier (private,protected,internal, Public) Kotlin programming proxy mode Kotlin high-order function, lambda expression, anonymous function

Kotlin the following three types of jump operators in programming:

Return: Close the nearest closed loop

Break: End the nearest closed loop

Continue: Jumps to the next loop of the nearest closed loop break and Continue tags

Case 1: Using BREADK and Continue labels in a single for loop

Write the following code:

Package com.xingen.kotlin.day201761 Fun

Main (args:array<string>) {
    test1 ()
}
fun  test1 ( {
    for (j in 0..10) {
            if (j==5) {
                println ("Current value is $j, use break label to jump out of loop") Break
            }else{
                println (" Current value is $j, use continue label to continue Next loop ")
                continue
            }
    }
}

Output Result:

The current value is 0, use the Continue label to continue the next loop with the current value of
1, use the Continue label to continue the next loop with the
current value of 2, and use the continue label to continue the next loop with the
current value as 3, use the continue tag to continue the next loop
current value is 4, use the Continue label to continue the next loop
current value is 5, use the break label to jump out of the loop

Case 2: Nesting for loop, specifying jump to outermost for loop

Write the following code:

Package com.xingen.kotlin.day201761 Fun

Main (args:array<string>) {
    test2 ()
}

/**
 *  Nested for loop, specify jump to outermost for loop:
 *
 *  1. Customize a label, xxx@
 *  2. Add a custom label to the outermost for loop, Use break@xxx or continue@xxx to jump */
 Fun
test2 () {
    loop@ for (j in 0..10) {for
        (i in J.). {
            if (i==5&&j = = 5) {
                println ("When the outer value is $j the inner layer value is $i, use the break tag to jump out of the loop")
                Break@loop
            } else {
  
   println ("When the outer value is $j the inner layer value is $i, use the Continue label to continue the Next loop") Continue@loop}}}

  

Output Result:

When the outer value is 0 with an inner value of 0, use the Continue label to continue the next loop
when the outer value is 1 the inner value is 1, use the Continue label to continue the next loop
when the outer value is 2, and the next loop is continued with the Continue label
when the outer value is 3 with an inner value of 3, use the Continue label to continue the next loop
when the outer value is 4 the inner value is 4, use the Continue label to continue the next loop
when the outer value is 5, and the break label jumps out of the loop.
return label

return returns the outermost function :

A return statement without a label is always returned in a function declared with the FUN keyword.
This means that the return in the lambda expression will be returned from the function that contains it.

Package com.xingen.kotlin.day201761 Fun

Main (args:array<string>) {

    test2 {change ()}
}
/**
 * Higher order function, accept a function as parameter */fun
test2 (changefun: ()->unit) {
    changefun ()
    println (" The lambda expression of the advanced function finishes ")
}

var  list= listof<int> (/**)
 * The  function passed in as a parameter
 */ Fun Change
() {
    /**
     * lambda expression, single parameter, when you can omit the declaration parameter and
     * *,  The following code equals:
     *      List.foreach {
     *          if (item==2) return
     *          println ("The value currently traversed is $item")
     *      }

    */ List.foreach {
         item->
          if (item==2) return
          println ("The currently traversed value is $item")
    }
    println (" List collection traversal completed ")
}

The output results are as follows:

The currently traversed value is the 1
advanced function of the lambda expression after the execution

The output finds that return returns in the Test2 function, which is the function that contains the lambda expression as a parameter.

return returns the nearest closed function :

If you want to return to the nearest closed function, that is, in change () instead of test2 (), you can use the label. The label can be an incoming
A label with the same name as the lambda expression

Fun Change () {
    List.foreach  {
         item->
          if (item==2) Return@foreach//Use tag
          println ("The currently traversed value is $ Item ")
    }
    println (" list collection Traversal complete ")
}

The output results are as follows:

The currently traversed value is 1 the
current traversed value is 3 the
list collection traverses the completion
of the lambda expression of the advanced function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.