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:
for (i in 1..5) { println(i) }
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:
for (i in 5 downTo 1) { println(i) }
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
✗ 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?
Afor (i in 5..1)
Bfor (i in 1..5)
Cfor (i in 5 downTo 1)
Dfor (i in 1 downTo 5)
✗ Incorrect
The downTo keyword counts backwards from 5 down to 1.
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
✗ Incorrect
The step keyword changes how much the loop variable increases each time.
Which of these is a valid Kotlin range?
A1..5
B5..1
C1...5
D1-5
✗ Incorrect
The correct syntax for a range is start..end, like 1..5.
What will this code print? for (i in 1..5 step 2) println(i)
A1 2 3 4 5
B5 3 1
C2 4
D1 3 5
✗ 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.
Practice
(1/5)
1. What does the following Kotlin code do? for (i in 1..5) { println(i) }
easy
A. Prints numbers from 1 to 4
B. Prints numbers from 1 to 5 inclusive
C. Prints numbers from 0 to 5
D. Prints numbers from 5 down to 1
Solution
Step 1: Understand the range operator
The operator 1..5 creates a range including numbers 1, 2, 3, 4, and 5.
Step 2: Loop through the range
The for loop runs from 1 to 5, printing each number.
Final Answer:
Prints numbers from 1 to 5 inclusive -> Option B
Quick Check:
Range 1..5 includes 5 [OK]
Hint: .. means inclusive range from start to end [OK]
Common Mistakes:
Thinking .. excludes the last number
Confusing range with step or downTo
Assuming it counts down automatically
2. Which of the following is the correct syntax for a for loop counting down from 5 to 1 in Kotlin?
easy
A. for (i in 5 downTo 1) { println(i) }
B. for (i in 1..5 step -1) { println(i) }
C. for (i in 5..1) { println(i) }
D. for (i in 1 downTo 5) { println(i) }
Solution
Step 1: Identify correct descending range syntax
Kotlin uses downTo to count down from a higher number to a lower number.
Step 2: Check each option
for (i in 5 downTo 1) { println(i) } uses 5 downTo 1, which is correct. for (i in 5..1) { println(i) } uses an invalid range (5..1). for (i in 1..5 step -1) { println(i) } tries to use negative step incorrectly. for (i in 1 downTo 5) { println(i) } reverses the range incorrectly.
Final Answer:
for (i in 5 downTo 1) { println(i) } -> Option A
Quick Check:
Use downTo for descending loops [OK]
Hint: Use downTo to count down in Kotlin loops [OK]
Common Mistakes:
Using .. for descending ranges
Trying negative step with .. (not allowed)
Swapping start and end in downTo
3. What is the output of this Kotlin code?
var sum = 0
for (i in 1..5 step 2) {
sum += i
}
println(sum)
medium
A. 6
B. 15
C. 9
D. 10
Solution
Step 1: Understand the loop range and step
The loop runs from 1 to 5, stepping by 2: values are 1, 3, 5.
Step 2: Calculate the sum of these values
sum = 0 + 1 + 3 + 5 = 9
Final Answer:
9 -> Option C
Quick Check:
1 + 3 + 5 = 9 [OK]
Hint: Add numbers in steps to find sum quickly [OK]
Common Mistakes:
Adding all numbers 1 to 5 instead of stepping
Using step as a multiplier
Confusing step with downTo
4. Find the error in this Kotlin code snippet:
for (i in 10..1 step 2) {
println(i)
}
medium
A. println cannot be used inside for loop
B. step cannot be used with ranges
C. Variable i is not declared
D. Range 10..1 is invalid for counting down
Solution
Step 1: Analyze the range 10..1
The range 10..1 tries to count up from 10 to 1, which is invalid because 10 is greater than 1.
Step 2: Understand correct descending syntax
To count down, use downTo instead of .., e.g., 10 downTo 1 step 2.
Final Answer:
Range 10..1 is invalid for counting down -> Option D
Quick Check:
Use downTo for descending ranges [OK]
Hint: .. always counts up; use downTo to count down [OK]
Common Mistakes:
Using .. for descending ranges
Thinking step fixes descending range
Ignoring Kotlin's range direction rules
5. You want to print all even numbers from 2 to 20 inclusive using a for loop in Kotlin. Which code snippet correctly does this?
hard
A. for (i in 2..20 step 2) { println(i) }
B. for (i in 2..19) { if (i % 2 == 0) println(i) }
C. for (i in 2 downTo 20 step 2) { println(i) }
D. for (i in 1..10 step 2) { println(i * 2) }
Solution
Step 1: Identify the range and step for even numbers
Even numbers from 2 to 20 can be generated by starting at 2 and stepping by 2 up to 20.
Step 2: Check each option
for (i in 2..20 step 2) { println(i) } uses 2..20 step 2, which prints 2,4,6,...,20 correctly. for (i in 2..19) { if (i % 2 == 0) println(i) } ends at 19 so prints evens up to 18, missing 20. for (i in 2 downTo 20 step 2) { println(i) } counts down incorrectly. for (i in 1..10 step 2) { println(i * 2) } prints 2,6,10,..., not all even numbers.
Final Answer:
for (i in 2..20 step 2) { println(i) } -> Option A
Quick Check:
Use step 2 from 2 to 20 for evens [OK]
Hint: Use step 2 starting at 2 to print even numbers [OK]
Common Mistakes:
Using downTo instead of .. for ascending
Forgetting to step by 2
Multiplying inside loop incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.