Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
kotlin for loop is weird
//normal for in
fun fizzBuzz(start: Int = 1, end: Int = 15) {
for(foobar in start..end) {
println(when {
foobar % 5 == 0 && foobar % 3 == 0 -> "FizzBuzz"
foobar % 3 == 0 -> "Fizz"
foobar % 5 == 0 -> "Buzz"
else -> "$foobar"
})
}
}
//equivalent to for(start = 1; start < end; start+=2) ?
fun fizzBuzz(start: Int = 1, end: Int = 15) {
for(foobar in start..end step 2) {
println(when {
foobar % 5 == 0 && foobar % 3 == 0 -> "FizzBuzz"
foobar % 3 == 0 -> "Fizz"
foobar % 5 == 0 -> "Buzz"
else -> "$foobar"
})
}
}
//equivalent to for(end = 15; end > start; end-=2) ?
fun fizzBuzz(start: Int = 1, end: Int = 15) {
for(foobar in end downTo start step 2) {
println(when {
foobar % 5 == 0 && foobar % 3 == 0 -> "FizzBuzz"
foobar % 3 == 0 -> "Fizz"
foobar % 5 == 0 -> "Buzz"
else -> "$foobar"
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment