Example: While Loop in Kotlin
fun main(args: Array) {
var i = 1
var max = 5
while (i <= max) {
println("$i")
++i
}
}
Output:
1
2
3
4
5
Example: While Loop in Kotlin with Decrementer and Sum
fun main(args: Array) {
var sum = 0
var i = 3
while (i != 0) {
println("i = $i")
sum += i // sum = sum + i;
--i
}
println(" ---\nSum: $sum")
}
Output:
i = 3
i = 2
i = 1
---
Sum: 6
Example: For Loop in Kotlin
fun main(args: Array) {
for (i in 1..5) {
println(i)
}
}
Output:
1
2
3
4
5
Example: For Loop in Kotlin with Step
fun main(args: Array) {
for (i in 1..9 step 2)
println(i)
}
Output:
1
3
5
7
9