Which demons can also give you control over your code, you learn to control which parts of your code
run using, if elusive, and now statements, each statement has a condition and Java looks at the first
statement with a true condition and runs the code inside.
You can also use switch statements to control how your code runs.
So in this lesson, you're going to learn how do you switch as well as when do you switch versus if
else?
First thing I'll need you to do is create a new class by yourself inside the Section three project,
create a new file name, switch statements Java, and make sure the class has a main method.
So how do you switch a switch statement compares one value against a list of values.
In other words, it compares an argument against a list of cases.
The argument is the value that you're comparing and if the argument matches value one, the first case
runs, if the argument matches value to the second case runs, if the argument matches value three,
the third case runs and if the argument matches none of the values the default case runs.
And so back in our code, we're going to see this in action, we're going to make it into variable named
and month and it's going to story value that represents a month five, which corresponds to the month
of May.
And so now we're going to make a switch statement and it's going to compare your month value against
the list of month values in the form of cases.
So we're going to rate case one.
And there are 12 months, so we'll need 12 cases, so I'm going to copy this 11 more times.
And I'm going to number each case in ascending order.
Followed by default, now, if the argument month matches one of the cases, that case is going to run
and we're going to need to print that month in text.
So if the month ends up being equal to one, we're going to print January.
If it matches, case two will print.
And you know what, I'm just going to copy this Prince theme throughout.
And now I'm going to fill in the appropriate month values.
And if I were a month value doesn't match any of our cases, a default case should execute.
And if it does, we're going to print please enter a valid month.
They think we're ready to run our code.
Whoa, that's weird.
The month argument matches Case five, Jalaa clearly ran Case five, but it also runs every case that
follows.
Well, that's because by nature, which runs every case that follows the case much, it's like the switch
turns on when there's a match.
Let's have a look.
You saw the value five in the month.
Variable switch accepts your month value as an argument, and it compares it to a list of month values.
It starts comparing your value against each case.
Case one, two, three, four, five.
Aha.
We have a match.
So the switch turns on and Javor runs case five, but it also runs every case that follows the case
match.
This is pretty frustrating.
How do we fix it?
That is exactly why we have the brake keyboard, the brake keyboard can break the switch statements.
And so every case inside your switch them it needs to end with the keyword that way after your case
runs, Java knows to break out of the switch statement.
So back in our code, we're going to fix the switch statements.
We're going to add the break keyword at the end of each case.
And now we can rerun our code.
And that's much better.
So which compares your value against each case?
Uh huh.
We found a match after Jova runs the code inside case five, it then sees the break keyword and that
breaks out of the switch statements.
So why didn't we at break next to default?
Well, default is the last thing that can run.
Switch already breaks after default, but for the sake of example.
We'll set month equal to some absurd number like 30 to.
We'll rerun our code.
And the month value doesn't match any of the cases, so the default case Rut's.
All right, so it's time for the million dollar question.
When do we use a Felsen?
When do we use switch?
I want to preface this by saying I use a false 95 percent of the time.
And if statement can test any condition, a switch statement can only compare a value against the list
of others.
So we'll think back to the OP we just built, the one that grants diploma if a student has over 40 credits
and a minimum GPA of 2.0.
Can you think of a way to do this using such absolutely impossible GPA and credits can be any number
from an infinite range of values.
Are you going to make infinite cases?
I don't think so.
The way it fails is designed around conditions and ranges.
It's way more flexible than comparing a value against the list of cases.
So if you fast forward to the AP book just now, you can easily use it false.
So why didn't we use them?
We'll have a look at this code looks pretty ugly, doesn't it?
Once again, the screaming cat is there to tell us what not to do if you're comparing one value against
the list of other values, don't use a false uSwitch because it's honestly prettier.
It's easier to read.
And they also run a tiny bit faster than anything else in general.
So if you don't want to get lost, just follow this rule of thumb and you should be golden.
You can only use switch to compare one value against the list of others in any other scenario.
And 95 percent of the time you're going to need efforts to control how your code runs.
In this section, you learn to fully control how your code runs using NFLs and switch statements, if
false runs a piece of code.
If the condition is true, conditional statements, officials are way more flexible than switch statements.
Ninety five percent of the time you're going to use efforts to control how your code runs.
Switch runs a piece of code.
If the argument matches a case on the off chance that you need to compare a value against a list of
other values, then you can use switch.