Kotlin Tutorial
Range in Kotlin: All Concepts With Examples (Full Guide)
Table of Contents
- Introduction
- What is Range in Kotlin?
- How to Create Range in Kotlin?
- Kotlin until() Function
- How to Filter Ranges in Kotlin?
- Distinct Values in a Range
- Kotlin Range Utility Functions
- Checking if a value exists in Range or not
- Kotlin Inclusive Range
- Kotlin Exclusive Range
- Kotlin Float Range
- Kotlin Range Map
- Using break and continue in Kotlin Range
- Kotlin Range using forEach loop
- Modifying the Default Step() in Range in Kotlin
- How to Reverse a Range in Kotlin?
- Kotlin Full Course Video for Beginners [FREE]
Kotlin Full Course Video for Beginners [FREE]
Kotlin Range FAQs
A closed range (e.g., 1..5) includes both the start and end values. An open or half-open range (e.g., 1 until 5) includes the start value but excludes the end value.
Yes, you can create ranges with custom types as long as those types support comparison operations (<, <=, >, >=). You can define how comparisons work for your custom types by implementing the necessary comparison operators.
You can use the filter or filterNot functions to filter values within a range based on a condition. These functions return a new range with the filtered values.
No, ranges in Kotlin are immutable. Once created, you cannot change their start or end values. If you need a mutable sequence of values, you should use a list or another data structure.
Yes, you can create ranges with floating-point numbers (e.g., Float or Double) in Kotlin using the same range creation methods as with integers.