Scala Tutorials Part #13 - Loops
Originally Posted On : 20 Mar 2017
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
- Controlling both indexes
- Controlling only the starting index
- No Index for expressions
- For loop with guards
- Yield construct
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.
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.
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.
It is to note that all for loops in scala generate a value unlike java.
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.