Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions docs/topics/ranges.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
[//]: # (title: Ranges and progressions)

Ranges and progressions define sequences of values in Kotlin, supporting range operators, iteration, custom step values, and arithmetic progressions.

## Ranges

Kotlin lets you easily create ranges of values using the [`.rangeTo()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.ranges/range-to.html)
and [`.rangeUntil()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.ranges/range-until.html) functions from the
`kotlin.ranges` package.

A range represents an ordered set of values with a defined start and end. By default, it increments by 1 at each step.
For example, `1..4` represents the numbers 1, 2, 3, and 4.

To create:
* a closed-ended range, call the `.rangeTo()` function with the `..` operator.
* an open-ended range, call the `.rangeUntil()` function with the `..<` operator.

* a closed-ended range, call the `.rangeTo()` function with the `..` operator. This includes both the start and end values.
* an open-ended range, call the `.rangeUntil()` function with the `..<` operator. This includes the start value but excludes the end value.

For example:

```kotlin
fun main() {
//sampleStart
// Closed-ended range
// Closed-ended range: includes both 1 and 4
println(4 in 1..4)
// true

// Open-ended range
// Open-ended range: includes 1, excludes 4
println(4 in 1..<4)
// false
//sampleEnd
Expand Down Expand Up @@ -50,11 +58,10 @@ fun main() {
```
{kotlin-runnable="true" kotlin-min-compiler-version="1.3" id="kotlin-ranges-downto"}

It is also possible to iterate over numbers with an arbitrary step (not necessarily 1). This is done via the
[`step`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.ranges/step.html) function.
You can also iterate over numbers with a custom step using the
[`step()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.ranges/step.html) function, instead of the default increment of 1:

```kotlin

fun main() {
//sampleStart
for (i in 0..8 step 2) print(i)
Expand Down