English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Scala for loop

Scala Loops

The for loop in Scala allows you to write a loop control structure that executes a specified number of times.

syntax

In the Scala language for The syntax for the loop is:

for( var x <- Range ){
   statement(s);
}

In the above syntax,Range can be a numeric range representing i to j or i until jThe left arrow <- to assign a value to the variable x.

Online Example

The following is an example that uses i to j The example syntax (including j):

object Test {
   def main(args: Array[String]) {
      var a = 0;
      // for loop
      for( a <- 1 to 10){
         println( "Value of a: " + a );
      }
   }
}

The output of the above code is as follows:

$ scalac Test.scala
$ scala Test
value of a: 1
value of a: 2
value of a: 3
value of a: 4
value of a: 5
value of a: 6
value of a: 7
value of a: 8
value of a: 9
value of a: 10

The following is an example that uses i until j The example syntax (excluding j):

object Test {
   def main(args: Array[String]) {
      var a = 0;
      // for loop
      for( a <- 1 until 10){
         println( "Value of a: " + a );
      }
   }
}

The output of the above code is as follows:

$ scalac Test.scala
$ scala Test
value of a: 1
value of a: 2
value of a: 3
value of a: 4
value of a: 5
value of a: 6
value of a: 7
value of a: 8
value of a: 9

In for loop You can use a semicolon (;) to set multiple ranges, which will iterate through all possible values in the given range. The following example demonstrates a loop example with two ranges:

object Test {
   def main(args: Array[String]) {
      var a = 0;
      var b = 0;
      // for loop
      for( a <- 1 to 3; b <- 1 to 3){
         println( "Value of a: " + a );
         println( "Value of b: " + b );
      }
   }
}

The output of the above code is as follows:

$ scalac Test.scala
$ scala Test
Value of a: 1
Value of b: 1
Value of a: 1
Value of b: 2
Value of a: 1
Value of b: 3
Value of a: 2
Value of b: 1
Value of a: 2
Value of b: 2
Value of a: 2
Value of b: 3
Value of a: 3
Value of b: 1
Value of a: 3
Value of b: 2
Value of a: 3
Value of b: 3

for loop collection

The syntax for the for loop collection is as follows:

for( x <- List ){
   statement(s);
}

In the above syntax, List is a collection, and the for loop will iterate through all elements of the collection.

Online Example

The following example will loop through a number collection. We use List() to create a collection. In later chapters, we will introduce collections in detail.

object Test {
   def main(args: Array[String]) {
      var a = 0;
      val numList = List(1,2,3,4,5,6);

      // for loop
      for( a <- numList ){
         println( "Value of a: " + a );
      }
   }
}

The output of the above code is as follows:

$ scalac Test.scala
$ scala Test
value of a: 1
value of a: 2
value of a: 3
value of a: 4
value of a: 5
value of a: 6

for loop filtering

Scala can use one or more if Used to filter some elements.

The syntax for using a filter in a for loop is as follows.

for( var x <- List
      if condition1; if condition2...
   {
   statement(s);

You can use semicolons (;) to add one or more filtering conditions to expressions.

Online Example

The following is an example of filtering in for loops:

object Test {
   def main(args: Array[String]) {
      var a = 0;
      val numList = List(1,2,3,4,5,6,7,8,9,10);

      // for loop
      for( a <- numList
           if a != 3; if a < 8 ){
         println( "Value of a: " + a );
      }
   }
}

The output of the above code is as follows:

$ scalac Test.scala
$ scala Test
value of a: 1
value of a: 2
value of a: 4
value of a: 5
value of a: 6
value of a: 7

Using yield in for loops

You can store the return value of a for loop as a variable. The syntax is as follows:

var retVal = for{ var x <- List
     if condition1; if condition2...
}yield x

Note the curly braces used to store variables and conditions,retVal is a variable, and the yield in the loop will record the current element, store it in the collection, and return the collection after the loop ends.

Online Example

The following example demonstrates the use of yield in for loops:

object Test {
   def main(args: Array[String]) {
      var a = 0;
      val numList = List(1,2,3,4,5,6,7,8,9,10);

      // for loop
      var retVal = for{ a <- numList
                        if a != 3; if a < 8
                      }yield a

      // Output the returned value
      for( a <- retVal){
         println( "Value of a: " + a );
      }
   }
}

The output of the above code is as follows:

$ scalac Test.scala
$ scala Test
value of a: 1
value of a: 2
value of a: 4
value of a: 5
value of a: 6
value of a: 7

Scala Loops