Hey there everyone! My name is DJ Spiess
and in this lesson we're going to look at another control statement. My childhood
Green Lantern game that I described back in the last lesson
was too basic. It offered two choices, the correct choice
and the choice that kills him. If I had known more control statements at the time
a given our hero more choices to avoid his demise, or
more creative ways for him to horribly meet his end. In this lesson
I'm going to share with you branching Java code based on a value using the
switch statement.
Coming up next.
The goals for this lesson
are to learn what is a switch statement. We're going look at
why we need to include breaks for each case. We'll look at the default case
and finally show you when it's a good idea to use a switch,
and when a simple if else statement will suffice. A switch statement
is a control statement that allows you to select a code block to run
based on a value. For example you might have a variable that can have
five possible values. In each case you want a different Java block to run.
An example might be a game. You want different actions to happen when the user
presses a button.
Depending on the button you might be moving up and down or
firing your weapon. The switch statement
allows you to switch to a different code block to execute while the program is
running.
You list the possibilities, and Java will select the correct block to run
at runtime. The switch statement looks like this.
You have a keyword "switch" to start your switch block. Immediately following the
keyword
is a variable that determines which block of code to switch to.
Now the variable can be a char, byte, short,
int. Basically anything with clear, distinct,
discrete values. So float and doubles are out.
As you remember from the floating-point lesson, we could never switch on 0.1.
A switch statement must account for every possible value.
While booleans could fit this description, they're not allowed.
You can also use a string, enumerated type, or primitive class type.
Those are types we are going to cover in later lessons.
We list every case we want to handle
in our program. Each case is defined with the keyword
"case", a colon and then the Java block to run.
The switch statement has a weird concept called
"fall-through". When Java is finished executing your case block,
it will fall through and start executing the next Java block.
It'll keep doing this until it gets to the end of your switch, or
it reaches the keyword "break". The reason Java does this
is because the C language also does this. And I'm not sure why C does this,
new languages like Apple Swift don't do it at all.
If you know why C and Java have fall-throughs, please leave a comment below.
I'd really love to know. What this means is
you should add the keyword "break" to every case statement.
If you don't, you should leave a really very large,
all caps comment saying that you're allowing Java to fall through
intentionally.
Really. All caps. Other programmers are going to think this is an
error if a break is missing. The last part of the switch
is optional, but you should always include it. It's called the "default" keyword.
This is the case to run, if all others fail.
It should be the last case you list. The only time you should consider not using
default
is if you've listed every possible case for all-time,
and clear that it's impossible to add another case, even if the default case does
nothing.
However you should leave a comment explaining why it does nothing. That way it's
clear to another developer reading your code that
you did consider the default case. Okay
we've seen enough keywords and structure to test this out. Let's go create a
switch statement in the IDE
and try it out. Okay for this example, switch operation
you can imagine that we're creating a calculator. I've created a character to
represent each operation.
So for addition we have a plus sign,
subtraction we have a minus sign,
division slash and multiplication
the star, Here I set the operation equal to addition.
We have two variables a and b. a equal to 23,
and b equal to 14. Now since our operation is addition,
it should run the first case and print out a + b
equals 23 plus 14. Let's try it. And it printed out the results for all of them.
This is illustrating the case, where I broke my own rule here.
I said that I put in a break statement for every single case.
So I'll let's go do that real quick. Okay now that I've added a break statement for each
one, let's try running it again.
And in this case it prints only
a + b equals 37. Now let's
assume another programmer came in and decided "hey I wanna do
powers and I'm going to use the carrot symbol for powers", but he doesn't go in and add that case.
So what this is illustrating is a case where I broke my own rule again.
I did not put in the the default. So when I run this code
there won't be an error, but it won't print anything either
because this switch statement hit and not a single case was matched.
What we should do is go in an add a default. Okay so I added a
default case and we're going to print out unknown operation.
So now when a programmer goes in to add that new operation without adding more code,
it will printout unknown operation. There's another thing you can do is switch
statements.
If you have multiple cases that perform the same action,
you can do this. This allows you to test for small ranges,
each case still requires a colon.
Now you could get the same results in your code using a long if-else statement. Each statement
would be a new
if-else. You'll want to prefer a switch statement if you have more than a few
possibilities.
Usually I go with the number 4. If I have 4
or more cases, I'll consider using a switch. Otherwise
I'll stick with an if-else. Other things that I always do is use breaks
and defaults. You could probably come up with an argument for specific case where it's not
needed,
but I can come up with a bazillion cases where it is needed. You're better off
including them.
The real difference between the two is switch branches on the value of a
variable.
If-else statements branch on conditions. If you're looking at branching your code
based on a single variable value,
the switch might make more sense. If-else works better when you're testing general
conditions
since if-else doesn't need to test a single variable.
Switches on the other hand read better when you working with enumerated types.
Again we'll cover enumerated types after we cover classes.
So that's how the switch statement works in Java.
You've seen the importance of using breaks in all cases, and why you should always
include the default case.
We've also talked about when to use a switch, and when to use the if-else.
Next we'll take a look at looping in Java. Till then!
Hey thanks for watching the video.
There is a quick quiz for this on DeegeU.com you if you'd like to gauge how much you
learned.
If you like the videos you are seeing, please let me know by liking the video and
hitting the subscribe button on the DeegeU channel on YouTube. I'd really appreciate that!
If you have concerns or questions please leave them in the comments below,
or on DeegeU.com. There's a poll on the front page of DeegeU.com
so you can let me know the topics covered next.
Thanks for watching, and see you in the next video.