Check out a free preview of the full Java Fundamentals course
The "Switch Statements" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:
Angie discusses using switch statements instead of if-else-if to check conditionals. Unlike if-else-if statements, switch statements check for equality.
Transcript from the "Switch Statements" Lesson
[00:00:00]
>> Switch statements are another decision structure. They basically solve the problem in the same way that the if-else-if does, so it's multiple branches, multiple possibilities, more than two, right? Yeah, so good when there's more than two possible paths. The difference between the if-else-if in the switch is that the switch checks for equality on a condition.
[00:00:26]
Whereas, for example, in the program we just wrote, we said if the score is less than this, then it's this, there's no less than or anything like that. With Switch, it's straight up equality. Does this equal this? Okay, so let's go ahead and write another version of that grade program.
[00:00:51]
We'll have them enter the letter grade this time versus the score and then we're gonna use a switch statement to print out a message basically. Like letting them know what how they did it, okay so back to our IDE. And let's go ahead and call this one great message and this will be in the decision structure package, so GradeMessage, All right?
[00:01:23]
I'm making my main method, and I'm going to ask them for a letter grade. We'll read that in with our scanner. Import scanner. System that in and the letter grade is what? A character, right? I'm gonna read it in. Let me read it in as a char. I don't know if scanner has a thing let me see.
[00:02:10]
grade scanner.next. Okay, it doesn't have one that reads char so we'll make it a string and All right, great. So in scanner.next, we'll read in a string. All right, so we have our grade, let's go ahead and close the scanner. And I want to essentially initialize a message based on the grade.
[00:02:50]
So I'll say string message. We won't give it a value for now, and we're going to use the switch decision structure to determine how do we initialize the message, right. So to use switch, we write the word switch. Inside of a set of parentheses, you're going to put the variable that you want it to evaluate.
[00:03:15]
So that would be grade, right? So look at grade and then, remember, it's going to check for equality. So within the switch statement, we write what's called cases. So we can say case, and then you give it a possible value for grade. So if the case is that grade is equal to A, then you put a semicolon and any statements that you will want executed if this is the case.
[00:03:50]
So I'm gonna say I want the message to be excellent job.. Okay, your IDE would have automatically indented that message statement to let you know that that's a part of the case. And then once I'm done with the case, I can write as many statements as I want here, but when I'm done with that case, I need to write break to let the compiler know, I'm done.
[00:04:24]
All right and then you can follow this with however many cases you want so our next case would be B. And if they gotta B we'll say good job and then we'll break. Okay, I want to show you, we'll write the rest but I wanna show you what happens if you don't write the break.
[00:04:50]
What's going to happen is what's called fall through. So in switch cases, if I omit this break statement, if A happens to be the case, grade equals A, it'll come here, it'll say, okay, let me assign excellent job to message, but there's no break. The programmer must want me to keep going.
[00:05:16]
So it won't even evaluate the next case, it's just gonna go right in there. And it'll override message to good job. And that will be what happens. So you'll end up in the wrong case. All right, it will do what you wanted it to do but it will keep going untill it hits the break statement, so, that's called fall through.
[00:05:38]
Some times you want fall through to happen most times you don't, all right. So,that's case B, case C we'll say message =, what's the message for C?
>> Keep trying. [LAUGH]
>> [LAUGH] Let's say good job, and we'll say great job here. All right C is, I guess, doable, right?
[00:06:04]
D is when you get yourself in trouble. Okay, case D.
>> You can do better.
>> You can [LAUGH] message=you need to work a bit harder.
>> You have any examples or follow through might be useful?
>> Yeah. Yeah, good question. Let's say that you want to do something special if they match the first case, right?
[00:06:39]
You want to do something different if they match the second case, but for the first case you will want the code that's special to them, but also the code that's applicable to the second case. So you want two things written, but you don't want to duplicate the code that's common between those two cases.
[00:06:59]
So you can set it up where you say, all right, if you match this case, do this, but fall through to the next one so you can do the rest of it too. And if you just match the second case, you wouldn't get the first statements.
>> So its kind of like a almost like a flowchart where you're trying to check off boxes and essentially hit a condition where it's no longer true?
[00:07:20]
>> And then follow you but the no longer true part.
>> Well, you know, case A, B and C, they meet the case for case A and I made the case for case B. Those are both true so you do this, they don't meet the case for case C, and so you wanna stop there and you don't wanna move along further in the process.
[00:07:37]
>> Right, but there's also some differences if they only meet B. So for A, I will want A and B. For B I just want B.
>> Yeah.
>> Okay. All right. So if they got an F, we'll say, and then we'll break. Okay, so that's all of the possible cases but we know that human beings don't care about possible cases, right?
[00:08:10]
So someone might enter something like a Z, for example, right? And it might not even be that they're doing this out of error, but maybe in their culture, their country, or whatever, the letters are different, right? So we don't support that. So we just need to take care of those cases in switch you can add a case that's called default.
[00:08:39]
The default case means, if it doesn't match any of these other cases, just do this. So, here we'll just supply a little error message, we'll say, Error _Invalid grade. And we'll break this as well. Okay, so after our switch statement, we'll go ahead and print out the message.
[00:09:10]
Does this make sense? Okay, questions on switch, let's.
>> But if you wanted to force a good response, like you don't just want to like print out an error message. You wanna force them to keep going until.
>> Yeah, so we can handle that with a loop and we'll actually code up a case like that when we talk about the loops.
[00:09:33]
>> Do you always need to end with a default case?
>> You don't have to add a default case. So what will happen, let's see what will happen. All right. Okay, so, I don't have a default case here, right? And then I'm trying to use message later. So it's gonna say message might not have been initialized because I could have a possibility where it's not.
[00:10:03]
If I just did not want the default case, I could go back here and I can initialize this to maybe a blank string to start off with. And now I don't have a default and I can print this. So what'll happen is, I think nothing's gonna happen. Let's go ahead and try to run it just to prove that out.
[00:10:22]
So let's say enter your grade and we're going to say a Z, right? In that case, nothing happens, it just ends, okay? So you don't have to, but it's good practice just to make sure you have something going on. All right, great question. Any others? All right, let's run it again with our good cases.
[00:10:55]
Enter your letter grade, B, great job, okay. Let's show the, fall through. So I'm going to omit the break and I should get excellent when I say A but I'm gonna get grade. So let's run it again. My grade is an A and notice I got great because it overrode it.
[00:11:22]
>> I think you might have just talked about this. I was kind of distracted, but it says variable grade might not be initialized. Is that what you were just talking about?
>> Right, so if you did not have the default case, there's a possibility that none of these cases matched message.
[00:11:37]
And so when you try to use message it says we're not sure message has a value, right? So what you could do is if you didn't want the default you could just initialize this to A blank string or whatever you wanted to. And then once you do that, the error goes away because it has some value there.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops