Get startedGet started for free

Make decisions with if and else

1. Make decisions with if and else

Nice work! Those were some heavy concepts. Let's get back to Scala code in this lesson.

2. A program for playing Twenty-One

We can now hold gameplay data in variables and collections and use functions like bust.

3. Control structures

Let's add control structures. If you need to, take a second to pause the video and read this definition. The first control structure you'll learn is if/else.

4. A single if

To demonstrate, let's do if without an else. In a script, we'll define a val for the point value of a specific hand: 24, which is a bust. We start with a single if keyword: if space then a boolean expression in parentheses. The space is a Scala style guide recommendation, which applies to all control structures. With a single if, the style guide says to wrap the contents with curly braces, even if the contents are only a single line. Running this script, we get "This hand busts!" printed to output since 24 is greater than 21.

5. A single if

Changing the hand value to 18, nothing is printed.

6. if-else control flow

Next, if else, which you've already seen! This simple version of the maxHand function determines the maximum of two competing players' hands, ignoring whether each hand busts or not. Let's extract the function body and work with it separately.

7. if-else control flow

We'll define variables, handA and handB, and add print statements. Again, if is followed by a space then a boolean expression in parentheses. Next, a difference! No curly braces! The style guide says no curly braces if we have an else keyword.

8. if-else control flow

Here's improper style, with curly braces. Remove those,

9. if-else control flow

we get this, which IS in good style. After the if and the boolean expression, if the contents are only one line of code,

10. if-else control flow

we can put that code on the same line as the keyword itself, bringing us to the original version of this code. Let's evaluate it now. If handA is greater than handB, we print the value of handA to output, otherwise, we print the value of handB. The output is 19, since handB is greater.

11. if-else control flow

This version with the else on the same line as the if is also is good style AS LONG as the line is short and readable, with 100 characters in length being a common limit for Scala code. You may have noticed: since busts occur often in Twenty-One, this code and the maxHand function aren't that useful!

12. if-else if-else

To account for busts, we can use an if, else if, else flow. Again, we'll define variables to work with. Let's walk through the logic. If both hands bust, neither wins, so we print 0, which means "no hand wins" in our program. If handA busts, handB automatically wins, so we print that. If handB busts, handA automatically wins. If our program reaches this next else if, both hands are still alive. If handA is greater than handB, we print handA. Otherwise, handB is greater, so we print handB. Whoa! A little complex. Cool though. Since we have an else keyword, we omit curly braces.

13. if-else if-else

Running this script prints 20 to output, since 26 is a bust.

14. if expressions result in a value

In Scala, if is an expression and expressions result in values. That means if expressions can be assigned to a val. The val maxHand is the result of the if expression. 19, an Int.

15. Relational and logical operators

These are the operators you'll use most often for the boolean expressions of control structures.

16. These "operators" are actually methods!

Like the plus operator for addition and cons operator for prepending to lists, these operators are actually methods.

17. These "operators" are actually methods!

But that's future music.

18. Let's practice!

if/else practice time!