Junior — Middle
How can you implement a code execution loop in Kotlin?
sobes.tech AI
Answer from AI
In Kotlin, several types of loops can be used to organize iterations:
forloop — iterates over elements of a collection or range:
for (i in 1..5) {
println(i) // Will print numbers from 1 to 5
}
whileloop — executes the loop body as long as the condition is true:
var i = 0
while (i < 5) {
println(i)
i++
}
do..whileloop — executes the loop body first, then checks the condition:
var i = 0
do {
println(i)
i++
} while (i < 5)
These constructs allow for repeated execution of code in Kotlin.