Recall & Review
beginner
What does the
downTo keyword do in a Kotlin for loop?It creates a range that counts backwards from a higher number down to a lower number.
Click to reveal answer
beginner
How do you make a Kotlin for loop skip numbers when counting?
Use the
step keyword to specify how many numbers to skip each time.Click to reveal answer
intermediate
Write a Kotlin for loop that counts down from 10 to 1, stepping by 2.
for (i in 10 downTo 1 step 2) {
println(i)
}
Click to reveal answer
intermediate
What will this Kotlin code print?
for (i in 5 downTo 1 step 2) {
println(i)
}It will print:
5
3
1
Because it counts down from 5 to 1, skipping every second number.
Click to reveal answer
beginner
Can you use
step with a normal increasing range in Kotlin?Yes! For example,
for (i in 1..10 step 3) counts from 1 to 10, jumping 3 numbers each time.Click to reveal answer
What does
for (i in 10 downTo 1 step 3) do?✗ Incorrect
The
downTo keyword makes the loop count backwards, and step 3 makes it jump 3 numbers each time.Which keyword lets you skip numbers in a Kotlin for loop?
✗ Incorrect
step controls how many numbers the loop skips each time.What will
for (i in 1..5 step 2) print?✗ Incorrect
It counts up from 1 to 5, stepping by 2, so it prints 1, 3, and 5.
What is the output of this code?
for (i in 3 downTo 1) {
println(i)
}✗ Incorrect
The loop counts down from 3 to 1, printing 3, then 2, then 1.
Can
step be used with downTo in Kotlin?✗ Incorrect
step works with both downTo (decreasing) and normal increasing ranges.Explain how to use
downTo and step together in a Kotlin for loop.Think about counting backwards and skipping numbers.
You got /3 concepts.
Describe what happens when you use
step in a Kotlin for loop with an increasing range.Imagine counting forward but only saying every other number.
You got /3 concepts.