If You are interested to learn about the Scala If ELSE Statements
This chapter takes you through the loop control structures in Scala programming languages. There may be a situation, when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages −
Flow Chart

Scala programming language provides the following types of loops to handle looping requirements. Click the following links in the table to check their detail.
Sr.No | Loop Type & Description |
---|---|
1 | while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. |
2 | do-while loop Like a while statement, except that it tests the condition at the end of the loop body. |
3 | for loop Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. |
Loop Control Statements
Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. As such Scala does not support break or continue statement like Java does but starting from Scala version 2.8, there is a way to break the loops. Click the following links to check the detail.
Sr.No | Control Statement & Description |
---|---|
1 | break statement Terminates the loop statement and transfers execution to the statement immediately following the loop. |
The infinite Loop
A loop becomes an infinite loop if a condition never becomes false. If you are using Scala, the while loop is the best way to implement infinite loop.
The following program implements infinite loop.
Example
object Demo { def main(args: Array[String]) { var a = 10; // An infinite loop. while( true ){ 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
If you will execute above code, it will go in infinite loop which you can terminate by pressing Ctrl + C keys.
Value of a: 10 Value of a: 10 Value of a: 10 Value of a: 10 …………….
Example 1:
object Hello { def main(args: Array[String]) { // Local variable declaration: var a = 5; // while loop execution while( a < 10 ){ a = a + 1; println( "Value of a: " + a ); } } }
Example 2:
object Hello { def main(args: Array[String]) { // Local variable declaration: var a = 5; // do loop execution do { a = a + 1; println( "Value of a: " + a ); } while( a < 10 ) } }
Example 3:
object Hello { def main(args: Array[String]) { var a = 0; // for loop execution with a range println("Loop 1 - simple loop") for( a <- 1 to 5){ println( "Value of a: " + a ); } println("Loop 2 - loop with custom incremental value") for( a <- 1 to 10 by 2){ println( "Value of a: " + a ); } println("Loop 3 - loop in reverse, decreasing order") for( a <- 5 to 1 by -1){ println( "Value of a: " + a ); } println("Loop 4 - loop using until, one iteration less than simple loop") for( a <- 5 until 1 by -1){ println( "Value of a: " + a ); } println("Loop 5 - loop over collections") val myList = List(1,2,3,4,5); for( a <- myList ){ println( "Value of a: " + a ); } println("Loop 6 - loop over multiples ranges, loop within loop") for( a <- 1 to 5; b <- 1 to 3){ println( "Value of a: " + a ); println( "Value of b: " + b ); } } }
Types of For loops in Scala
There are four types of Scala For Loop:
a. i to j
Let’s take an example to demonstrate the syntax for i to j.
object Main extends App {
var a=7
for(a<-1 to 10)
{
println(a)
}
}
Here’s the output:
1
2
3
4
5
6
7
8
9
10
b. i until j
The other syntax we talked about is the until-syntax. Here’s an example:
object Main extends App{
var a=0
for(a<-1 until 10)
{
println(a)
}
}
c. Using Multiple Ranges in Scala for loop
We can use multiple ranges in a Scala for loop. For this, we just separate the ranges with semicolons(;).
object Main extends App{
var a=0
var b=0
var c=0
for(a<-1 until 3;b<-7 to 9;c<-12 until 15)
{
println()
println(a)
println(b)
println(c)
}
}
d. Using Collections
We can also use a collections object to provide values to the for-loop to iterate on. Let’s take an example with a list.
object Main extends App{
var a=0
val odds=List(1,3,5,7,9)
for(a<-odds)
{
println(a)
}
}
The output:
1
3
5
7
9
e. Using Filters
Adding extra if-conditions can let us filter out values from a collection.
object Main extends App{
var a=0
val odds=List(1,3,5,7,9,11,13,15,17,19)
for(a<-odds
if a>3; if a<15)
{
println(a)
}
}
And the output:
5
7
9
11
13
f. Scala for loop Yield
The last option is to return a value from a Scala for loop, and store it in a variable. We can also return it from a function. Then, we follow this by a yield statement. Note the curly braces.
object Main extends App{
var a=0
val odds=List(1,3,5,7,9,11,13,15,17,19)
var ret=for{a<-odds
if a>3; if a<15} yield a
for(a<-ret)
{
println(a)
}
}
And the output is:
5
7
9
11
13