Tim: In this video we'll just take a look
at some decision constructs
that you may not necessarily ever use
but that should point out items to look for
on the certification exam.
So let's start by looking at the switch statement
and we're gonna create a new class
in our decisions project 'SwitchStatementOddities.'
I'll paste in some code.
Okay, there's the code.
So terms of this code,
it contains an array of byte assigned values,
a for loop that loops through the array of byte values,
also contains a switch statement with an expression in it,
which simply multiplies the pass value by 10
and the switch statement labels
should now match the value in the switch expression.
So let's actually run this.
You can see we've got the output in the bottom down there.
So far so good,
but let's actually alter this slightly as follows.
I'm going to change the switch statement on line 13.
Instead of switch i multiplied by 10,
just to make that switch i.
Suddenly we've got five compiled errors,
basically one on each of the five case labels,
here as you can see on screen.
Why is that the case?
Required byte found int.
When our switch expression was i multiplied by 10,
the switch variable data type was an int.
And so our case labels were also int, and all was well.
Now what we've done is change the switch variable data type
to a byte where after will lead me through an array of byte
but our case labels are still an int,
because their values exceed the byte valid value range.
So can fix this if I actually change this code slightly now.
Basically adjusting the labels
to be values that are valid bytes.
So I'm just going to copy and paste the code.
Okay so you can see what I've done there now.
Changing those values,
and because the values are now byte valid
so they're actually in the range of a byte
they've no longer got any compilation errors.
Just to confirm we'll run this.
And the code works.
Right so let's go back to the code.
Specifically what I'm going to do
is make a change to the case 104.
If I change that and add a continue.
What does continue mean in a switch statement?
Well actually continue doesn't mean anything
in a switch statement.
It's associated with the encasing for loop,
that we're executing in.
The one starting on line 12.
So let's run the code again.
And you can see we've got a different output
down the bottom now.
So looking at the output, the values 100, 103, 104
they all actually reach the case statement 104
and because of the continue statement
initiated a new iteration of the surrounding loop,
skipping the code after the switch statement.
In other words, down here the system.out.println on line 25.
How would you break out of a loop from a switch statement?
You can't do it with a simple break, can you?
Because a break in a switch statement
is just a break in the switch label.
To break out of a loop from a switch conditional block
you'll need to create a label for your for loop,
which we'll cover in much more detail in the video on loops.
And that's coming up later in the course.
But you're probably already familiar with labels here.
What we'll do is we'll add a label here
and we'll answer the question we asked by changing our code
for the case label 104 and 107.
So what we're gonna do is add a label as I mentioned.
So we'll go back to the code.
We're going to add the label above for loop.
So up here.
Start, I'll just call for.
There's our label.
Now we're gonna change the condition
from continue for our case 104 to a break.
Moving on down for the condition for 107,
we've got a break there.
We're gonna change that to a break.
This time we're gonna use the start_for label
that we added on line 13.
Right, so we run the code now.
So the break in case label 104 prevented any of values
being assigned a value
because the value only occurs in case 107 and case 126.
And the break in case label 107 is the for loop break
and not just the switch statement break.
So we immediately break out of the for loop
without printing another statement.
Now if we added a case statement break
either before or after the break, to find in label 107
we would actually cause a compiler error
indicating one of the other breaks is unreachable.
So as you can see there's many variations
to a switch statement,
and many variables that can lead to compiler errors.
When you see a switch statement on an exam,
examine its location, is it participating in a loop?
Don't let the breaks and continue statements mislead you.
Examine the switch expression, can you determine the type?
Does the type match the case labels?
Is the type valid?
Doubles, floats, longs, booleans are all not valid.
Objects that are not strings or wrappers are also not valid,
remembering e nums are valid.
If the switch expression is a local variable,
has it been initialised?
If it hasn't,
does the switch statement have a default block?
If not, then the answer is compiler error.
Examine the case labels, are they all compiler constants?
Variables are not permitted.
If case blocks do not have breaks, the code falls through,
so multiple blocks could be executed.
And finally, remember that a default label
doesn't have to be the last one
or need to be included at all.
All right so let's end the video here.
In the next one we'll start looking, or look at a quiz
on decision constructs.
Then after the quiz we'll get into loops.
See you in the next video.