Repeat! Again!

Kotlin loops are intuitive and easy to read.


Repeat over a range (for)

for (i in 1..10) {
    println("Current number is $i")
}

Translation:

(For i in the range 1 to 10) repeat:
    print the message

The .. symbol is a cute way to say “from here to there.”


Take items from a bundle

val foods = listOf("chicken", "pizza", "tteokbokki")

for (food in foods) {
    println("$food sounds delicious!")
}

Translation:

Repeat by taking each item from foods and calling it food:
    print the message

Repeat while a condition is true (while)

var energy = 3

while (energy > 0) {
    println("Energy left: $energy")
    energy--
}

Translation:

While (energy is greater than 0) keep repeating:
    print energy and subtract one

With Kotlin loops, you can process lots of data in no time.