Scala

Scala “For” Loop

The “For” loop in any programming language is used to iterate over a list or array of items. It does so by repeatedly calling a script until the iterator meets with its terminating condition. This article will walk you through the usage of the “For” loops in the Scala programming language in Ubuntu 20.04.

What are the “For” Loops in Scala in Ubuntu 20.04?

The “For” loops in Scala are used to serve the exact same purpose as in any other programming language, however, its functionality differs a little bit from the rest. It just takes two numbers within the “For” loop i.e., one as the lower iteration limit and the other one as the upper iteration limit.

How to Use the “For” Loops in Scala in Ubuntu 20.04?

For beginning with the usage of the “For” loops in Scala in Ubuntu 20.04, you will have to understand the two examples that we have formulated for you. The “For” loops in Scala are slightly different from the ones that we use in C and C++. In the latter programming languages, we have an iterating condition as well as an iterator for the loop. On the other hand, in Scala, both the iterating condition as well as the iterator is replaced by a single keyword that is either “to” or “until”. These keywords act as the range specifier for the loop to iterate over. However, both of these keywords are used to serve a different purpose and we will understand it by going through the following examples.

Example # 1: Using the “For” Loops in Scala in Ubuntu 20.04 with the “to” Keyword

Whenever we want our “For” loop in Scala to include both the upper limit and the lower limit in its iterations, then we always make use of the “to” keyword. For example, if our lower limit or the starting iterator’s value is “0” and we want to have the iterations till “10” i.e. 11 iterations altogether, then we will make use of the “to” keyword. The “to” keyword will basically iterate till “n” iterations where “n” represents the upper limit of the “For” loop. Now, let us take a look at the following Scala script to understand the functionality of the “For” loop with the “to” keyword.

In this Scala program, we have first created a class with the name “ForLoop” by using the “object” keyword. Then, after defining our “main()” function, we have simply enclosed a “println” statement within it for displaying a message on the terminal to notify the user that we are going to display the values of the “For” loop’s iterator on the terminal.

After doing that, we have defined a “For” loop with the iterator “var” whose lower limit is “0” and the upper limit is “5”. Moreover, since we have used the “to” keyword with our loop, therefore, the values printed on the terminal will range from “0” to “5” (both lower and upper limits included). Finally, within this loop, we have printed the values of the “var” variable on the terminal.

This Scala script was compiled by using the command given below:

$ scalac ForLoop.Scala

“ForLoop.Scala” is the name of our Scala script file.

Then, this Scala script was executed by using the following command:

$ scala ForLoop

“ForLoop” represents the name of our class within the Scala script.

The output of the above-mentioned Scala script is shown below. This output confirms that the “to” keyword always iterates over the complete range given including the lower and upper limits whenever it is used with the “For” loop in Scala.

Example # 2: Using the “For” Loops in Scala in Ubuntu 20.04 with the “until” Keyword

Whenever we want our “For” loop in Scala to include the upper limit-1 and the lower limit in its iterations, then we always make use of the “until” keyword. For example, if our lower limit or the starting iterator’s value is “0” and the ending iterator’s value is “10” and we want to have the iterations till “n-1” i.e., 10 iterations altogether, then we will make use of the “until” keyword. The “until” keyword will basically iterate till “n-1” iterations where “n” represents the upper limit of the “For” loop. Now, let us take a look at the following Scala script to understand the functionality of the “For” loop with the “until” keyword.

In this Scala program, we have first created a class with the name “ForLoop” by using the “object” keyword. Then, after defining our “main()” function, we have simply enclosed a “println” statement within it for displaying a message on the terminal to notify the user that we are going to display the values of the “For” loop’s iterator on the terminal. After doing that, we have defined a “For” loop with the iterator “test” whose lower limit is “0” and the upper limit is “5”. Moreover, since we have used the “until” keyword with our loop, therefore, the values printed on the terminal will range from “0” to “4” (lower limit till n-1). Finally, within this loop, we have printed the values of the “test” variable on the terminal.

When we executed our Scala script, we received the output shown in the image below that confirms that the “until” keyword always iterates over the lower limit given till n-1 (where “n” represents the upper limit of the “For” loop) whenever it is used with the “For” loop in Scala.

Conclusion

This brings us to the end of our article which was based on the usage of the “For” loops in the Scala programming language in Ubuntu 20.04. Firstly, we explained to you the basic purpose of using the “For” loops followed by two different examples. With the help of these examples, it will get extremely easier for you to use the “For” loops in the Scala programming language.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.