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?✗ Incorrect
The range 1..3 includes 1, 2, and 3, so the loop runs with i equal to those values.
How do you write a loop that counts down from 5 to 1?
✗ Incorrect
The
downTo keyword counts backwards from 5 down to 1.What does
step 3 do in a Kotlin for loop?✗ Incorrect
The
step keyword changes how much the loop variable increases each time.Which of these is a valid Kotlin range?
✗ Incorrect
The correct syntax for a range is
start..end, like 1..5.What will this code print? <br>
for (i in 1..5 step 2) println(i)✗ Incorrect
The loop counts from 1 to 5, increasing by 2 each time, so it prints 1, 3, and 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.