In all the examples we have looked at so far, the operations performed on the elements are forgotten once the for expression is executed. But what if you want to store the values and create new collections?
As discussed in the first lesson, control structures in Scala have return values. To obtain the return value of a for expression, we can use the yield expression which does exactly what its name implies: yield the return value.
yield must be placed before the entire body or block of code.
Let’s take the previous example where we are capitalizing the elements of an array. But this time we will create a new array of the capitalized elements.