Scala for loop
Tags: Scala, scala for loopIn this post, we list the common ways of for loop in Scala programming.
Scala for loop with ranges
The simplest syntax of for scala loop with ranges is as follows:
|
1 2 3 4 |
for( var x <- i to j ){ process(x); } |
i to j can also be replaced with other generators such as i until j. See the following example:
|
1 2 3 4 5 6 7 8 9 |
object ScalaForLoop { def main(args: Array[String]) { var x = 0; // for loop execution with a range for( x <- 1 to 5){ println( "Value of x: " + x ); } } } |
The output is:
value of a: 1 value of a: 2 value of a: 3 value of a: 4 value of a: 5
Scala for loop with multiple ranges
We can use multiple ranges separated by semicolon (;) within scala for loop. In this case, the for loop will iterate through all the possible computations of the given ranges. The following code shows example of using two ranges, we can also use more than two ranges.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
object ScalaForLoopMultipleRanges { def main(args: Array[String]) { var a = 0; var b = 0; // for loop execution with a range for( a <- 1 to 3; b <- 1 to 3){ println( "Value of a: " + a ); println( "Value of b: " + b ); } } } |
The output is:
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
Scala for Loop with List
The following is the syntax of scala for loop with collections or list.
|
1 2 3 4 |
for( var e <- List ){ process(e); } |
Here, the List variable is a collection type having a list of elements and the for loop iterates all the elements and returns one element stored in the e variable at a time.
See the following example showing how to for loop a Scala List.
|
1 2 3 4 5 6 7 8 9 10 |
object ScalaForLoopList { def main(args: Array[String]) { var a = 0; val numList = List(1,2,3); // for loop execution with a list for( a <- numList ){ println( "Value of a: " + a ); } } } |
The output: value of a: 1 value of a: 2 value of a: 3
Scala for loop with guards
Scala’s for loop allows to filter out some elements using one or more if statement(s). The code below is the syntax of for loop along with filters (guards). To add more than one filter to a ‘for’ expression, separate the filters with semicolons(;).
|
1 2 3 4 5 6 |
for( var x <- List if condition1; if condition2... ){ process(s); } |
See the following example:
|
1 2 3 4 5 6 7 8 9 10 |
object ScalaForLoopWithFilters { def main(args: Array[String]) { var a = 0; val numList = List(1,2,3,4,5); // for loop execution with multiple filters for( a <- numList if a != 3; if a < 4 ){ println( "Value of a: " + a ); } } } |
The output is:
Value of a: 1
Value of a: 2
Scala for loop with yield
|
1 2 3 4 5 |
var retVal = for{ var a <- List if condition1; if condition2... } yield a |
|
1 2 3 4 5 6 7 8 9 |
val numList = List(1,2,3,4,5); val a = 0 // for loop execution with a yield var res = for{a <- numList if a != 3; if a < 5 }yield a // Now print returned values using another loop. for( x <- res){ println( "Value of x: " + x ); } |
The output:
Value of x: 1
Value of x: 2
Value of x: 4
Scala for loop, yield, and guards (for loop ‘if’ conditions)
This is another example:
|
1 2 3 4 5 6 |
scala> val list = Array(1, 2, 3, 4, 5) a: Array[Int] = Array(1, 2, 3, 4, 5) scala> for (x <- list if x > 2) yield x res1: Array[Int] = Array(3, 4, 5) |
By using the “if x > 2” guard condition, we limit the Array with only the three elements shown above.
Scala for loop on collection using foreach
A common way to iterate over a Scala List is using the foreach method. Here’s a quote about foreach from the book Programming in Scala:
foreach takes a procedure — a function with a result type
Unit— as the right operand. It simply applies the procedure to eachListelement. The result of the operation is againUnit; no list of results is assembled.
The following example shows how to use foreach to print every item in a List:
|
1 2 3 4 5 6 7 8 |
scala> val x = List(1,2,3) x: List[Int] = List(1, 2, 3) scala> x.foreach { println } 1 2 3 |
This next example shows a way to sum all the elements in a list using the foreach:
|
1 2 3 4 5 6 7 8 9 10 |
scala> var sum = 0 sum: Int = 0 scala> val x = List(1,2,3) x: List[Int] = List(1, 2, 3) scala> x.foreach(sum += _) scala> println(sum) 6 |
Scala call foreach for Java List
If you have a java ArrayList, you can’t use foreach, as it is not defined in the the Java List. However, we can use the implicit conversion to call foreach on Java’s ArrayList. See the following example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
scala> var list = new java.util.ArrayList[Int]() list: java.util.ArrayList[Int] = [] scala> list.add(1) res6: Boolean = true scala> list.add(2) res7: Boolean = true scala> list res8: java.util.ArrayList[Int] = [1, 2] scala> list.foreach(println) <console>:9: error: value foreach is not a member of java.util.ArrayList[Int] list.foreach(println) ^ scala> import scala.collection.JavaConversions._ import scala.collection.JavaConversions._ scala> list.foreach(println) 1 2 |











