What is a “ForEach” Loop in Scala in Ubuntu 20.04?
A “foreach” loop in Scala is simply a loop that can be used to iterate over a range of elements for simply printing those elements, calculating the sum of these elements, finding and replacing any particular element, finding the maximum element out of these elements, etc. The best thing about this loop is that you do not even need to explicitly specify an iterator for this loop in most cases. You can simply access it with the help of an already declared variable.
Instances of Utilizing the For Each Loop in Scala in Ubuntu 20.04:
We can keep on explaining the working of the Scala “foreach” loop verbally; however, all of that explanation will make more sense to you once you see some practical examples of the implementation of this loop. Therefore, below, we have shared five different examples that will build your understanding of the “foreach” loop in Scala in Ubuntu 20.04.
Example # 1: Printing the Values of an Iterator in Scala:
This Scala program is intended to teach you how you can print the values of an iterator on the terminal. The said Scala script is as follows:
In the Scala script shown above, we have defined a “ForEach” class with the “object” keyword. Then, we have defined our “main()” function within which we have declared a value with the “val” keyword and the title “iterator”. We have assigned five different values to this iterator using the “Iterator” keyword. After that, we have simply called the “foreach” loop for printing the values of this iterator on the terminal by using the “iterator.foreach(println)” notation.
Now comes the time for compiling this Scala script with the command shared below:
In the above-mentioned command, “ForEach.Scala” refers to the name of our Scala script file.
Upon a successful compilation, we can execute this Scala script with the subsequent command:
In the above-mentioned command, ForEach refers to the name of our Scala class used in our script.
All the values of the iterator used in our Scala script are shown in the image below:
Example # 2: Printing the Elements of a List in Scala:
This Scala program is intended to teach you how you can print the elements of a list on the terminal. The said Scala script is as follows:
In the Scala script shown above, we have defined a “ForEach” class with the “object” keyword. Then, we have defined our “main()” function within which we have declared a value with the “val” keyword and the title “list”. We have assigned different string elements to this list by using the “List” keyword. After that, we have simply called the “foreach” loop for printing the elements of this list on the terminal by converting them to the upper case while using the “list.foreach(x=>println(x.toUpperCase))” notation.
Note: The toUpperCase function converts all the alphabets of the given words to capitals.
When we compiled and executed this Scala script, all the elements of the list used in our program were printed on the terminal in upper case, as shown in the following image:
Example # 3: Calculating the Sum of all the Elements of a List in Scala:
This Scala program is intended to teach you how you can calculate the sum of the elements of a list and print it on the terminal. The said Scala script is as follows:
In the Scala script shown above, we have defined a “ForEach” class with the “object” keyword. Then, we have defined our “main()” function within which we have declared a variable with the “var” keyword and the title “sum”. We have initialized this variable with the integer value “0”. Then, we have declared a value with the “val” keyword and the title “list” and assigned to it five different integer values by using the “List” keyword. After that, we have simply called the “foreach” loop for calculating the sum of all the elements of this list by using the “list.foreach(sum+=_)” notation. Here, the “sum+=_” notation will do the real magic of iterating over the entire list and adding all of its elements together. Finally, we have printed the result of the “sum” variable on the command prompt by using the “println” command.
When we compiled and executed this Scala script, the sum of all the elements of the list used in our program was printed on the terminal, which in the current case is “15” as shown in the following image:
Conclusion:
This article was meant to explain to the readers the usage of the “foreach” iteration in Scala in Ubuntu 20.04. We shared with you three examples that implemented this loop in Scala. In the first example, we simply shared the method of printing the values of an iterator on the terminal. Then, in the second example, we talked about the procedure of printing the elements of a list on the terminal. Finally, we shared an example for calculating the sum of all the elements of a list using the “foreach” loop of Scala. By understanding these examples, you will quickly grasp the concept of using the “foreach” loop in Scala in Ubuntu 20.04.