Scala for yield

For sequential iterators like loops, Scala yield is employed. After the iteration, the Scala language’s yield term is applied. To use this, we may carry out any action on the items inside the collection, such as increasing the set’s value by one. We shall then be back at the fresh collection. Whenever an action is finished, a for loop simply yields the outcome from the cache it has used to hold the resultant components. The set we are employing to loop remains the same type in which it yields. The for loop uses a cache inside to hold iterated results and pulls the end outcome out of that memory after completing all iterations. This guide will help you understand the usage of “yield” in “for” loops. While getting started with Scala programming, we need the Scala module installed at our end. You can use the Scala file to execute a code or the Scala shell to directly put some code queries on it. So, we will take a look at both ways. We have started Ubuntu’s terminal application and launched the Scala shell on it as presented.

Example 01

Within our first and most basic example, we will discuss the use of yield in the “for” loop. So, we have started the “for” loop iterating from number 3 to 8 using the “k” iterator. It means 3 would be its starting point, and 8 would be its endpoint. The yield keyword has been used after the simple brackets to display the value of “k” on each iteration until the end. On execution, this shows the vector containing all the values from 3 to 8 in an immutable collection.

The yield keyword can also be utilized to modify the result for each iteration. Let’s say we want to multiply the value at each iteration by 3. We used “k*3” after the keyword “yield” as presented. The output presented in the images shows that each loop value has been multiplied by 3 and then displayed as a yielded result.

In the same way, we have found the modulus of each iteration value “k” by 3 and got all 0, 1, and 2 in return.

Example 02

Let’s take a good look at the second way to create programs in a Scala file to discuss the use of yield with “for” loops. We have created a file named “test.scala” and added the following code. The code has been initiated with the “test” object to be used for execution and its main() program. We have initialized an array variable “A” with 5 numerical values. At the 4th line, we have initiated another variable, “v”, that has been using the “For” loop in it along with the “yield” keyword.

The “for” loop takes values from the array “A” as iterators and checks if the value is greater than 5 or not. If so, it will use the “yield” keyword to save that particular value in variable “v”. At the 5th print, the print statement is castoff to display the “Array:” string and the “for” loop has been started once again. This loop would use the variable “v” values as an iterator, and the print statement would display each of its values as “k”.

On listing all the files and folders of the home directory, we have found the test.scala file is also listed. We have to use the “Scalac” compiler of Scala with the file name “test.scala” to find out its issues in it. No errors have occurred so far. So, we used the “Scala” query to run the object of this code, i.e., test. The output shows the array values larger than 5 displayed.

Example 03

Let’s dig a little more to see how much we can achieve by using the yield in the “for” loop. Within the previous example, we have applied the “for” loop with “yield” only on the array structure. Now, we will make a comparison of using the “for” loop with the yield on the lists versus arrays. We have started our main() thread with the creation of an array “a1” and used the “for” loop until the length of the array to iterate each of its elements and display. Next, we created another variable, v2, using the “for” loop with the yield to iterate the values of array a1 and multiply each iteration value by 6.

Each yielded result would be saved to another variable, v1. The next println() statement shows that we will display the yielded values, i.e., after modification. Thus, in line 9, we have used the “For” loop once again to iterate values of variable v1 and print using the “println” statement. At the 10th line, we have created a list a2 with 5 numerical values and used the “for” loop to iterate and display each on the shell. On line 14, we have created a variable v2 that uses the “for” loop along with a yield to add each value of the list divided by 4 only if the modulus of a value by 3 equals 0.

Then, a println() statement has been telling us that we will display the yielded result. The “for” loop has been applied to variable v2 to iterate and display each of its elements.

After the compilation and execution, we have the array and list displayed before applying the yield. After that, the yielded results are displayed with not much difference.


This article was about using “for” loops with the “yield” keyword in Scala programming. We have discussed the yield in the “for” loop within the Scala shell and a separate Scala program file through some basic examples. We have explained how a modified result through “yield” can be saved to a new variable after the application “for” loop in our examples and also discussed the example containing a comparison of lists and arrays.

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.