>> Remember I planned to program a smart coffee maker from the beginning of this lesson?
We wanted to program a coffee maker that gives us our favorite cup of coffee.
To build this, our coffee machine needs to know who's asking for coffee,
and then we can give it instructions to select the right coffee for that person.
Each person will choose a three-digit passcode that they can
enter into the coffee maker to get their favorite cup.
I've chosen my passcode to be 555,
which will get me an espresso.
>> Or if you prefer something with flavor,
you could enter 312 and get this delicious vanilla latte.
>> Everyone has their own preference,
and the more passcodes a coffee maker knows,
the smarter it will be.
Now let's get into programming this behavior.
For our smart coffee machine,
we wanted to check the value of an int variable called passcode,
and test it for equality against the list of specific values that it can take.
Passcodes like 555 for me and my espresso,
and 312 for Asor and his vanilla latte;
and let's include one more passcode 629 that makes a drip coffee.
We can program this behavior using a series of decision making statements.
Let's say we have access to our int passcode variable,
but we aren't sure what value will be.
We just know that it will be some three-digit number.
We can then write an if statement and else ifs with
test conditions that check if our passcode is equal to a certain value,
like 555 or 312.
And then inside each statement,
we'll assign what type of coffee to make,
based on the value of passcode;
and the type of coffee is kept track of in a string variable coffee type.
Then at the very end of our code,
we'll print out the value of coffee type.
So if we look at our first if in detail,
it tests if our passcode is equal to 555;
and we test for equality with this double equal sign;
and the value 555 corresponds to our espresso passcode.
Then if this test condition is true,
we'll enter if block of code and set coffee type to the value espresso.
Then because we've executed the code in our if,
we'll skip down to the int curly brace of all these statements to our print line of code,
and this will just print out espresso.
And the rest of this code works similarly.
We check if the passcode is equal to 312.
If it's not, we then check if it's equal to 629;
and if it's not equal to any of these,
our coffee type will be set to unknown.
But this type of code can get really long for multiple passcode checks;
and we're doing a similar check for equality in each of these test conditions.
We're saying passcode equals equals some specific value.
And Java gives us another way to write this code with the switch statement.
A switch statement does the same thing a little more concisely.
It let's us check the value of a certain variable like our passcode,
and test it for equality against a list of possible values it can take,
like 555, 312 and 629.
Each of these specific values is called the case,
and we can program different behavior for each case.
In Java, this switch statement looks like this.
It says switch followed by the variable it
wants to check for equality surrounded by parentheses.
Then, it has curly braces that will contain all of our equality checks,
and our equality checks are just these specific cases;
which here are the specific three-digit passcodes that our program knows,
555, 312 and 629.
Our first if statement here will execute the same code as our case 555 here.
The value of our passcode will be checked for equality
against the number 555 and if it matches,
we'll set our coffee type string to be equal to espresso;
and that's the same thing we did in our first if.
But if our passcode is not equal to 555,
then we'll move on to our next case.
Just like we'd move on to our next else if.
And case 312 and case 629 correspond to our else if code.
This case checks for passcode is equal to 312.
If it is, we set our coffee type to be vanilla latte.
If it's not, we move on to our next case.
Just like we move on to our next else if.
So these three cases check for our three known passcode values.
And if our passcode matches any one of these values,
we'll enter the corresponding case code
and set our coffee type to the correct coffee value.
Notice that after each case is defined;
like case 312, we write a colon,
not a semi-colon, and then the instructions we want our program to execute,
which is to give a certain value to our coffee type string.
And this is the format for all our cases: the word case,
the specific value we want to check for equality,
a colon, and then the code we want to execute.
After each case is this word break.
A break happens after code inside a case executes.
It breaks out of the switch statement and goes to its ending curly brace.
So having breaks after each case
ensures that only one of these blocks of code will execute.
This is just like how only one block of code in
a series of ifs and else ifs will execute at a time.
The break will break out of our decision making code and go to
the end of the ending curly brace where it will print out the value of coffee type.
There's also a default case that acts the same as an else statement.
The default code will execute whenever other cases aren't met,
like if the passcode was something like
914 and we don't recognize it or don't have a case for it.
So both of these sets of code do the same thing.
In fact, if our passcode was 555,
the print output for both of these codes would be espresso.
For this code, it would enter our first if statement and set coffee type to espresso,
and for this code it would check the value of our passcode against our first case;
case 555; and because these match,
it would enter this code and set our coffee type to espresso again.
So switch statements can be implemented with ifs and else's,
but it can be easier and more understandable to write
code in a switch if you're doing a lot of equality checks.
And as a programmer, it's up to you to decide what to
use to make your code easy to understand and use.
Let's get some more practice with switch statements.