Kotlin Study Notes (iii)

Source: Internet
Author: User
Tags foreach anonymous arrays
For Loop

The For loop can traverse any object that provides an iterator (iterator), with the following syntax:

Fun Main (args:array<string>) { 
    val arrays = intarrayof (1,2,3,4,5) for
    (item in arrays) println (item)
}

The loop body can be a block of code:

Fun Main (args:array<string>) { 
    val arrays = arrayof (1,2,3,4,5) for
    (item:int in arrays) println (item) 
  
   }
  

As mentioned above, for can iterate through any object that provides an iterator.
If you want to traverse an array or a list through an index , you can do this:
Indices

Fun Main (args:array<string>) { 
    val arrays = arrayof (1,2,3,4,5) for
    (i in arrays.indices) {
      println ( Arrays[i])  
    } 
}

Note that this "traverse over Interval" is compiled into an optimized implementation without creating additional objects.
Or you can use the library function Withindex:

Fun Main (args:array<string>) { 
    val arrays = arrayof (1,2,3,4,5) for
    ((Index,value) in Arrays.withindex ()) {
      println ("Index is $index value is $value")  
    } 
}
While and Do...while loops

While is the most basic loop:

Fun Main (args:array<string>) {
    var maxCount = 3
    while (MaxCount > 0) {
        println (MaxCount-)//3, 2, 1      
    }
}

Do...while Loop for a while statement, you cannot enter a loop if the condition is not met. But sometimes we need to do it at least once, even if we don't meet the conditions.
The Do...while loop is similar to the while loop, but the Do...while loop executes at least once.

Fun Main (args:array<string>) {
    var maxCount = 3 does
    {
        println (MaxCount-)//3
    } while ( MaxCount > 3)   
}
Return and Jump

Kotlin has three structured jump expressions: Return is returned by default from the function or anonymous function that encloses it most directly. Break terminates the loop that encloses it most directly. Continue continues the next most direct loop that surrounds it.

The Kotlin supports the traditional break and continue operators in the loop.

Fun Main (args:array<string>) {for
    (i in 0..10) {
        if (i = = 3) Continue       
        if (i = = 6) break
        println (i) 0, 1, 2, 4, 5
    }
}
Break and Continue tags

Any expression in Kotlin can be tagged with a label. The format of the label is the identifier followed by the @ symbol, for example: abc@, foobar@ are valid tags. To label an expression, we just tag it in front of it.

Fun Main (args:array<string>) {
    qfxl@ for (i-in-1..3) {for
        (J-in 1..2) {
           if (j = = 2) {
               BREAK@QFXL or Continue
           } 
            println (j)//Output only 1
        }
    }

The break of the label limit jumps to the execution point that is just behind the loop specified by the label. Continue continues to label the next iteration of the specified loop. return at label

Kotlin have function literals, local functions, and object expressions. Therefore, Kotlin functions can be nested. The return of the tag limit allows us to return from the outer function. One of the most important uses is to return from a lambda expression.

Fun Main (args:array<string>) {
    foo ()
} fun
foo (): Unit {
    val arrays = arrayof ("A", "B", "C")
    Arrays.foreach {
        if (it = = "B") {
            return
        }        
        println (IT)//print a
    }
}

The return expression is returned from the function that most directly surrounds it-Foo. (Note that this non-local return only supports lambda expressions passed to inline functions.) If we need to return from a lambda expression, it must be tagged and used to restrict return.

Fun Main (args:array<string>) {
    foo ()
} fun
foo (): Unit {
    val arrays = arrayof ("A", "B", "C") C15/>arrays.foreach lit@{
        if (it = = "B") {
            return@lit
        }        
        println (IT)//print A, C
    }
}

Now it will only be returned from the lambda expression. Implicit tagging is usually easier to use. The label has the same name as the function that accepts the lambda.

Fun foo (): Unit {
    val arrays = arrayof ("A", "B", "C")
    Arrays.foreach {
        if (it = = "B") {
            return@foreach< c26/>}        
        println (IT)
    }
}

Alternatively, we use an anonymous function instead of a lambda expression. The return statement inside the anonymous function is returned from the anonymous function itself.

Fun Foo1 () {
    val arrays = arrayof ("A", "B", "C")
    Arrays.foreach (Fun (value:string) {
        if (value = = "B") return< C3/>PRINTLN (value)//print A, C
    })
}

Excerpt from: http://www.runoob.com/kotlin/kotlin-loop-control.html

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.