In this guide, we will discuss Scala for loops. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. There are various forms of for loop in Scala which are described below −
Syntax − for loop with ranges
The simplest syntax of for loop with ranges in Scala is −
for( var x <- Range ){ statement(s); }
Here, the Range could be a range of numbers and that is represented as i to j or sometime like i until j. The left-arrow ← operator is called a generator, so named because it’s generating individual values from a range.
Try the following example program to understand loop control statements (for statement) in Scala Programming Language.
Example
object Demo { def main(args: Array[String]) { var a = 0; // for loop execution with a range for( a <- 1 to 10){ println( "Value of a: " + a ); } } }
Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
\>scalac Demo.scala \>scala Demo
Output
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
Try the following example program to understand loop control statements (for statement) to print loop with the range i until j in Scala Programming Language.
Example
object Demo { def main(args: Array[String]) { var a = 0; // for loop execution with a range for( a <- 1 until 10){ println( "Value of a: " + a ); } } }
Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
\>scalac Demo.scala \>scala Demo
Output
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
You can use multiple ranges separated by semicolon (;) within for loop and in that case loop will iterate through all the possible computations of the given ranges. Following is an example of using just two ranges, you can use more than two ranges as well.
Example
object Demo { 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 ); } } }
Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
\>scalac Demo.scala \>scala Demo
Output
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
Syntax − for Loop with Collections
The following syntax for loop with collections.
for( var x <- List ){ statement(s); }
Here, the List variable is a collection type having a list of elements and for loop iterate through all the elements returning one element in x variable at a time.
Try the following example program to understand loop with a collection of numbers. Here we created this collection using List(). We will study collections in a separate chapter. Loop control statements (for statement) in Scala Programming Language.
Example
object Demo { def main(args: Array[String]) { var a = 0; val numList = List(1,2,3,4,5,6); // for loop execution with a collection for( a <- numList ){ println( "Value of a: " + a ); } } }
Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
\>scalac Demo.scala \>scala Demo
Output
value of a: 1 value of a: 2 value of a: 3 value of a: 4 value of a: 5 value of a: 6
Syntax − for loop with Filters
Scala’s for loop allows to filter out some elements using one or more if statement(s). Following is the syntax of for loop along with filters. To add more than one filter to a ‘for’ expression, separate the filters with semicolons(;).
for( var x <- List if condition1; if condition2... ){ statement(s); }
Try the following example program to understand loop with a filter.
Example
object Demo { def main(args: Array[String]) { var a = 0; val numList = List(1,2,3,4,5,6,7,8,9,10); // for loop execution with multiple filters for( a <- numList if a != 3; if a < 8 ){ println( "Value of a: " + a ); } } }
Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
\>scalac Demo.scala \>scala Demo
Output
value of a: 1 value of a: 2 value of a: 4 value of a: 5 value of a: 6 value of a: 7
Syntax − for loop with yield
You can store return values from a “for” loop in a variable or can return through a function. To do so, you prefix the body of the ‘for’ expression by the keyword yield. The following is the syntax.
Example
var retVal = for{ var x <- List if condition1; if condition2... } yield x
Note − the curly braces have been used to keep the variables and conditions and retVal is a variable where all the values of x will be stored in the form of collection.
Try the following example program to understand loop with yield.
Example
object Demo { def main(args: Array[String]) { var a = 0; val numList = List(1,2,3,4,5,6,7,8,9,10); // for loop execution with a yield var retVal = for{ a <- numList if a != 3; if a < 8 }yield a // Now print returned values using another loop. for( a <- retVal){ println( "Value of a: " + a ); } } }
Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
\>scalac Demo.scala \>scala Demo
Output
value of a: 1 value of a: 2 value of a: 4 value of a: 5 value of a: 6 value of a: 7
Next Topic : Click Here
Pingback: Scala - do-while Loop | Adglob Infosystem Pvt Ltd
Pingback: Scala - Loop Statements | Adglob Infosystem Pvt Ltd