Repetition statements for Scala include for, while, and do while loops. "For" expressions are also called for comprehensions in Scala. The term comprehension expresses the idea that we are traversing one or more collections of some kind, comprehending wh
- [Instructor] Repetition statements in Scala include loops, such as a for loop, a while loop, and a do while loop. For loops are also called for comprehensions in Scala. The term comprehension expresses the idea that we are traversing one or more collections of some kind, comprehending what we find and computing something new from it. I think the best way to talk about these repetition statements is to look at some examples. So here I have several examples of for comprehensions, a while loop, and even a do while loop.
Let's start at the top and walk through the code. At the very top, I'm declaring a variable i and setting it equal to the value five. Then we use the keyword while, that says while i is greater than or equal to zero, print out the number, followed by a comma, and then decrease i by one each time. Then I have a print line statement just to separate the output from this loop from the next one. So this is going to print out the numbers five, four, three, two, one, zero, all followed by a comma.
Next, on line nine, I declare a variable called fruit, and fruit contains a list of different types of fruits. Apples, bananas, cherries, pears, and blueberries. I wanted to show you that you can actually traverse or iterate over a list of strings. So line 10 says for, it has a variable f that is going to hold each string as we go through our fruit list. It says for f, and it uses the left-hand arrow symbol, which indicates that the values on the right will be assigned to the variable f.
For each fruit value, it's going to print it out, so below that I have print line f. So it'll print out apples, bananas, cherries, pears, and blueberries on a separate line. Using that same list, I wanted to show you how we could attach filtering to our for comprehensions. On line 14, I have for f, just like I did above, with the left-hand facing arrow. It traverses the fruit list, but this time, I'm adding a filter that says if the fruit starts with the letter C.
Now, there's only one fruit in my list that does start with C, and that's cherries. So on line 15, I'm printing out all fruits that start with C, and only print that once, with cherries as the fruit. Next, on line 17, we can also nest our for loops. So in this case, I have for i, and this time i is going from one to five. J goes from one to five also. So every time i is one, j will go from one to five.
When i is two, j will go from one to five, et cetera. And it prints out the product of multiplying i times j separated by a comma. All right, let me scroll down a little bit. We've seen a while loop, we've seen a for loop that loops through a list of strings. We've also seen a nested for loop. Now, on line 23, I have a comment there that says for each even number between one and 10, I want you to square it. So the results we expect would be two squared, which is four, four squared is 16, six squared is 36, eight squared is 64, and 10 squared is 100.
So the for loop looks like this, on line 24. For every value n, where n represents the number from one to 10. Now, we have a second value there that says only evaluate the numbers where n percent sign two is equal to zero. So only get the ones where n is even. And then here's a new keyword for us, yield. It's going to yield n times n. That's almost like a return, it's going to actually provide us the value of n squared.
When we run the program, we'll see if we get our expected results. On line 26 is a comment indicating that what comes next is code that will sum each pair of numbers, so that's a little different. I don't really have an easy way to do that in Java. But in this case, I have a list of numbered pairs, and what I'm going to do is in my for loop I'm going to add the values inside the parentheses. So one plus two will give me three, three plus four will give me seven, five plus six will give me 11.
So on line 28, we have for, and we use the values a comma b, that represent the values in the list for each ordered pair, and we yield a plus b. The next one, line 30, is going to create a vector of numbered pairs. So this time, I'm actually going to yield a pair whereas the lines above it I'm actually taking a pair and yielding a single integer. This for loop says for x, goes from zero to four, and y goes from zero until three.
So y is actually showing us that we can go zero, one, and two, it means it stops when it gets to three. So we should get the pairs when x is zero, I'll get y of zero, so I'll get zero, zero, then I'll get zero, one, then I'll get zero, two, and then when x goes to one, y will be zero, y will be one, y will be two, and then when x goes to two, y will be zero, y will be one, y will be two, et cetera. So we'll take a look at that output as well. And the final one is a typical do while.
A lot of times you'll use a do while loop when you want to print out a menu of choices. 'cause you want to present the menu to the user at least once. A do while loop, the difference there is it'll always execute the code at least once. So for this particular loop, I needed to import scala.io.StdIn.readLine and readInt. I'm really only going to use the readInt, but I've included both libraries just in case. On line 34 I carried a variable called choice, which is set to zero.
I create a variable result which is set to a null string. Then I have my do statement, and I have a print line that will print out choose one. Notice the escape sequence characters there, the backslash n. So that will put a blank line in between each choice. One is Java, two is Scala, three is C++, and four is Exit. Next, I use the readInt to read the integer that the user types into the variable choice. On line 39, I set the result equal to pattern matching the choice value with each of these case statements.
It automatically matches one through four, but if the user puts something other in there, it'll print out Invalid choice, try again. On line 46, I print out the result, and on line 47 I have the while statement that goes with the do on line 36 that says if the choice is not equal to one, two, three, or four, then don't exit, keep going. If it is equal to one of those, it'll get out. All right, let's go ahead and run this program. I'm going to open up my command prompt and I'm going to go into my interpreter, because what I have here, let me go back over to Sublime for a second, what I have here is just a sequence of commands.
It's almost like a Scala script file. It doesn't have an object or a class to encase the entire thing. So all I want to do is load my script into my Scala interpreter, but this saves me from having to type the code over and over again. All right, in my command prompt, I am now in my Scala interpreter. So now I can load the file. I can load repetition.scala. Don't forget, I'm still in my My Scala Projects folder so it knows where to find it.
Okay, I'm going to minimize this a little bit just so I can see the code at the same time. So let's scroll back up to the top here in the Sublime editor, and let me bring up my command prompt. And you can see at the very top, it prints out five, four, three, two, one, zero. The next line shows that I declared a list of string objects that has these values. And then I told it to print out the fruits. Apples, bananas, cherries, pears, and blueberries. Then on line 14 is where I only printed out the fruits starting with C, which is just cherries.
The nested for loop prints out the numbers one, two, three, four, five, two, four, six, eight, 10, et cetera. The next for loop on line 24 printed out all the even numbers squared from one to 10, and we did get the expected results of four, 16, 36, 64, and 100. Let me scroll down a little more on my Sublime text editor. Next, we had the list of ordered pairs where it was going to add the numbers and only print out the sum, and that's where it printed out three, seven, and 11, followed by a new vector that used the values where x started at zero and went to four, and y started at zero and went to two.
And you can see the list of ordered pairs here on the right. And then finally, now we have our last do while loop, which has, it shows the import statement, it shows the declaration of the choice variable, the declaration of the result variable, and then it printed out my menu, where it says choose one. I'm going to click into my output window here and I'm going to type in a five. And you can see it says invalid choice, try again. This time I'll type in a two, and it'll say you chose Scala.
Since it was a valid value, it got out of my do while loop. The type of the value produced by a for expression is the same as the type of the first generator, so inside the for loop we can use a generator to generate our values. As an alternative to using yield followed by an expression, we can omit the yield keyword and use a block of code in place of a single expression. A for statement can always be translated into a series of for each, filter, map, and flat map method calls.
And we also saw how to create a while loop and a do while loop. These are all types of repetition loops that you can do in Scala.
- Installing Scala
- Integrating with IDEs
- Creating variables
- Using special functions
- Basic types and operations
- Tuples, sets, maps, arrays, and lists
- Classes, fields, and methods
- Singleton objects