belongs to collection: Kotlin Ranges Programs
The rangeTo() function is similar to (..) operator. It creates a range up to the value passed as an argument.
Here, we will demonstrate the example of creating an integer range using the rangeTo() function.
Example 1:
fun main(args : Array<String>){ // creating integer ranges println("Integer range 1:") for(x in 1.rangeTo(10)){ println(x) } println() println("Integer range 2:") for(x in 0.rangeTo(3)){ println(x) } println() }
Output:
Integer range 1: 1 2 3 4 5 6 7 8 9 10 Integer range 2: 0 1 2 3
Example 2:
fun main(args : Array<String>){ // creating integer ranges println("Integer range 1:") for(x in 1.rangeTo(10) step 2){ println(x) } println() println("Integer range 2:") for(x in 0.rangeTo(5) step 2){ println(x) } println() }
Integer range 1: 1 3 5 7 9 Integer range 2: 0 2 4
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Example 1:
Output:
Example 2:
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer