Kotlin Ranges with Inclusive, Descending, and Half-Open Examples
Kotlin ranges represent a sequence of values from a start point to an end point. They are commonly used in for loops, index checks, validation conditions, and character-based iteration.
A Kotlin range can include both boundary values, exclude the end value, or move in descending order. The most used forms are m..n, n downTo m, and m until n. Kotlin also supports the open-ended range operator ..< in modern Kotlin versions for the same half-open style as until.
Kotlin ranges are useful when you want to avoid manual loop counters and repeated boundary checks. Instead of writing boilerplate code to initialize, increment, decrement, and compare a variable, you can express the intended range directly and let Kotlin handle the progression.
The Kotlin standard library documentation for ranges is available at kotlinlang.org/docs/ranges.html. This tutorial focuses on practical syntax and examples for loops and conditional checks.
Kotlin range syntax at a glance
Kotlin ranges are commonly written in the following forms.
start..end // includes start and end
start until end // includes start, excludes end
start..<end // includes start, excludes end
end downTo start // counts down, includes both ends
range step value // skips by the given positive step
| Kotlin range form | Includes end value? | Typical use |
|---|---|---|
2..4 | Yes, gives 2, 3, 4 | Inclusive loop or membership check |
2 until 4 | No, gives 2, 3 | Looping over array or list indexes before the size value |
2..<4 | No, gives 2, 3 | Open-ended range syntax in modern Kotlin |
4 downTo 2 | Yes, gives 4, 3, 2 | Reverse counting |
0..10 step 3 | Yes, but only reached values are printed | Looping with a custom increment |
By default, the step value from m to n is 1. You can change this step value by specifying a step value after the Kotlin range using the step keyword. We shall look into these examples as well.
In this tutorial we will learn about different forms of Kotlin ranges and how to use them in for loop statements and if conditional statements.
Kotlin inclusive range using m..n
m..n creates an inclusive range from m to n, given m < n. Both the lower limit and the upper limit are included. For example, 2..4 contains 2, 3, and 4.
In the following example, we shall look into the usage of Kotlin range m..n in Loop statement.
Kotlin Program – example.kt
fun main(args: Array<String>) {
for(i in 2..4){
println(i)
}
}
Run the program. For loop iterates with i starting from 2 to 4, both included.
Output
2
3
4
The step value by default for for-in-range is 1. But, you can change this step value using step keyword. step keyword transforms the range into a progresstion.
In the following example, we shall use step keyword and run the for loop in steps of 3 in the given range.
Kotlin Program – example.kt
/**
* Kotlin Range
*/
fun main(args: Array<String>) {
for(i in 0..10 step 3){
println(i)
}
}
Run the program. The loop starts at 0 and adds 3 after each iteration. Since 12 would be outside 0..10, the last printed value is 9.
Output
0
3
6
9
You can also use Kotlin range m..n in a conditional statement like if-statement. Using in keyword, you can check if a value is in the given range.
In the following example, we shall check if the number 3 is present in given range 2..4.
Kotlin Program – example.kt
fun main(args: Array<String>) {
var r = 3
if(r in 2..4){
println(r.toString() + " is in the range 2..4")
}
}
Run the above Kotlin program.
Output
3 is in the range 2..4
We have used a range, if-in statement, to check if a given item is present in the range.
Kotlin membership checks with in and !in
Use in when you want to check that a value is inside a Kotlin range. Use !in when you want to check that a value is outside the range. This is useful in input validation, score ranges, age ranges, and index bounds.
Kotlin Program – example.kt
fun main() {
val score = 82
if (score in 0..100) {
println("Valid score")
}
if (score !in 90..100) {
println("Score is not in the top band")
}
}
Output
Valid score
Score is not in the top band
Kotlin descending range using n downTo m
n downTo m corresponds to the range [n,m] given m<n. By default, m and n are included in the range.
In the following example, we shall use the Kotlin range n downTo m in Loop statement
Kotlin Program – example.kt
fun main(args: Array<String>) {
for(i in 4 downTo 2){
println(i)
}
}
Run the above example Kotlin program.
Output
4
3
2
Now, let us use this Kotlin range n downTo m in a conditional statement to check if an element is present in the range.
Kotlin Program – example.kt
fun main(args: Array<String>) {
var r = 3
if(r in 4 downTo 1){
println(r.toString() + " is in the range")
}
}
Run the above Kotlin program.
Output
3 is in the range
3 is of course present in the range 4 downTo 1. Therefore, r in 4 downTo 1 returns true.
Kotlin downTo with step for reverse iteration
You can combine downTo with step when you want to count backward by more than one value at a time. The step value is written as a positive number; downTo decides the descending direction.
Kotlin Program – example.kt
fun main() {
for (i in 10 downTo 2 step 2) {
println(i)
}
}
Output
10
8
6
4
2
Kotlin half-open range using m until n
m until n corresponds to the range [m,n) given m<n. By default, m is included in the range and n is excluded from the range.
In the following example, we shall use Kotlin range in the form m until n in for loop statement.
Kotlin Program – example.kt
fun main(args: Array<String>) {
for(i in 2 until 4){
println(i)
}
}
Run the above example program. In the range, 2 until 4, we have only 2, 3. 4 is not included in the range.
Output
2
3
In the following example, we use Kotlin range m until n in if conditional statement.
Kotlin Program – example.kt
fun main(args: Array<String>) {
var r = 3
if(r in 1 until 4){
println(r.toString() + " is in the range")
}
}
Run the above example program.
Output
3 is in the range
Kotlin until and ..< for list and array indexes
A common reason to use until is index iteration. For a list with size 3, valid indexes are 0, 1, and 2. Therefore, 0 until names.size avoids the invalid index 3.
Kotlin Program – example.kt
fun main() {
val names = listOf("Arun", "Bala", "Charu")
for (index in 0 until names.size) {
println("$index: ${names[index]}")
}
}
Output
0: Arun
1: Bala
2: Charu
The same idea can be written with the open-ended range operator ..<.
for (index in 0..<names.size) {
println(names[index])
}
Kotlin character ranges with CharRange
Kotlin ranges are not limited to integers. You can also create character ranges such as 'a'..'d'. This produces a CharRange that can be used in loops and membership checks.
Kotlin Program – example.kt
fun main() {
for (letter in 'a'..'d') {
print(letter)
}
println()
val grade = 'B'
if (grade in 'A'..'C') {
println("Grade is in the first group")
}
}
Output
abcd
Grade is in the first group
Kotlin range mistakes to avoid
- Using
..for indexes when the end is a size value:0..list.sizeincludeslist.size, which is outside the valid index range. Use0 until list.size,0..<list.size, orlist.indices. - Expecting
4..2to count down: use4 downTo 2for descending iteration. - Passing a negative step: write
10 downTo 0 step 2instead of trying to makestepnegative. - Forgetting that
untilexcludes the end:1 until 4contains1,2, and3, but not4.
Kotlin ranges FAQ
What is the difference between 1..5 and 1 until 5 in Kotlin?
1..5 includes both 1 and 5, so it produces 1, 2, 3, 4, 5. 1 until 5 excludes the end value, so it produces 1, 2, 3, 4.
How do I create a descending Kotlin range?
Use downTo. For example, 5 downTo 1 produces 5, 4, 3, 2, 1. To skip values while descending, use 5 downTo 1 step 2.
How do I check whether a value is inside a Kotlin range?
Use the in operator. For example, if (age in 18..60) checks whether age is between 18 and 60, including both limits. Use !in to check that a value is outside a range.
Can Kotlin ranges be used with characters?
Yes. Kotlin supports character ranges such as 'a'..'z'. You can loop through them or use in to test whether a character belongs to a range.
When should I use list.indices instead of a Kotlin range?
Use list.indices when you want the valid index range of a list directly. It avoids mistakes such as writing 0..list.size, which includes an invalid final index.
Kotlin ranges editorial QA checklist
- Confirm that every
..example explains that the end value is included. - Confirm that every
untiland..<example explains that the end value is excluded. - Check that descending examples use
downTo, not a reversed..expression. - Check index examples for off-by-one errors, especially when using
list.size. - Run added Kotlin snippets and verify that output blocks match the printed result.
Kotlin ranges tutorial conclusion
In this Kotlin Tutorial, we learned about inclusive ranges with .., descending ranges with downTo, half-open ranges with until and ..<, custom steps with step, and membership checks with in and !in.
TutorialKart.com