官术网_书友最值得收藏!

Break and continue

All loops in Kotlin support classic break and continue statements. The continue statement proceeds to the next iteration of that loop, while break stops the execution of the most inner enclosing loop:

    val range = 1..6 
 
    for(i in range) { 
        print("$i ") 
    } 
 
    // prints: 1 2 3 4 5 6 

Now let's add a condition and break the iteration when this condition is true:

    val range = 1..6 
 
    for(i in range) { 
        print("$i ") 
 
        if (i == 3) 
            break 
    } 
 
    // prints: 1 2 3 

The break and continue statements are especially useful when dealing with nested loops. They may simplify our control flow and significantly decrease the amount of work performed to save priceless Android resources. Let's perform a nested iteration and break the outer loop:

    val intRange = 1..6 
    val charRange = 'A'..'B' 
 
    for(value in intRange) { 
        if(value == 3) 
            break 
 
        println("Outer loop: $value ") 
 
        for (char in charRange) { 
            println("\tInner loop: $char ") 
        } 
    } 
 
    // prints 
    Outer loop: 1  
        Inner loop: A  
        Inner loop: B  
    Outer loop: 2  
        Inner loop: A  
        Inner loop: B  

We used a break statement to terminate the outer loop at the beginning of the third iteration, so the nested loop was also terminated. Notice the usage of the \t escaped sequence, which adds indents on the console. We can also utilize the continue statement to skip the current iteration of the loop:

    val intRange = 1..5 
 
    for(value in intRange) { 
        if(value == 3) 
            continue 
 
        println("Outer loop: $value ") 
 
        for (char in charRange) { 
            println("\tInner loop: $char ") 
        } 
    } 
 
    // prints 
    Outer loop: 1  
        Inner loop: A  
        Inner loop: B  
    Outer loop: 2  
        Inner loop: A  
        Inner loop: B  
    Outer loop: 4  
        Inner loop: A  
        Inner loop: B  
    Outer loop: 5  
        Inner loop: A  
        Inner loop: B  

We skip the iteration of the outer loop when the current value equals 3.

Both continue and break statements perform corresponding operations on the enclosing loop. There are, however, times when we want to terminate or skip the iteration of one loop from within another; for example, to terminate an outer loop iteration from within an inner loop:

    for(value in intRange) { 
        for (char in charRange) { 
            // How can we break outer loop here? 
        } 
    } 

Fortunately, both continue and break statements have two forms--labeled and unlabeled. We already saw unlabeled; now we will need labeled to solve our problem. Here is an example of how a labeled break might be used:

    val charRange = 'A'..'B' 
    val intRange = 1..6 
 
    outer@ for(value in intRange) { 
        println("Outer loop: $value ") 
 
        for (char in charRange) { 
            if(char == 'B') 
                break@outer 
 
            println("\tInner loop: $char ") 
        } 
    } 
 
    // prints 
    Outer loop: 1  
        Inner loop: A  

The @outer is the label name. By convention, the label name always starts with @ followed by the label name. The label is placed before the loop. Labeling the loop allows us to use a qualified break (break@outer), which is a way to stop execution of a loop that is referenced by this label. The preceding qualified break (a break with a label) jumps to the execution point right after the loop marked with that label.

Placing the return statement will break all the loops and return from enclosing an anonymous or named function:

    fun doSth() { 
        val charRange = 'A'..'B' 
        val intRange = 1..6 
 
        for(value in intRange) { 
            println("Outer loop: $value ") 
 
            for (char in charRange) { 
                println("\tInner loop: $char ")  
                return 
            } 
        }   
    } 
 
    //usage 
    println("Before method call") 
    doSth() 
    println("After method call") 
 
    // prints
Outer loop: 1
Inner loop: A

After the method call:

    Outer loop: 1  
        Inner loop: A  
主站蜘蛛池模板: 苍山县| 广灵县| SHOW| 灵山县| 保山市| 无棣县| 商水县| 大庆市| 囊谦县| 南宁市| 新绛县| 汾西县| 雅江县| 和林格尔县| 连南| 南岸区| 麦盖提县| 中江县| 龙胜| 嘉祥县| 鄯善县| 海阳市| 德阳市| 安多县| 固安县| 沛县| 孟津县| 沙洋县| 阳城县| 安塞县| 白银市| 慈利县| 神农架林区| 和顺县| 宁武县| 富裕县| 四子王旗| 临高县| 宾川县| 筠连县| 湖口县|