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:
min: the minimum value where the range beginsmax: the maximum value the range will not exceedincrement: 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 methodobject Main extends App{// applying Range methodval range_1 = Range(2, 12, 2)// Displays given rangeprintln(range_1)for(i <- range_1){println(i)}// associated methodsprint("Accessing element by indexing: ")println (range_1(2))print("Last element in range: ")println (range_1.last)// alternate to range methodval range_2 = 2 until 11 by 2// Displays given rangeprintln(range_2)// associated methodsprint("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