Open In App

Scala | Ranges

Last Updated : 08 Apr, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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.



Similar Reads

Scala short <(x: Short): Boolean
Short, a 16-bit signed integer (equivalent to Java's short primitive type) is a subtype of scala.AnyVal. The <(x: Short) method is utilized to return true if this value is less than x, false otherwise. Method Definition: def <(x: Short): Boolean Return Type: It returns true if this value is less than x, otherwise false. Example #1: // Scala p
1 min read
Scala short <(x: Char): Boolean
Short, a 16-bit signed integer (equivalent to Java's short primitive type) is a subtype of scala.AnyVal. The <(x: Char) method is utilized to return true if this value is less than x, false otherwise. Method Definition: def <(x: Char): BooleanReturn Type: It returns true if this value is less than x, otherwise false. Example #1: C/C++ Code //
1 min read
Scala Extractors
In Scala Extractor is defined as an object which has a method named unapply as one of its part. This method extracts an object and returns back the attributes. This method is also used in Pattern matching and Partial functions. Extractors also explains apply method, which takes the arguments and constructs an object so, it's helpful in constructing
6 min read
Scala | Partially Applied functions
The Partially applied functions are the functions which are not applied on all the arguments defined by the stated function i.e, while invoking a function, we can supply some of the arguments and the left arguments are supplied when required. we call a function we can pass less arguments in it and when we pass less arguments it does not throw an ex
3 min read
Scala String indexOf(String str) method with example
The indexOf(String str) method is utilized to return the index of the sub-string which occurs first in the string stated. Method Definition: indexOf(String str) Return Type: It returns the index of the sub-string which is specified in the argument of the method. Example #1: // Scala program of int indexOf() // method // Creating object object GfG {
1 min read
Scala String contentEquals() method with example
The contentEquals() method is utilized to compare a string to the content of StringBuffer. Method Definition: Boolean contentEquals(StringBuffer sb) Return Type: It returns true if the content is equal to the stated string else it returns false. Example #1: // Scala program of contentEquals() // method // Creating object object GfG { // Main method
1 min read
Scala Keywords
Keywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to use as variable names or objects. Doing this will result in a compile-time error. Example: // Scala Program to illustrate the keywords // Here object, def, and var are valid ke
2 min read
Scala Int /(x: Int) method with example
The /(x: Int) method is utilized to return the quotient when the specified first int value is divided by the second int value. Method Definition: (First_Int_Value)./(Second_Int_Value) Return Type: It returns the quotient when the specified first int value is divided by the second int value. Example #1: // Scala program of Int /(x: Int) // method //
1 min read
Scala Int /(x: Short) method with example
The /(x: Short) method is utilized to return the quotient when the specified int value is divided by the short value. Method Definition: (Int_Value)./(Short_Value) Return Type: It returns the quotient when the specified int value is divided by the short value. Example #1: // Scala program of Int /(x: Short) // method // Creating object object GfG {
1 min read
Program to print Java Set of characters in Scala
A java Set of characters can be returned from a Scala program by writing a user defined method of Java in Scala. Here, we don't even need to import any Scala’s JavaConversions object in order to make this conversions work. Now, lets see some examples. Example:1# // Scala program to print Java Set // of characters in Scala // Creating object object
2 min read