Kotlin Range

Introduction

In Kotlin, a Range represents a sequence of values defined by a start and an end value. Ranges are often used for iteration and checking if a value lies within a specific interval. Kotlin provides various types of ranges, such as integer ranges, character ranges, and others. The Range class and its extensions are part of the Kotlin standard library.

Table of Contents

  1. What is Range?
  2. Creating a Range
  3. Common Operations
  4. Examples of Range
  5. Real-World Use Case
  6. Conclusion

1. What is Range?

A Range in Kotlin defines a closed interval between a start and an end value. Ranges are used to represent continuous sequences and can be iterated over. Common types of ranges include IntRange, LongRange, and CharRange.

Syntax

val intRange: IntRange = 1..10
val charRange: CharRange = 'a'..'z'

2. Creating a Range

You can create ranges using the .. operator or the rangeTo function.

Example

val intRange: IntRange = 1..10 // Using the .. operator
val anotherIntRange = 1.rangeTo(10) // Using the rangeTo function
val charRange: CharRange = 'a'..'z'

3. Common Operations

Ranges support various operations such as iteration, checking if a value is within the range, and performing actions on each element.

Iteration

You can iterate over a range using a for loop.

for (i in 1..5) {
    println(i)
}

Checking Membership

You can check if a value lies within a range using the in operator.

val inRange = 5 in 1..10 // true

Other Operations

  • first: Returns the first value in the range.
  • last: Returns the last value in the range.
  • step: Returns the step value for the range.
  • reversed(): Returns a range with elements in reverse order.

4. Examples of Range

Example 1: Iterating Over an IntRange

This example demonstrates how to iterate over an integer range.

fun main() {
    for (i in 1..5) {
        println(i)
    }
}

Output:

1
2
3
4
5

Explanation:
This example iterates over the range 1..5 and prints each value.

Example 2: Checking if a Value is Within a Range

This example demonstrates how to check if a value is within a specified range.

fun main() {
    val range = 1..10
    val number = 5
    if (number in range) {
        println("$number is within the range.")
    } else {
        println("$number is outside the range.")
    }
}

Output:

5 is within the range.

Explanation:
This example checks if the value 5 is within the range 1..10.

Example 3: Using Step in a Range

This example demonstrates how to use a step value in a range.

fun main() {
    for (i in 1..10 step 2) {
        println(i)
    }
}

Output:

1
3
5
7
9

Explanation:
This example iterates over the range 1..10 with a step value of 2, printing every second number.

Example 4: Reversing a Range

This example demonstrates how to reverse a range.

fun main() {
    for (i in (1..5).reversed()) {
        println(i)
    }
}

Output:

5
4
3
2
1

Explanation:
This example reverses the range 1..5 and iterates over it in reverse order.

Example 5: Iterating Over a CharRange

This example demonstrates how to iterate over a range of characters.

fun main() {
    for (c in 'a'..'e') {
        println(c)
    }
}

Output:

a
b
c
d
e

Explanation:
This example iterates over the range of characters from 'a' to 'e' and prints each character.

5. Real-World Use Case: Validating Age

You can use a range to validate if an age falls within a valid range for a certain activity, such as voting.

Example: Validating Voting Age

fun isValidVotingAge(age: Int): Boolean {
    val votingAgeRange = 18..100
    return age in votingAgeRange
}

fun main() {
    val age = 20
    if (isValidVotingAge(age)) {
        println("Age $age is valid for voting.")
    } else {
        println("Age $age is not valid for voting.")
    }
}

Output:

Age 20 is valid for voting.

Explanation:
This example uses a range to check if an age falls within the valid voting age range of 18..100.

Conclusion

The Range class in Kotlin is a powerful and flexible way to represent and manipulate sequences of values. It is part of the Kotlin standard library and provides essential operations for iteration, membership checking, and more. Understanding and utilizing the Range class can greatly enhance your ability to work with sequences and intervals in Kotlin.

Comments