In software programming, certain situations may arise where we need to execute a block of code several times. The loop statements helps in this repetitive tasks and are executed sequentially. Loop control statements include while, do while and for loop. Let’s look into how we use them in Scala programming language.

Scala while loop

A while loop statement is executed repeatedly until the condition is false.

The syntax for while loop is

The condition can be an expression, boolean value or a non zero value. The statement can be a single statements or a group of statements. When the specified condition becomes false the control is transferred to the next statement outside the loop.

Consider an example below.

We are declaring and initializing a variable sid with an initial value of 5. In the while loop the condition if sid less than 15 is checked and the value of id is printed after which sid is incremented. The loop stops when sid is greater than 14

Run the above code by typing Stu.main(null) in scala shell and you will see below output.

Scala-while-loop

You can also save the above code in Stu.scala file and run as shown in below image.

Scala-while-loop-program

Scala do while loop

The do while loop statements executes at least once and then the while loop is executed till the condition is true.

The syntax for do-while loop is;

The statements enclosing the do loop is executed first and then the while condition is checked.

Consider an example below.

The value of student id is displayed and the value is incremented by 1 and then the condition in while loop is checked.

Run the above code by typing Stud.main(null) and you will see output as in below image.

Scala-while-loop

Scala for loop

For loop is a repetitive structure which allows us to execute a block of code multiple times similar to the other loops we saw.

Since for loop is widely used, there are various forms of for loop in Scala.

For loop with ranges:

The syntax of for loop is

Range represents numbers as x to y or x until y. The left arrow operator(
object Student {
def main(args:Array[String]) {
var sid = 0
for (sid <- 6 to 12){
println(“Student Id is :”+sid)
}
}
}

In this example we are printing student id specifying the range as 6 to 12.

Run the above code as shown in below image with output.

Scala-while-loop

Alternatively for loop range can also be written as x until y.

Here we specify range as 9 until 14 so that the values from 9,10,11,12 and 13 will be displayed.

Multiple ranges can be specified using semicolon (;) as the separator and the loop iterates for all possible combinations.

For example

Here we are specifying ranges for id and marks together separated by a semicolon.

Run the above code by as shown below.

For Loop with collections:

for loop can be used efficiently to iterate over collections. The syntax is

List represents collection having list of all the elements to iterate using for loop.

Consider an example below.

Below image shows the output produced by above example.

for loop with Filters

Some elements can be filtered in the for loop using if statements.

The syntax is;

For example;

Here we are using filter conditions as id greater than 6 and id not equal to 11.

Run the above code by and you will see output as shown below.

for loop with yield:

The return values can be stored in a variable or can be returned through a function. To do this use the keyword yield as

For example;

The value of id is stored in the variable id using the keyword yield. Below image shows the output produced.

That’s all for loop control statements in Scala programming, we will look into more Scala features in coming posts.

By admin

Leave a Reply