Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

scala

What are For loops in Scala?

Hassaan Waqar

Scala, short for Scalable Language, combines features of object-oriented and functional languages to improve application scalability and reliability.

Scala is based on Java. Hence, the syntax is similar. We can consider a Scala program as a collection of objects that call each others’ methods.

For loops

For loops are used when a portion of code needs to be repeated a fixed number of times. There are different types of For loops in Scala:

For loop with ranges

For loop with ranges are used when a loop is to be run up to or until a particular value. The basic syntax is as follows:

for( var x <- Range ){
   line(s) of code;
}

The <- symbol is known as the generator. It generates values from a range.

Range represents numerical values from a lower bound to an upper bound.

In a loop using the to keyword, the loop iterates inclusive of the upper bound. The code snippet below shows a For loop using the to keyword:

Notice how the code outputs numbers from 1 to 5 inclusive.

object Loops {
   def main(args: Array[String]) {
      var x = 0;
      /// for loop using 'to'
      for( x <- 1 to 5){
         println( "Value of x: " + x );
      }
   }
}

In a loop using the until keyword, the loop iterates exclusive of the upper bound. The code snippet below shows a For loop using the until keyword:

Notice how the code outputs numbers from 1 to 4.

object Loops {
   def main(args: Array[String]) {
      var x = 0;
      /// for loop using 'until'
      for( x <- 1 until 5){
         println( "Value of x: " + x );
      }
   }
}

We can also use multiple ranges within the For loop. The code will then run through all the possible combinations of both ranges.

Ranges are separated using a semi-colon.

The following example shows a For loop using multiple ranges:

object Loops {
   def main(args: Array[String]) {
      var x, y = 0;
      /// for loop using multiple ranges
      for( x <- 1 to 5; y <- 1 to 3){
         println( "Value of x is " + x + " and value of y is " + y );
      }
   }
}

For loop with collections

A collection of items can be represented using a List in Scala. A For loop can be used to iterate over every element of the List.

The basic syntax is as follows:

for( var x <- List ){
   line(s) of code;
}

The variable x keeps track of individual elements of the collection while iterating using a For loop.

The code snippet below shows a For loop iterating over a collection of items:

object Loops {
   def main(args: Array[String]) {
      var x = 0;
      val days = List("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
      // for loop execution with a collection
      for( x <- days ){
         println( "Today is: " + x );
      }
   }
}

For loop using filters

We can add filters to a For loop when some conditions need to be satisfied.

Filters are added using the if keyword.

Filters are separated using a semi-colon.

Filters can be used with For loops having both ranges and collections.

The basic syntax is as follows:

for( var x <- Range 
condition 1; condition 2... ){
   line(s) of code;
}

or

for( var x <- List;
condition 1; condition 2...   ){
   line(s) of code;
}

The code snippet below shows a For loop with filters:

A filter is used to skip over elements using the != (not equals) operator.

A filter is used to choose elements > (greater than) some number.

object Loops {
   def main(args: Array[String]) {
      var x = 0;
      // for loop with filters
      for( x <- 1 to 10
           if x != 5; if x != 7; if x > 3){
         println( "Value of x: " + x );
      }
   }
}

RELATED TAGS

scala

CONTRIBUTOR

Hassaan Waqar
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring