1. while and the imperative style
Let's continue our program. In this lesson, we'll automate the repetition of gameplay code with while loops.
2. Control structures
While loops are another control structure, like if/else.
3. Hip hip hooray
First, a minimal example. Say we want to congratulate the winner of a hand of Twenty-One with the classic cheer, "Hip hip hooray!" printed to output.
4. Loop with while
Let's open a Scala script. The first step is defining a counter variable. Often, we name it i and set it equal to zero. You could initialize to 1, or even 1000, but zero is a common convention. Zero keeps the mental math simple and makes your code more concise when accessing elements of Scala collections like Arrays or Lists, which are zero-indexed.
Next, we'll define a val to represent the number of times we want to repeat our code. Per tradition, hip hip hooray repeats three times.
Next is the while loop itself, which has two parts. The body: a block of code between curly braces, and the control statement: a boolean expression, must be in parentheses. The body of the loop is executed repeatedly until the boolean expression is false. Style guide note: always put a space between the keyword while and the parentheses, and always wrap the body of a while loop in curly braces.
5. Loop with while
This body contains two statements, each indented two spaces as per the Scala style guide. The first statement prints "Hip hip hooray!" to output. The second statement increments i by one, which could be rewritten
6. Loop with while
like this, like in C and Python.
7. Loop with while
++i and i++ DON'T work like they do in Java.
8. i = 0
For the first iteration of the loop, i is 0, and the cheer is printed.
9. i = 1
Second iteration, i is 1, cheer printed.
10. i = 2
Third iteration, i is 2, cheer printed.
11. i = 3
The body doesn't execute a fourth time because i is 3 and the boolean expression i less than numRepetitions is no longer true.
12. Loop with while over a collection
Looping over collections is common. Say we want to loop over several hands in a round of Twenty-One to see which hands bust. To begin, we still need that counter variable.
Next, we define an array to iterate over: hands is created with hand values of 17, 24, and 21.
With collections, the control statement is a little different. Here we loop while i is less than hands dot length, where hands dot length is the number of elements in the hands array.
13. Scala is object-oriented
hands dot length is a feature of Scala being an object-oriented language. Remember our rule of thumb: almost everything is an object in Scala. We won't dive much further into object-oriented programming in this course. All you need to know now is that objects can do useful things with dot something, like get the number of elements in hands with hands dot length, which is 3.
14. Loop with while over a collection
In the body, the first statement, print line bust hands i, prints true or false, depending on whether or not the ith hand is a bust. The second statement increments the counter variable.
15. Loop with while over a collection
Running this code produces false (17 is NOT greater than 21), true (24 IS greater than 21), and false (21 is NOT greater than 21).
16. Like if, parentheses required for while
In Scala, parentheses MUST surround the boolean expression for while, just like for if.
17. Like if, parentheses required for while
That means you can't do this, which is allowed in Python for example.
18. The imperative style
Writing while loops is a feature of a style of programming called the imperative style, which actually isn't the Scala PREFERRED style. Keep this in mind for next lesson.
19. Let's practice!
Let's get practicing for a while first.