while loops

The while() { ... } and do { ... } while() statements work in the same way that they work in Java. The while statement takes a condition, and do specifies a block of code that should be invoked while the condition is true. The following example demonstrates how do { ... } while() looks in Kotlin:

fun testWhileLoop() {
val array = arrayOf(1, 2, 3)
do {
var index = 0
println(array[index++])
} while (index < array.lastIndex)
}

As you can see, the do { ... } while construction works in the same way that it does in other C-like languages.