# Scala | Ranges

The Range in Scala can be defined as an organized series of uniformly separated Integers. It is helpful in supplying more strength with less methods so, operations performed here are very quick.

Some important points:

• The Ranges can be utilized by the for loops for iteration.
• It can be obtained using some predefined methods namely until, by, and to.
• It is defined by three constants i.e, (start, end, and increment value).

Syntax:

`val range = Range(x, y, z)`

Where, x is the lower limit, y is the upper limit, and z is the increment.
Example:

 `// Scala program for Ranges ` ` `  `// Creating object ` `object` `GFG ` `{ ` ` `  `    ``// Main method ` `    ``def` `main(args``:` `Array[String]) ` `    ``{ ` ` `  `        ``// applying range method ` `        ``val` `x ``=` `Range(``3``, ``10``, ``1``) ` ` `  `        ``// Displays given range ` `        ``println(x) ` ` `  `        ``// Displays starting value ` `        ``// of the given range ` `        ``println(x(``0``)) ` ` `  `        ``// Displays last value ` `        ``// of the given range ` `        ``println(x.last) ` `    ``} ` `} `

Output:

```Range(3, 4, 5, 6, 7, 8, 9)
3
9
```

Thus, we can say that upper bound of the Range is not inclusive.

### Operations performed on Ranges

• If we want a range inclusive of the end value, we can also use the until method both until and Range methods are used for the same purpose.
Example:

 `// Scala program for Ranges ` ` `  `// Creating object ` `object` `GFG ` `{ ` ` `  `    ``// Main method ` `    ``def` `main(args``:` `Array[String]) ` `    ``{ ` ` `  `        ``// applying range method ` `        ``val` `x ``=` `Range(``0``, ``10``, ``2``) ` ` `  `        ``// applying until method ` `        ``val` `y ``=` `0` `until ``10` `by ``2` ` `  `        ``// Displays true if both the  ` `        ``// methods are equivalent ` `        ``println(x ``==` `y) ` `    ``} ` `} `

Output:

```true
```

Here, by method performs the work of increment.

• The upper bound of the Range can be made inclusive.
Example:

 `// Scala program for Ranges ` ` `  `// Creating object ` `object` `GFG ` `{ ` ` `  `    ``// Main method ` `    ``def` `main(args``:` `Array[String]) ` `    ``{ ` ` `  `        ``// applying range method ` `        ``val` `x ``=` `Range(``1``, ``8``) ` ` `  `        ``// Including upper bound ` `        ``val` `y ``=` `x.inclusive ` ` `  `        ``// Displays all the elements ` `        ``// of the range ` `        ``println(y) ` `    ``} ` `} `

Output:

```Range(1, 2, 3, 4, 5, 6, 7, 8)
```

Here, inclusive is used to include upper bound of the Range.

• If we want a range of integer values, we can use the to method both to and inclusive Ranges are equivalent.
Example:

 `// Scala program for Ranges ` ` `  `// Creating object ` `object` `GFG ` `{ ` ` `  `    ``// Main method ` `    ``def` `main(args``:` `Array[String]) ` `    ``{ ` ` `  `        ``// applying range method ` `        ``val` `x ``=` `Range(``1``, ``8``) ` ` `  `        ``// Including upper bound ` `        ``val` `y ``=` `x.inclusive ` ` `  `        ``// applying 'to' method ` `        ``val` `z ``=` `1` `to ``8` ` `  `        ``// Displays true if both the ` `        ``// methods are equal ` `        ``println(y ``==` `z) ` `    ``} ` `} `

Output:

```true
```

Thus, both the methods here performs the same task.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.