August 2018
Beginner
178 pages
4h 56m
English
For loops have a slightly different syntax than in other languages (Java, C#, C++). Index based for loops, like those found in other languages don't exist in Kotlin. In Kotlin you use for loop for iterating over anything that provides anIterable interface. The next example shows how to loop from 0 to 10 and print these numbers out (10 is included):
for (i: Int in 0..10) { println(i)}
The .. syntax creates an IntRange type which implements the Iterable interface, in other words, it can be iterated over. For loops always use the in keyword for iteration. This keyword can also be used to check if a value is contained inside an Iterable interface:
if (5 in 1..10) print("5 found in range")
Type inference also works inside for loop syntax, ...
Read now
Unlock full access