0
0
Kotlinprogramming~5 mins

For loop with step and downTo in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ACounts up from 10 to 1, jumping 3 numbers each time
BCounts up from 1 to 10, jumping 3 numbers each time
CCounts down from 1 to 10, jumping 3 numbers each time
DCounts down from 10 to 1, jumping 3 numbers each time
Which keyword lets you skip numbers in a Kotlin for loop?
Askip
Bjump
Cstep
DdownTo
What will for (i in 1..5 step 2) print?
A1 2 3 4 5
B1 3 5
C5 3 1
D2 4
What is the output of this code?
for (i in 3 downTo 1) {
 println(i)
}
A3 2 1
BError
C1 3
D1 2 3
Can step be used with downTo in Kotlin?
AYes, it works with both increasing and decreasing ranges
BNo, only with increasing ranges
COnly with <code>until</code>
DOnly with <code>rangeTo</code>
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.