Ranges let you easily repeat actions for a set of numbers without writing extra code. They make loops simple and clear.
When you want to count from 1 to 10 and do something each time.
When you need to check every number between two values.
When you want to run a loop a fixed number of times.
When you want to create a list of numbers quickly.
When you want to step through numbers with a specific gap, like every 2 or 3.
Syntax
Kotlin
for (i in start..end) {
// code to repeat
}
The .. operator creates a range from start to end, including both.
You can use downTo to count backwards, and step to skip numbers.
Examples
This prints numbers 1 to 5, one per line.
Kotlin
for (i in1..5) {
println(i)
}
This counts backwards from 5 down to 1.
Kotlin
for (i in5 downTo 1) {
println(i)
}
This prints every second number from 1 to 10: 1, 3, 5, 7, 9.
Kotlin
for (i in1..10 step 2) {
println(i)
}
Sample Program
This program shows three loops using ranges: counting up, counting down, and counting with steps.
Kotlin
fun main() {
println("Counting up:")
for (i in1..5) {
println(i)
}
println("Counting down:")
for (i in5 downTo 1) {
println(i)
}
println("Counting with steps:")
for (i in1..10 step 3) {
println(i)
}
}
OutputSuccess
Important Notes
Ranges include the last number, so 1..5 means 1, 2, 3, 4, 5.
You can use ranges with other types like characters, for example 'a'..'e'.
Using ranges makes your code shorter and easier to read compared to manual loops.
Summary
Ranges let you loop over numbers easily and clearly.
You can count up, down, or skip numbers with ranges.
Using ranges makes your code simpler and less error-prone.
Practice
(1/5)
1. What is the main advantage of using ranges in Kotlin for iteration?
easy
A. They let you loop over numbers easily and clearly.
B. They require more code to write loops.
C. They only work with strings, not numbers.
D. They make loops run slower.
Solution
Step 1: Understand what ranges do
Ranges represent a sequence of numbers, making it easy to loop through them.
Step 2: Compare with other methods
Using ranges is simpler and clearer than manually managing loop counters.
Final Answer:
They let you loop over numbers easily and clearly. -> Option A
Quick Check:
Ranges simplify iteration = They let you loop easily [OK]
Hint: Ranges make number loops simple and clear [OK]
Common Mistakes:
Thinking ranges are slower
Believing ranges only work with strings
Assuming ranges need more code
2. Which of the following is the correct syntax to loop from 1 to 5 using a range in Kotlin?
easy
A. for (i in 1 to 5) { println(i) }
B. for (i in range(1,5)) { println(i) }
C. for (i in 1...5) { println(i) }
D. for (i in 1..5) { println(i) }
Solution
Step 1: Identify Kotlin range syntax
Kotlin uses two dots '..' to create ranges, like 1..5.
Step 2: Check each option
for (i in 1..5) { println(i) } uses '1..5' which is correct; others use invalid syntax.
Final Answer:
for (i in 1..5) { println(i) } -> Option D
Quick Check:
Correct range syntax = 1..5 [OK]
Hint: Remember Kotlin uses '..' for ranges [OK]
Common Mistakes:
Using 'to' instead of '..' for ranges
Using three dots '...' which is invalid
Trying Python-like range() function
3. What will be the output of this Kotlin code?
for (i in 3..6) { print(i) }
medium
A. 3456
B. 3 4 5 6
C. 3..6
D. Error
Solution
Step 1: Understand the loop range
The loop runs from 3 to 6 inclusive, so i takes values 3,4,5,6.
Step 2: Analyze the print statement
print(i) prints numbers without spaces or newlines, so output is '3456'.
Final Answer:
3456 -> Option A
Quick Check:
Loop prints numbers directly = 3456 [OK]
Hint: print() outputs without spaces or newlines [OK]
Common Mistakes:
Assuming spaces between numbers
Thinking '..' prints as string
Expecting an error from range
4. Identify the error in this Kotlin code snippet:
for (i in 5..1) { println(i) }
medium
A. Runtime error: cannot loop backwards with '..'.
B. Syntax error: ranges must count up only.
C. The range 5..1 is empty, so loop does not run.
D. No error; it prints 5 to 1.
Solution
Step 1: Check the range direction
Range 5..1 tries to count up from 5 to 1, which is empty.
Step 2: Understand loop behavior
Since the range is empty, the loop body never executes, no error occurs.
Final Answer:
The range 5..1 is empty, so loop does not run. -> Option C
Quick Check:
Counting up range empty if start > end [OK]
Hint: Ranges count up only unless reversed explicitly [OK]
Common Mistakes:
Expecting syntax or runtime error
Thinking loop prints numbers backwards
Using '..' for descending ranges without 'downTo'
5. How can you use ranges to print numbers from 10 down to 5 in Kotlin?
hard
A. for (i in 10..5) { println(i) }
B. for (i in 10 downTo 5) { println(i) }
C. for (i in 5..10 step -1) { println(i) }
D. for (i in 10..5 step 1) { println(i) }
Solution
Step 1: Understand descending ranges
To count down, Kotlin uses 'downTo' instead of '..'.
Step 2: Check each option
for (i in 10 downTo 5) { println(i) } uses '10 downTo 5' which correctly counts down from 10 to 5.
Final Answer:
for (i in 10 downTo 5) { println(i) } -> Option B
Quick Check:
Use 'downTo' for descending ranges [OK]
Hint: Use 'downTo' for counting down in ranges [OK]
Common Mistakes:
Using '..' for descending ranges
Trying negative step in ascending range
Confusing step direction
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.