Hello, in this Java video, we are going to take a look at the switch statement. The switch statement is still part of the conditional statement, the decision making aspect of programming. So we've looked at if statements the elsif branch and the else branch for this entire sort of if format, and the switch them, probably as a slightly simpler version of what we're doing here with the switch statement is great. If all you want to do is check if one value for like I, for example, is equal to something and if not, is equal to something else. If not, if something else and maybe have a fallback like this, it doesn't allow you to do advance checks like this where you check if it's, you know, greater than a value or not equal to a value. It doesn't allow you to do checks, like, No, we don't like to do multiple checks as well combine that sort of stuff.
So it isn't as complex but for simple stuff. It's great. So let me just show you the format. So if I were to do Shaw put a student say, I say, Yeah, I do in tax credit and you new value there we go. And that's one way you could switch. open close bracket in here is the value that you want to check.
So what is like we're going to check if x equals on fee so just put the value don't put any condition here, curly braces. And what's this? Azzam initializer list initialized to one there should disappear and the format here is you put the keyword case. Now you put the condition itself, and you don't put the condition as in like w equals or anything you just put what you want to check if x equals one, one safe actually zero. Do something Put a colon. Then on a new line, I put the keyword break.
I'll explain the keyword break in a moment. But for now, just take you for granted from the System dot out dot print ln. And I'm going to just number one, number zero. And if you want to check if n equals one, it's simple. You just add another case, put the number one is button one. So I think it's pretty cool if all you want to check is if a certain value is equal to something.
If not, is it something else? So just simple, simple check. So what are they up to like? Five, so they'll be free for five. Now, let's just fix all of this. Fine.
Okay. So now let's run it, see what we get. And I'm actually going to comment out this code. So run it. So he says number one could actually once or twice a four runner. It does, it goes to the checks, and he finds this one.
So case four, and it prints out number four, what are the different part nine, which isn't a valid case, by running it print out nothing. So we can do something similar to what we did here where you have a fallback if all else fails, and at the end, you put the keyword default. So do colon, and one and in here, you'd simply put what you want to have in the system. I've got an honest, not valid. So for running again, save, not valid. And if I just message in zero just from that is still working, it says number zero reason we don't have the break statement here is because if you write at the end, so what is the break statement?
So when we did loops or when sorry, when you get onto loops, you'll see that the break statement essentially allows you to, you know, get out of the loop. And it's sort of similar in a switch statement. So as soon as it hits the break statement, it is going to, you know, exit the, you know, the switch statement, but if let's say we don't put this break here, and the case is zero. What happens is it goes to case 00 prints out this line, any time Just go to the next case without doing a check and does whatever code that is. And if I were to, let's say, I'll actually undo this, and I'll just comment this out. So what you'll see is it prints out number zero, number one, number two, and number three, because the no breaks, he just trickled down.
So this is really good. If you have a bunch of potential different comparisons you want to do, but what you actually end up doing with the code is the same. This is fantastic for that. So the equivalent in terms of if statements, this will be something along the lines of if x is equal to zero, or x is equal to one or equal to two or so that is the equivalent of sort of what we've got here. Obviously print out all of these So maybe you most likely just get rid of these. And maybe just have the one piece of code that code here.
And it doesn't matter where we get into case 102 or three, and let's print this line out that well, let me just undo this again, actually, I'll just do Command D. Okay, and one last thing I want to cover is, you know, up till now I've just been using integers in the if statement, and the switch case statement, you can use other you know, their date type. You can read too much any data type in the if statement, a few other data types and switch as well like char, but that's not the case for all languages, like gangway certain languages, you can only use integers in there. So just bear that in mind if you are coming from another programming language. So that's it for the switch statement. If you have any questions, feel free to reach out. There will be a link with the video to the GitHub page.
Contains all the source code from this tutorial and every other part in the series and I look forward to seeing you in the next video.