Skip to content

Instantly share code, notes, and snippets.

@PatilShreyas

PatilShreyas/1Ranges.kt Secret

Last active Dec 31, 2020
Embed
What would you like to do?
fun main() {
for (i in 0..10) { print(i) } // 012345678910
for (i in 1 until 10) { print(i) } // 123456789 (10 is excluded)
for (i in 9 downTo 0) { print(i) } // 9876543210
for (i in 2..20 step 2) { print("$i ") } // 2 4 6 8 10 12 14 16 18 20
if (50 in 0..100) println(true) else println(false) // true
val list = listOf(1, 2, 3, 4, 5)
if (3 in list) println("3 PRESENT") else println("3 ABSENT") // 3 PRESENT
if (10 !in list) println("10 NOT PRESENT") else println("10 PRESENT") // 10 NOT PRESENT
}
fun main() {
val batteryPercentage = 90
val message = when (batteryPercentage) {
in 1..20 -> "Very low"
in 21..50 -> "Low"
in 51..80 -> "Medium"
in 81..100 -> "Charged"
else -> "Invalid"
}
println(message) // Charged
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment