kotlin while code example
Example 1: kotlin while
var i = 1
while (i < 5) { // while loop
println(i)
i++
}
var i = 1
do { // do-while loop
println(i)
i++
} while (i < 5)
Example 2: Kotlin when
val x = 3
when(x) {
3 -> println("yes")
8 -> println("no")
else -> println("maybe")
}
// when can also be used as an expression!
val y = when(x) {
3 -> "yes"
8 -> "no"
else -> "maybe"
}
println(y) // "yes"
Example 3: kotlin while loop
while ( isTrue() );
while ( isTrue() ) {
println("while!")
}
do {
println("do while!")
} while (isTrue())
Example 4: while loop kotlin
for (item in collection) print(item)
Example 5: kotlin if
if (a > b) {
max = a
} else {
max = b
}