Scala for loop – Types, Syntax and Example of For loop

1. Scala for loop – Objective

Sometimes, in your code, you may want to execute a set of statements for, say, 100 times. Would you type them a hundred times? Obviously not. This is where loops come in. A loop lets us execute a group of statement a set number of times, or until an expression becomes false. In this lesson, we will see the Scala for loop with its syntax and examples. Along with this, we will discuss different types of for loop in Scala.

So, let’s start Scala For Loop Tutorial.

Scala for loop

Scala for loop with Syntax and Examples

Learn: Loop Control statement in Scala

2. What is For Loop in Scala?

Scala for loop lets us execute specific code a certain number of times. It is a control structure in Scala, and in this article, we’ll talk about the forms of for-loop we have here.

3. A syntax of Scala For Loop

Let’s first check out the syntax for Scala for Loop.

for(var x <- Range){
statement(s);
}

The arrow pointing leftward is a generator; it generates individual values from a range. Range is a range of numbers; we may use a list for this. We can also represent this as i to j, or as i until j.

Scala Quiz

4. 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
Learn: While loop in Scala

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)
    }
}

What would the output be? Combinations of all values:
1
7
12

1
7
13

1
7
14

1
8
12

1
8
13

1
8
14

1
9
12

1
9
13

1
9
14

2
7
12

2
7
13

2
7
14

2
8
12

2
8
13

2
8
14

2
9
12

2
9
13

2
9
14
Learn: do-while loop in Scala

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

Learn: Scala if-else statement

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

5. Conclusion

These are the ways we can implement for loop in Scala. Try your own questions, and ask us your doubts in the comments section.

Reference

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.