The for loop with a value range

Kotlin, unlike Java, supports looping through a range of values such as 1 to 10 using a for loop, as in the following:

for (i in 1..10) {
if (i % 2 == 0) {
println("$i is an even number");
} else {
println("$i is an odd number");
}
}

Here the number range is specified by the 1..10 syntax.

Furthermore, Kotlin supports looping through ranges in reverse as well as with custom specified stepping values, seen as follows:

for (i in 10 downTo 0 step 2) {
println("$i");
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.252.204