- 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
- 單片機C語言程序設計實訓100例:基于STC8051+Proteus仿真與實戰(zhàn)
- 老“碼”識途
- Securing WebLogic Server 12c
- 軟件測試技術(shù)指南
- 青少年學Python(第1冊)
- Oracle 18c 必須掌握的新特性:管理與實戰(zhàn)
- BeagleBone Black Cookbook
- Getting Started with Hazelcast(Second Edition)
- 匯編語言編程基礎:基于LoongArch
- Learning YARN
- Learning AWS
- Flowable流程引擎實戰(zhàn)
- 大學計算機基礎
- Java EE 7 with GlassFish 4 Application Server
- Python Digital Forensics Cookbook