Scala Tutorials Part #13 - Loops
Originally Posted On : 20 Mar 2017 Last Updated : 26 Aug 2018
Loops are fundamental building blocks of imperative programming languages. Since scala also supports imperative style, lets take a look at different variations of loops and how they work.
This is part 13 of the scala tutorial series. Check here for the full series.
While loops in scala are very similar to their other language counterparts, so I am not going to explain it.
for loop however, can be used as a drop in replacement for any while loop construct.
- Concept of looping
- Introduction to the Syntax
- Controlling both indexes
- Controlling only the starting index
- No Index for expressions
- For loop with guards
- Yield construct
- Multiple Generators
The concept of looping came up in early languages such as the assembly language. Several variations exist with different instruction sets.
Taken from here, a few examples below in several languages.
For loops in assembly
xor cx,cx ; cx-register is the counter, set to 0 loop1 nop ; Whatever you wanna do goes here, should not change cx inc cx ; Increment cmp cx,3 ; Compare cx to the limit jle loop1 ; Loop while less or equal
Do while in C
The concept is the same immaterial of the language i.e doing something repetitively.
Loops in Scala do not have a three part structure like Java. A typical for loop in Java would like below,
A typical Scala for loop on the other hand does not have the increment part and also the variable declaration is slightly different.
Scala does not really have anything that is similar to Java for loops. If you take a closer look, the
to is actually a method on the
RictInt class which gives back a
Range collection from 1 to 10. But notice that we are using these directly from the
Int class. This is because the class
RichInt is not a normal class but something called a booster class. We will take a look at booster classes in later tutorials. For now, it is sufficient to understand that the
for is not something native to Scala, but it is just syntactic sugar over iterating a
i <- 1 to 10 part is called a loop generator. In below sections we will see how we can combine multiple generators to create a nested for loop.
In any for loop, there are two indexes involved i.e the beginning index and the ending one. We can use different constructs if we want to control either of these indexes.
i = 0 we loop till array length -1. As in many languages, arrays are indexed from 0. In this style of writing, we need to be careful in both the
beginning and end index. Typically one would make mistakes by starting at index 0 and then ending at
array.length which would cause
If we are worried about only the starting index then scala has a special construct for that.
until keyword makes sure that the loop iterates until the last element without us having to deal with the end index. Similar to
until is also a method in the
In java there are
Iterators in which you can loop through arrays without worrying about indexes and there are for loops in which we need not worry
about the indexes.
The scala version of the above would be quite similar,
There are reasons about why one would use any of the above. In the no index version there is no control over where the element is in the array i.e there is no track of the index. In certain situations there is no need to worry about it.
Many times we want to loop over something that satisfies a condition. In java we typically do so by wrapping the for loop inside of an
if statement. In scala there is a special syntax for it and they are named as
Guards in loops. Let’s look at an example.
Notice that the if condition is wrapped inside of the for construct. We can also chain complex if conditions.
This is just syntactic sugar over normal if statements.
For loops unlike Java generates a value in Scala. We saw this already in part 7 where is every expression is a value and every value is an object in Scala.
x is of type
Unit which is the equivalent of void in java, but as discussed before, this is actually a type.
Scala has a new keyword called
yield which produces new value out of a for loop.
Unlike regular for loops which is of type
Unit this actually yields as the name implies another array of 5 elements.
We can even print out the array
b which will turn out to be numbers
As we get deeper into functional programming, we will find out that this way of looping leads to really good and maintainable code.
We can easily create nested for loops with generator syntax.
This prints out
There are more advanced constructs possible with loops, but this should be enough to get started. As we explore more into Scala, we would find that loops can be replaced with more powerful constructs which are thread safe and safer/elegant to work with.