Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
fun main() {
for (i in 10 downTo 1 step 3) {
println(i)
}
}10 7 4 1
- Expecting
10 7 4: treating the end ofdownTolike the end ofuntil.downToincludes its end, and4 - 3is exactly1, so1is printed too. - Expecting
9 6 3: readingstep 3as "numbers divisible by three". The step counts from the start,10, and takes away three on each lap.
- What does the loop print with
step 4instead ofstep 3? - How would you write the same loop so that the numbers go up from
1?