Tsallaka zuwa abun ciki
Justice Tech School

Kotlin Loops

for iterates over ranges and collections, while and do while repeat on a condition. break and continue control loops and labels target outer loops.

Example
for (i in 1..3) println("i=$i")
var n = 3
while (n > 0) { n-- }
val items = listOf("a", "b")
for ((i, v) in items.withIndex()) println("$i $v")

Exercise

Loop over a range.

for (i  1..3)

Quiz Test

What does withIndex give?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.