Loops
Kotlin supports all the commonly used loops: for
, while
, do-while
for
for
in Kotlin works the same way as in most languages.
fun main(args: Array<String>) {
val cakes = listOf("carrot", "cheese", "chocolate")
for (cake in cakes) { // 1
println("Yummy, it's a $cake cake!")
}
}
- Loops through each cake in the list.
while
and do-while
while
and do-while
constructs work similarly to most languages as well.
fun eatACake() = println("Eat a Cake")
fun bakeACake() = println("Bake a Cake")
fun main(args: Array<String>) {
var cakesEaten = 0
var cakesBaked = 0
while (cakesEaten < 5) { // 1
eatACake()
cakesEaten ++
}
do { // 2
bakeACake()
cakesBaked++
} while (cakesBaked < cakesEaten)
}
- Executes the block while the condition is true.
- Executes the block first and then checking the condition.
Iterators
You can define your own iterators in your classes by implementing the iterator
operator in them.
class Animal(val name: String)
class Zoo(val animals: List<Animal>) {
operator fun iterator(): Iterator<Animal> { // 1
return animals.iterator() // 2
}
}
fun main() {
val zoo = Zoo(listOf(Animal("zebra"), Animal("lion")))
for (animal in zoo) { // 3
println("Watch out, it's a ${animal.name}")
}
}
- Defines an iterator in a class. It must be named
iterator
and have theoperator
modifier. - Returns the iterator that meets the following method requirements:
next()
:Animal
hasNext()
:Boolean
- Loops through animals in the zoo with the user-defined iterator.
The iterator can be declared in the type or as an extension function.