So carrying on with conditional statements, we're going to look at the switch case statement now as
to which case does the same functionality really as if all else.
But it's a little bit cleaner to read and a little bit cleaner to write.
If I just jump dump in something, it's going to take me ages to type, which is much better if I just
dump it in.
We have, first of all, some character, which in this case is the letter Z.
The switch case statement starts with a switch statement where we say switch some character.
So this is looking at the variable up here and it's saying in the case that that variable is a then
I want you to do whatever the following lines of code are in the case that it said, do this following
line of code in the case that it is B or why that's not.
And that is or.
Then it's going to be second last letter of the alphabet.
It also should be second letter of the alphabet.
Let's change that second, first or last letter of the alphabet, and then just like the if else, the
P&L statement covers all eventualities.
In this case, the default covers all the remaining eventualities, print some other character.
So if we run that, we should get last letter of the alphabet.
If I drop that to a B.
We should get a MacBook.
You got this second, last, second, first or last letter of the alphabet.
OK, so that's the switch case in a nutshell.
Now, the switch case has many features that we can implement.
And I'm just going to copy over this code here, because, again, it's long and convoluted.
So we have an approximate count of 62.
We have counted things, moons orbiting Saturn and we have a natural count.
Now, these three variables are going to combine with the switch case can statement to provide a human
readable output at the end, which is this.
And the way we're going to go about that is to look at the approximate count.
And say if there are zero in that approximate count, then the natural count is equal to no, as in
we're turning a number into an approximation or a natural approximation that humans understand.
So you wouldn't say why?
You would say there is zero moons orbiting whatever planet Pluto, but it's more likely you would say
there are no moons orbiting Pluto.
In the case of one or less than five, so in that range, one, two, three, four, we're going to
have a few.
In the case of five to 12, we're going to have several.
Twelve to one hundred, dozens of one hundred thousand.
Hundreds of and of course, anything else is just going to be many or an s load, if you like.
So once we run through this, 62 will come out to dozens of.
And it will say there are dozens of.
Counted things, which is moons orbiting Saturn, so we hit play on that.
There are dozens of moons orbiting Saturn, so this is an example of a more expansive switch case statement
where we're actually taking ranges into account.
Now, there are many other things we can do, switch cases, we can assign variables and things like
that.
But we're not going to do that here because you don't actually need it that often.
In my experience, it comes a time if you do need it.
OK, do I have homework?
Oh, yes.
Here it is.
Create a switch statement to call someone young, middle aged or old, so we did this in the previous
lesson with the if else, but now you can do it with a switch case.