Kotlin For Loop

 package com.example.my_kotlin_practice


fun main(args: Array<String>) {
for(i in 1..10) {
println("The value of 'i' is : $i")
}
print("\n")

for(i in 1..10 step 2) {
println(i)
}
print("\n")

for(i in 1 until 10) {
println("The value of 'i'(using 'until') is : $i")
}
print("\n")

for(i in 10 downTo 1) {
println("The value of 'i'(using 'downTo') is : $i")
}
print("\n")

val num = 3
for(i in 1..10) {
println("$num * $i = ${num*i}")
}
}

OUTPUT :

The value of 'i' is : 1 The value of 'i' is : 2 The value of 'i' is : 3 The value of 'i' is : 4 The value of 'i' is : 5 The value of 'i' is : 6 The value of 'i' is : 7 The value of 'i' is : 8 The value of 'i' is : 9 The value of 'i' is : 10 1 3 5 7 9 The value of 'i'(using 'until') is : 1 The value of 'i'(using 'until') is : 2 The value of 'i'(using 'until') is : 3 The value of 'i'(using 'until') is : 4 The value of 'i'(using 'until') is : 5 The value of 'i'(using 'until') is : 6 The value of 'i'(using 'until') is : 7 The value of 'i'(using 'until') is : 8 The value of 'i'(using 'until') is : 9 The value of 'i'(using 'downTo') is : 10 The value of 'i'(using 'downTo') is : 9 The value of 'i'(using 'downTo') is : 8 The value of 'i'(using 'downTo') is : 7 The value of 'i'(using 'downTo') is : 6 The value of 'i'(using 'downTo') is : 5 The value of 'i'(using 'downTo') is : 4 The value of 'i'(using 'downTo') is : 3 The value of 'i'(using 'downTo') is : 2 The value of 'i'(using 'downTo') is : 1 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27 3 * 10 = 30 Process finished with exit code 0

Comments

Popular posts from this blog

Maps (Collections) in kotlin

Functions in Kotlin

Arrays in Kotlin