| Start |
|---|
| Looping over iterables You can loop over any iterable by using the standard for-loop: Lots of things in Kotlin are iterable, like number ranges: If you need an index while iterating: There is also a functional approach to iterating included in the standard library, without apparent language constructs, using the forEach function:
|
Repeat an action x times |
| Break and continue Break and continue keywords work like they do in other languages. If you have nested loops, you can label the loop statements and qualify the break and continue statements to specify which loop you want to continue or break: This approach won't work for the functional |
Iterating over a Map in kotlin |
| Recursion Looping via recursion is also possible in Kotlin as in most programming languages. In the example above, the |
| While Loops While and do-while loops work like they do in other languages: In the do-while loop, the condition block has access to values and variables declared in the loop body. |
| Functional constructs for iteration The Kotlin Standard Library (opens new window) also provides numerous useful functions to iteratively work upon collections. For example, the One of the many advantages of this style is it allows to chain operations in a similar fashion. Only a minor modification would be required if say, the list above were needed to be filtered for even numbers. The RemarksIn Kotlin, loops are compiled down to optimized loops wherever possible. For example, if you iterate over a number range, the bytecode will be compiled down to a corresponding loop based on plain int values to avoid the overhead of object creation. |
Read Article
https://develop.horje.com/learn/1434/reference