- 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
- LabVIEW Graphical Programming Cookbook
- R語言經典實例(原書第2版)
- 造個小程序:與微信一起干件正經事兒
- Windows系統管理與服務配置
- Java 9 Programming Blueprints
- Web Application Development with MEAN
- JavaScript by Example
- Visual C#通用范例開發金典
- Visual Basic程序設計習題與上機實踐
- Android嵌入式系統程序開發:基于Cortex-A8(第2版)
- 并行編程方法與優化實踐
- WordPress Search Engine Optimization(Second Edition)
- HikariCP數據庫連接池實戰
- Build Your Own PaaS with Docker
- Java EE互聯網輕量級框架整合開發:SSM+Redis+Spring微服務(上下冊)