What is the Range method in Scala?

Scala offers a method for the implementation of ranges. The Range method sets the lower and upper limits of the range, along with the intended increments for producing a sequence of ordered numbers. We can use this method with other keywords as well, i.e., until, by, and to.

Syntax

int Range(int min, int max, int increment)

Parameters

The Range function takes three arguments:

  1. min: the minimum value where the range begins
  2. max: the maximum value the range will not exceed
  3. increment: the size of the jumps in the range

Return value

The Range function returns a range with the specified min/ max values and increment.

Code

The following code snippets show how we can use both the Range function and its alternative (until, to, by).

// Example code for Range method
object Main extends App{
// applying Range method
val range_1 = Range(2, 12, 2)
// Displays given range
println(range_1)
for(i <- range_1){
println(i)
}
// associated methods
print("Accessing element by indexing: ")
println (range_1(2))
print("Last element in range: ")
println (range_1.last)
// alternate to range method
val range_2 = 2 until 11 by 2
// Displays given range
println(range_2)
// associated methods
print("Accessing element by indexing: ")
println (range_2(2))
print("Last element in range: ")
println (range_2.last)
}

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved