Before we start, did you try solving the workbook yourself?
If not, please click the link in the resources folder and follow the instructions for this workbook.
This video will present the solution to workbook 3.6.
Please make sure that you launch the exact folder that contains your java file, otherwise you will
not be able to visualize the runtime with me.
All right, let's do this.
Zooming in a little bit.
So your friend wants to hang out on a Friday and we need to check that day against seven days from your
calendar.
The calendar, I hope you guessed it is just going to be a switch statement.
So here's a trick for you.
You can actually autocomplete a switch statement.
So write switch and click on this option where it says switch statement.
And here we're going to be comparing the day that your friend wants to hang out against a list of seven
cases.
So case Monday.
If the day key matches a value of Monday, then we're going to print.
System don't print line.
Sorry.
I have to stay at work late.
And you'll notice that it automatically adds a break keyword at the end of our case, which is perfect
because in the event of a case match, we're not going to want to execute the remaining cases.
We want the entire switch statement to break.
Let's copy and paste this.
One, two, three, four, five, six more times.
This is going to be Tuesday, in which case we're going to print the following.
It looks like I have meetings all day.
Wednesday.
Feel free to pause the video and do the rest on your own and resume once you are done.
All right.
You probably got something like this.
You'll notice that I removed the brake keyword that was automatically generated for the default case.
And that's because it's logically unnecessary to have a brake keyword here.
Let's just say they were to say, I don't know, laundry day here or something.
That's not going to end up matching any of the cases eventually reaching the default case, which is
the last possible case that will run.
So putting brake here to break the switch statement after won't really do anything right.
You can keep it, you can remove it.
It doesn't really matter.
But for tutorial purposes to prove to you that it's not necessary to have it, I'm going to remove it.
All right.
And now let's visualize the runtime.
So just like you would visualize a series of if, if, and when you have a series of cases, you can
just put a breakpoint right at the very beginning and use the familiar step over button.
And now we're going to add breakpoints here as well.
Let's start with the first test case, which was Friday.
If your friend wants to hang out on a Friday.
We'll continue to the next breakpoint day equals Friday, and now it's going to print.
Are you free on that day?
We'll continue to the next breakpoint.
Do not press continue here because continue will try to continue to the next breakpoint.
But there is no other breakpoint.
So it's not going to allow us to visualize the runtime press step over so that we can step over to the
matching case, which is Friday.
I am free.
And so upon pressing continue it prints I am free.
Test case number two is to put Saturday.
We shouldn't really see anything different.
Again here, press step over, not continue, because there are no other break points to continue to.
So we'll step over to the matching case, which is Saturday.
And upon executing this line of code, it's going to print I am free and break is going to break the
switch statement.
Beautiful.
Now, what if we put Monday?
Debugging the runtime.
Same thing.
The first matching case is Monday printing.
Sorry, I have to stay at work late and then breaking the switch statement.
If you forget to put the break word, what would happen?
So let's visualize the runtime.
Clearly the matching case is Monday upon a case match.
If you don't put brake, it's just going to run every other case that follows.
That is not good.
But now since we have brake over here, then it's going to break the switch statement.
So your friend wanted to hang out on a monday, but we printed out answers for Monday and Tuesday.
If you were to remove this brake keyword as well.
Let's continue.
Continue stepping over here.
We have a case match.
So the switch statement is going to run every other case that follows if we don't have a break keyword.
It runs the case for Tuesday.
And now it's also going to run the case for Wednesday.
But now we have the brake keyword, so it's going to ultimately break.
All right.
What if we had Friday?
And we were to remove the breaking word for Friday, Saturday and Sunday.
What would happen then?
Let's visualize the runtime.
The matching case is going to be Friday.
And after there is a case match, it's going to run every other case that follows, which is exactly
what we don't want.
It's going to print.
I'm free for the case Friday.
It's going to print.
I'm free for the case of Saturday.
It's going to print.
I'm free for the case of Sunday.
And it's also going to run the default case.
That's not a date.
So I hope you realize now how important it is to have the break keyword after each case.
Now you're able to visualize the runtime in real time and understand exactly what everything is doing.
So now if I put Friday restarting.
Now, this time the matching case is Friday.
So it's going to execute the code for this case.
And switch is designed to run every other case that follows.
But because we have the break keyword, it's going to break out of the switch statement.
This is working exactly as we want it to.
Again, the break keyword is really, really important to include.
After every case, you don't have to put it for a default because it would be the last thing that runs
anyway.
All right, let's execute the default case.
What if we put a laundry day?
Let's visualize the runtime.
All right.
Do not press continue because there are no other breakpoints that continue to let us step over to the
matching case.
There were no matching cases.
So the default case runs printing.
That's not a day Now we can press continue because we are done.
All right.
I really hope you enjoyed this breakpoint session, and I hope visualizing the runtime in real time
was able to shed some light on what the break keyword is actually doing.