- Android Development with Kotlin
- Marcin Moskala Igor Wojda
- 609字
- 2021-07-02 18:48:39
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
- 程序員考試案例梳理、真題透解與強化訓練
- 編譯系統(tǒng)透視:圖解編譯原理
- Extreme C
- Kivy Cookbook
- Hands-On JavaScript for Python Developers
- jQuery for Designers Beginner's Guide Second Edition
- 硬件產(chǎn)品設(shè)計與開發(fā):從原型到交付
- 深度學習程序設(shè)計實戰(zhàn)
- 寫給青少年的人工智能(Python版·微課視頻版)
- SCRATCH編程課:我的游戲我做主
- WCF全面解析
- Python趣味創(chuàng)意編程
- 計算機應用基礎(chǔ)(Windows 7+Office 2010)
- 信息學奧林匹克競賽初賽精講精練
- R語言與網(wǎng)站分析