In this lesson we would learn how to work with loops in Scala. A loop allows you to execute a particular block of code repeatedly depending on some condition.
The three types of loops we would cover are
1. for loop
The for loop executes a block (or blocks) of code a number of times. The number of times the code would executed is determined by the loop variable.
//For loop example object ForLoopDemo { def main(args: Array[String]) { var count = 0; for (count <- 1 to 10) { println("Current value of x: " + count) } } }
If you execute the code above, it would print the text “Current value of x: 1” 10 time each for each different value of x.
The statement
(count <- 1 to 10)
is called a generator statement and it can be modified by replacing the keyword “to” with “untill” like so:
(count <- 1 until 10)
Multiple Ranges
Scala allows you to specify multiple ranges separated by a semicolon(;). In this case, the loop will iterate through all possible combinations of the given ranges. Think of it as nested for loop. Below is an example:
//For loop with multiple ranges object ForLoopDemo { def main(args: Array[String]) { var x = 0; var y = 0; for (x <- 1 to 3; y <- 1 to 3) { println("Current value of x: " + x); println("Current value of y: " + y); } } }
The output will be as shown below:
Current value of x: 1 Current value of y: 1 Current value of x: 1 Current value of y: 2 Current value of x: 1 Current value of y: 3 Current value of x: 2 Current value of y: 1 Current value of x: 2 Current value of y: 2 Current value of x: 2 Current value of y: 3 Current value of x: 3 Current value of y: 1 Current value of x: 3 Current value of y: 2 Current value of x: 3 Current value of y: 3
2. while loop
While loop repeats a group of statements while a specified condition is true. It checks the loop condition before executing the loop the first time and subsequently. So the statements are executed so long as the condition is true. The syntax for the while loop is:
while(conditional expression){ statement(s); }
Note that the different between the for loop and the while loop is that the for loop will execute at least once, but the while loop may not even execute at all. The code below is an example of while loop.
//While loop example object WhileLoopDemo { def main(args: Array[String]) { var x = 5; while(x > 0) { println("Counting down at " + x); x = x - 1 } } }
The output of the code would be a countdown from 5 to 1
3. do-while loop
The different here is that the do-while loop tests the condition at the bottom of the loop. Therefore, the do-while loop would execute at least one time. The syntax for the do-while loop is:
do { statement(s); } while( loop condition );
The code below is an example of how the do-while loop works.
object DoWhileDemo { def main(args: Array[String]) { var x = 20; // do loop execution do { println( "Value of x: " + x ); x = x + 1; } while( a < 50 ) } }
4. Break Statement
Break statement is one of the loop control statements. The break statement terminates the current loop and transfers execution to the statement immediately following the loop. However, Scala does not originally support break statement. To achieve break statement in Scala, you need to do two things:
- Place the loop inside a loop.breakable block
- Then you need to use the statement loop.break.
The program below shows how the break statement works in Scala.
import scala.util.control._ object BreakDemo { def main(args: Array[String]) { var a=score = 0; val scores = List(60, 65, 70, 75, 80); val loop = new Breaks; loop.breakable { for( score <- scores){ println( "Value of score: " + score ); if( a == 70 ){ loop.break; } } } println( "Executes after the loop" ); } }