Range Operator (..) in Kotlin
Definition
- This operator is used to mention a range of values inclusive of the from and to values.
- It is represented by ".."
- This operator can be used only in the increasing order of ranges.
- Range operators are mainly used along with in and !in operators.
- While using range operator for iterating through a range, it is possible to set a range even skipping values. This is done using "step" keyword.
Syntax
1. Normal Usage
from_value .. to_value
For example,
if(4 in 1..10){
//This will get printed while iterating through values : 1,2,3,4,5,6,7,8,9 and 10
println("The value is in the range")
}
2. Range Operator with "step" Usage
from_value .. to_value step skip_value
For example,
if(4 in 1..10 step 4){
//This will get printed while iterating through values 1,4 and 8 skipping 4 values starting from 1 until 10.
println("The value is in the range")
}
Range Operator Example Program in Kotlin
//Range Operator Example Program in Kotlin
//Operator Kotlin Programs, Basic Kotlin Program
fun main(args: Array<String>) {
println("Loop 1")
//Printing values in range 100 to 105
for(num in 100..105){
println("$num is in the range")
}
println("\nLoop 2")
//Printing values in range 100 to 105 skipping 2 values
for(num in 100..105 step 2){
println("$num is in the range")
}
}
Sample Output
Loop 1
100 is in the range
101 is in the range
102 is in the range
103 is in the range
104 is in the range
105 is in the range
Loop 2
100 is in the range
102 is in the range
104 is in the range
Kotlin Operators
- Kotlin Operators Overview
- Arithmetic Operators (Mathematical Operators) in Kotlin
- Assignment Operators and Augmented Assignment Operators in Kotlin
- Unary Operators (Sign, Inverts, Increment, and Decrement) in Kotlin
- Logical Operators in Kotlin
- Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
- Comparison Operators in Kotlin
- In Operator (in and !in) in Kotlin
- Is Operator (is and !is) in Kotlin
- Indexed Access Operator [, ] in Kotlin
- Not Null Assertion Operator in Kotlin
- Safe Call Operator (?.) in Kotlin
- Elvis Operator (?:) in Kotlin
- Range Operator (..) in Kotlin
Read More Articles
- Declare Variables In Kotlin
- Read Data Input using Scanner in Kotlin
- print and println Data Output in Kotlin
- Double Data type Usage and Type Conversion in Kotlin
- Arithmetic Operators (Mathematical Operators) in Kotlin
- Unary Operators (Sign, Inverts, Increment, and Decrement) in Kotlin
- Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
- Printing Variables and Values in Kotlin
- Float Data type Usage and Type Conversion in Kotlin
- Long Data type Usage and Type Conversion in Kotlin
- Comparison Operators in Kotlin
- Byte Data type Usage and Type Conversion in Kotlin
- Is Operator (is and !is) in Kotlin
- In Operator (in and !in) in Kotlin
- Char Data type Usage and Type Conversion in Kotlin
- Read Data Input from Command Line in Kotlin
- Assignment Operators and Augmented Assignment Operators in Kotlin
- Indexed Access Operator [, ] in Kotlin
- Read String Data Input in Kotlin
- Short Data type Usage and Type Conversion in Kotlin
- Boolean Data type Usage and Type Conversion in Kotlin
- Logical Operators in Kotlin
- Elvis Operator (?:) in Kotlin
- Repeat and Its Usage in Kotlin
- Safe Call Operator (?.) in Kotlin