Scala for Loop
The for loop allows you to write a loop control structure that executes a specified number of times.
Syntax
In the Scala language,forthe syntax of the loop is:
for( var x <- Range ){
statement(s);
}
In the above syntax,Rangecan be a numeric range represented byi to j, ori until j. The left arrow <- is used to assign a value to the variable x.
Example
The following is an example using thei to jsyntax (including j):
Example
def main(args: Array[String]) {
var a = 0;
// for loop
for( a <- 1 to 10){
println( "Value of a: " + a );
}
}
}
Executing the above code outputs the result:
$ 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 using thei until jsyntax (excluding j):
Example
def main(args: Array[String]) {
var a = 0;
// for loop
for( a <- 1 until 10){
println( "Value of a: " + a );
}
}
}
Executing the above code outputs the result:
$ 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
InIn a for loop,you can use semicolons (;) to set multiple ranges. It will iterate all possible values of the given ranges. The following example demonstrates a loop with two ranges:
Example
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 );
}
}
}
Executing the above code outputs the result:
$ 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 over Collections
The syntax for a for loop over collections is as follows:
for( x <- List ){
statement(s);
}
In the above syntax,Listthe variable is a collection, and the for loop iterates over all elements of the collection.
Example
The following example loops over a numeric collection. We useList()to create the collection. In later chapters, we will introduce collections in detail.
Example
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 );
}
}
}
Executing the above code outputs the result:
$ 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 moreifstatements to filter some elements.
The following is the syntax for using filters in a for loop.
for( var x <- List
if condition1; if condition2...
){
statement(s);
}
You can use semicolons (;) to add one or more filter conditions to the expression.
Example
The following is an example of filtering in a for loop:
Example
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 );
}
}
}
Executing the above code outputs the result:
$ 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
for with yield
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 that the braces are used to hold variables and conditions,retValis a variable. In the loop, yield records the current element, stores it in a collection, and after the loop ends, that collection is returned.
Example
The following example demonstrates using yield in a for loop:
Example
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 return value
for( a <- retVal){
println( "Value of a: " + a );
}
}
}
Executing the above code outputs the result:
$ 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: 7Other Extensions
Scala Loops