0
0
Kotlinprogramming~5 mins

For loop with ranges in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a for loop with a range in Kotlin?
A for loop with a range in Kotlin repeats a block of code for each number in a sequence defined by a range, like 1..5, which means from 1 to 5 inclusive.
Click to reveal answer
beginner
How do you write a for loop in Kotlin that counts from 1 to 5?
You write: <br>
for (i in 1..5) {<br>  println(i)<br>}
This prints numbers 1 through 5, one per line.
Click to reveal answer
beginner
What does the range 1..5 mean in Kotlin?
It means all numbers starting at 1 and going up to 5, including both 1 and 5.
Click to reveal answer
intermediate
How can you loop backwards from 5 down to 1 in Kotlin?
Use the downTo keyword: <br>
for (i in 5 downTo 1) {<br>  println(i)<br>}
This counts down from 5 to 1.
Click to reveal answer
intermediate
What does the step keyword do in a Kotlin for loop?
It changes how much the loop variable increases or decreases each time. For example, for (i in 1..10 step 2) counts 1, 3, 5, 7, 9.
Click to reveal answer
What does for (i in 1..3) do in Kotlin?
ALoops with i = 1, 2, 3
BLoops with i = 1, 2
CLoops with i = 0, 1, 2, 3
DLoops with i = 3, 2, 1
How do you write a loop that counts down from 5 to 1?
Afor (i in 5..1)
Bfor (i in 1..5)
Cfor (i in 5 downTo 1)
Dfor (i in 1 downTo 5)
What does step 3 do in a Kotlin for loop?
AEnds the loop at 3
BIncreases the loop variable by 3 each time
CStarts the loop at 3
DRuns the loop 3 times
Which of these is a valid Kotlin range?
A1..5
B5..1
C1...5
D1-5
What will this code print? <br>for (i in 1..5 step 2) println(i)
A1 2 3 4 5
B5 3 1
C2 4
D1 3 5
Explain how to use a for loop with a range in Kotlin to count from 1 to 10.
Think about how to write the loop header and what the range means.
You got /4 concepts.
    Describe how to loop backwards from 10 to 1 using Kotlin ranges.
    Remember Kotlin has a special keyword for counting down.
    You got /3 concepts.