Home »
Scala
The for loop in Scala
By IncludeHelp Last updated : October 10, 2024
For loop
for loop in Scala is used to execute a block of code multiple numbers of times. It executes the block n number of times where n is specified by some integer initialized before the start of the loop. This means the programmer knows the exact number of time the loop is going to execute beforehand.
In Scala, there are many types of valid for loops that have specific usage and can be used in code as per user need. They are,
- for loop using ranges
- for loop with collections
- for loop with filters
- for loop with yield
- for loop using by keyword
For loop using ranges
A loop with that integrates within a range of values of its counter is for loop using ranges.
Syntax
for(var i <- Range){
// code to be executed...
}
The Range here is a range of numbers. There are two ways to define range in a for loop.
Using to keyword to define the range, i to j.
Syntax
for(var i <- a to b){
// code to be exected
}
Example
object MyClass {
def add(x:Int, y:Int) = x + y;
def main(args: Array[String]) {
var i = 0;
print("Print numbers from 5 to 10\n")
for( i <- 5 to 10){
print(i + "\n")
}
}
}
Output
Print numbers from 5 to 10
5
6
7
8
9
10
Code explanation
In the above code, we have used the to keyword to define a range. We have initialized a loop variable i. It takes both first and last values in consideration of the loop. It increases the value from 5 to 10 and increases the value of i by one. Loop runs from 5 to 10 and prints each value to the screen.
Using until keyword to define the range, i until j.
Syntax
for(var i <- a until b){
// code to be exected
}
Example
object MyClass {
def add(x:Int, y:Int) = x + y;
def main(args: Array[String]) {
var i = 0;
print("Print numbers from 5 until 10\n")
for( i <- 5 until 10){
print(i + "\n")
}
}
}
Output
Print numbers from 5 until 10
5
6
7
8
9
Code explanation
In the above code, we have used the until keyword to define a range. We have initialized a loop variable i. It takes first value in consideration and not the last one for the loop. It increases the value from 5 to 9 and increases the value of i by one. Loop runs from 5 to 9 and prints each value to the screen.
For loop with collections
The collection is a container of things. The list is a type of collection and for loop works by iterating over the list.
Syntax
for(var x <- list_name){
//code to be executed
}
Example
object MyClass {
def add(x:Int, y:Int) = x + y;
def main(args: Array[String]) {
var i = 0;
val myList = List(1,2,4,6,9);
print("Print numbers of list\n")
for( i <- myList){
print(i + "\n")
}
}
}
Output
Print numbers of list
1
2
4
6
9
Code explanation
The code initializes one loop variable i and a collection of type list myList that contains the variables to be iterated on. The for loop iterated over the array one by one and puts the value in the loop variable. The loop variable the prints the value at each iteration.
For loop with filters
You can add filters to the iteration of the for loop. This will check an optional condition which needs to be true for the code to get executed.
Syntax
for(var x <- 1 until 15 if condition ){
//code to be executed
}
There can be more that one condition that can be used to filter out the for loop result.
Example
object MyClass {
def add(x:Int, y:Int) = x + y;
def main(args: Array[String]) {
var i = 0;
print("Print numbers from 5 until 20 which are divisible by 3\n")
for( i <- 5 until 20
if i%3 == 0 ){
print(i + "\n")
}
}
}
Output
Print numbers from 5 until 20 which are divisible by 3
6
9
12
15
18
Code explanation
The code initializes a loop variable i this variable the increments the value by one. Before executing the loop the for statement checks for the condition. This condition checks if the loop variable can be divided by 3 properly. Then based on this condition the loop code block is executed. For every value of the loop variable that is divided by 3, it prints the value of the loop variable.
For loop with yield
Yield variable is used when we need to return the counter variable as an array to a variable. This means yield keyword return the counter variable at each iteration.
Syntax
var forVals = for{var i <- 4 until 35
condition
}yields i
Example
object MyClass {
def add(x:Int, y:Int) = x + y;
def main(args: Array[String]) {
var i = 0;
print("Print numbers from 11 until 15 using yield\n")
var forOut = for{ i <- 11 until 15}yield i
for(i <- forOut){
print(i + "\n")
}
}
}
Output
Print numbers from 11 until 15 using yield
11
12
13
14
Example explanation:
The above code elaborated the use of yield variable. It is used to filter out any collection into a filtered collection. Yield variable copies the value of loop counter to a list that stores all the values of it. In this, code be have used for loop to iterate over the list and print the numbers from 11 to 15.
For loop using by keyword
by keyword is used to increase the counter variable incrementations. This means, generally the counter is increased by 1 but using by 4 will increase in by 4.
Syntax
for(var x <- 1 to 54 by 5){
//code to be executed...
}
Example
object MyClass {
def add(x:Int, y:Int) = x + y;
def main(args: Array[String]) {
var i = 0;
print("Print numbers from 11 until 20 increasing interval by 2\n")
for( i <- 11 until 20 by 2){
print(i + "\n")
}
}
}
Output
Print numbers from 11 until 20 increasing interval by 2
11
13
15
17
19
Code explanation
The code initializes a loop variable i this variable the increments the value by two. The by variable is used to increase the counter increment value to a specific number. If this example, the increment value is 2. So the loop prints every second number from 11 to 20.