Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
You will end up writing if statements in pretty much every program you ever write, but sometimes there's a specific situation where it's not the only option for checking a condition. Let's say you're checking a variable for a selection of very specific values. Here is what I mean by that. We create a variable in this case called grade, and somewhere in the program I want to check is it Regular or is it Premium or is it Diesel? I've got a few very specific values I'm asking for. We can do this as a series of separate self-contained if statements like I have here or we could even start using the else condition to actually start combining them altogether, but this can get a little clunky, particularly if you don't just have three or four but you have 10 or 12.
So all I need to do is put grade inside the parentheses. It's not grade is greater than something, grade is less than something. We're just saying we're looking at the variable called grade. Then what we do is inside the code block inside the body of the switch statement, we describe the different situations, the different cases that we're looking for, and the way that I do it is this. I use the word case and we describe the first option. And in the case, the grade is equal to Regular.
I don't use the equals sign or the double equals sign because we already know what variable I'm looking at. I'm looking at grade. So case "Regular" and then I use a colon, and I'm going to describe what I'm going to do. Let me just bake this out a little more and that will start to make sense and fall into place here. Now, because I'm checking a variable that contains a string, I am using the double-quotes when I am checking these string literals. If I was checking a variable that was a number, I could just say case 1, case 2, case 3 without putting them in double-quotes.
Now, this is the first time we've actually used the colon here. It looks a little unusual, because we're used to the semicolon ending a line. That's not what we're doing here. We're actually saying in the case that it's Regular, we're going to do something. I just haven't described what that is yet. Well, this is how I start to fill it out. I'm going to put in the alert statement that I had a moment ago. Now, as I start to do this, hopefully it becomes apparent that this is a very readable format. It's very easy to scan this and figure out what we're actually looking at and what we're going to do.
One of the benefits of using the switch statement is we can actually finish off this. We can also describe what happens if it isn't any of these. And instead of using the word case, I can finish this off with just the word default. This is not in double-quotes. We're not checking that variable for the value default. We're just saying in any other situation I'm going to do something else. In this case, alert("That's not a valid grade"). now, there is one more thing I have to add to this for this to work properly.
You see when we come down into the switch statement, let's imagine we're going through this line by line. We create a variable called grade, set it equal to Premium. We come into the switch statement, and it asks, what are we looking at? We're looking at the variable grade. Is it Regular? No, it isn't. So we jump down to the next case. Is it Premium? Yes, in this case, it is. We're going to run the alert, but because of something that exists in switch statement called fall-through, we will immediately then just jump down and we'll find the next statement we can execute, which is this alert statement, and then we'll do this alert statement.
That's not what we want. We need to prevent fall-through in our cases here. The way that we do that is basically to add the word break. We can think of it as saying, now we're done with this case. So right at the end before the next one, we say break. And I'm going to put in three of those here. What break is going to do is jump us out of this switch statement. It will actually jump us right to the end of the closing curly brace and continue on executing any code that may come afterwards.
Technically, we could put a break keyword in after the default one. We don't really need to because this one is actually just going to jump out anyway because we're right at the end of the switch. So I'm going to save this and we're going to run it and what should happen is we'll start this code, we'll create a variable called grade and set it equal to Premium. We'll come into the switch, we'll check, is it Regular? No, it isn't. Is it Premium? Yes, it is. We should pop up the alert saying it's $3.35. We should then break and then jump outside of the switch statement.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 104677 Viewers
56 Video lessons · 116589 Viewers
71 Video lessons · 85767 Viewers
131 Video lessons · 41042 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.