Hello there, and welcome back to this Java development course. So, last time we went over else if statements in Java, and you had some homework to do, your job was to create a program which will take an integer from the console, in this case an age and print whether that person will be able to vote or not. So here is essentially what you should have done, secure and get into So first of all, you know, initially my scanner pass system.in as where I'm going to get the value from then I say integer age equals SC dot next answer just get the next value from the console. Then if age is more than or equal to 18, we print you can vote if it's not more than or equal to 18 we print you cannot vote and I also added here just for for the last I headed you will be able to vote in and then 18 minus age years so just tell the tell the person how long until they can vote It wasn't really required.
But you know, I just added it just, you know, for demonstration. Okay, so anyway, let's go ahead and run this application. Let's insert 12. And there we go, you cannot vote you will be able to vote in six years. But if we do 18 It's a you It says you can't vote. So if you're 18, if age is more than or equal to 18, which it is, we print, you can vote and then just ignores this.
So there we go. So that is essential. We should have done not very complex, pretty, pretty simple. Soa, yeah. Anyway, without further ado, let's get into today's lesson. Alright, so today, we're gonna be going over something, you know, that's actually kind of related to if statements, and l stiff statements, you know, all together just the whole thing, decision making in Java.
So that's actually what we're going to be going over today. More specifically, this is going to be called the switch statement. So let's go ahead and actually create a new class. This is going to be called switch. As always main method and finish. There we go.
Okay, so how does the switch statement work? So together we're going to write a program. scanner r equals new action. Oh, it's gonna be SC equals new scanner. system.in whoops, nope, not a string. system.in There we go.
All right. Let's import it. Yeah, there we go. Okay, so now we're gonna do int a equals SC dot next int. And so we're gonna write a very simple program. So we're going to do switch a, and there we go.
This is all be explained a second here. Case. One. System dot out dot print ln A is equal To one there we go. case two, System dot out dot print ln A is equal to two. Okay, and so we're just gonna do this exact same thing for all values up to 10.
Three, four, whoops. 5678 and I know this really isn't the most efficient way to do things. And of course, there are more, much better way of doing it we could just do system out print ln plus, then the value whatever value we actually went ahead and actually, you know, whatever value we inputted, so we can do system out println A is equal to plus a But in this case, we're just showing off the switch statement how it's used. So that way we'll just we'll just just take an idea of exactly what's going on here. So here change these values as well. 789 and 10 Okay, so there we go.
So what is this program going to do? Well, what's gonna happen is the switch statement always take some numbers input. So in this case, it's taking a is input, it can be any number and I believe even a string so recently in the eighth, I think version of Java strings in switch statements were allowed now. So is it the eighth or the seventh? I can't I can't remember for sure. But before you couldn't actually insert strings into switch statements, but now you can so you can answer any of the primitive values as well as the as well as the any of the primitive values with strings as well.
So you can also insert strings into switch statements. So essentially, here we have the entire you know, idea have sort of switch statements themselves. So okay, so we insert a, in this case it's a integer, which is two, let's go check case one, it's going to print this dot print line a is equal to one, if A is two, which it is, then it's going to print a is equal to two. So let's go ahead and I just realized I misspelled equal, but let's just keep it like this for now. So let's gonna run the application. Alright, so now we can insert four and there we go.
So as you can see, it's pretty it's behaving a little bit oddly though. So the whole idea of a switch statements pretty simple right? So if it is for which in this case it is, so case for it will just print this, but the problem is, how would we do if else so for that we have it so that if it actually prints case for it, we'll go ahead and print not only that is equal to four, but everything under it as well. So just to give you an idea of exactly what's happening here, so if I can do if I do one, it's going to print all of it so it doesn't stop. So just because we have something in a different case, doesn't necessarily mean that it's gonna not execute it. So if anything is under it, it's going to execute as well.
Of course, we can stop this though. So there's this handy keyword called the break keyword will which will just break so now if we do one, we just do a is equal to one and then a breaks and it exits the switch statement and goes to this line 35 and start executing from there. And since there's nothing there, it just ends the program. So through this break keyword, it allows us to perform or operations in switch statements. So let's say that if we wanted to print h if a was equal to four or five, so in this case, all we would do, so if a is equal to four. We print H, and then break.
Okay, there we go. So now if you go And do four, it'll print age. And if we do five, it'll also print age. So this allows us to, you know, create or statements inside switch statements. So that's where the break keyword comes in handy. So let me go and actually add that to all of these.
Let's go ahead and remove this back. So there's going to be a break, break, break, break. Break, break, break, break, break, break. All right, there we go. Okay, so, so now it just works like a, like an else if statement. So one, a is equal to one, five, a is equal to five and so on and so on.
Now, what if we wanted to do else? So if none of the values are true? Well, for that, we would just have the keyword default system dot out. Whoops, nope, not nano time system dot out dot print ln. A is more than 10. There we go.
All right. And so now if we do 11, a is more than 10. So this default will get executed if all of these are false, essentially. So the switch statement can actually be very confusing personally, when I was learning about it, it took me quite a bit of time before I actually understood where it can be useful and what even does. And generally speaking, if statements are more used that switch statements simply because they're just a little bit more robust, but nevertheless, switch is actually it's a pretty useful operation. Well, yeah, I guess operation in Java.
For working with values where you have to check a lot of different values, compare it with a lot of other different values. So they don't really really we really have switch is just to improve readability. So instead of doing if A is equal to one And then else if A is equal to two else if A is equal to three else, if A is equal to four, we just have this switch statement where we check case one, case two, case three, and so on and so on. So it just, it's just generally a lot easier to work with, and just looks better than doing a bunch of elsif statements and takes up less code. That's also you know, that was pretty good. Okay, so that is the switch statement.
Really nothing too difficult. Without further ado, let's find some work. All right. So for homework, we're going to be pretty simple. We're going to go ahead and first of all, create our switch homework class switch homework. There we go.
Alright, so what we're going to do is, first of all, we're gonna need our scanner class. And we're going to have four options. So we're going to do System dot out dot print ln. What is the correct way of defining a variable In Java, all right, there we go. And so what we're going to do now is we're going to have small print ln 234, we're going to have 123 and four. There we go.
One. int a equals to A to A equals 231. A equals two, and four, speak for something crazy. In two equals a. Let's see like that. All right, there we go.
All right, and so now we're going to get the next integer is going to be from one to four. And what you're gonna do is you're going to tell the person Whether or not they got it, right, so if they got it right, you're gonna say you got it right. If they got it wrong, you're gonna say you got it wrong. And if they are not in the, you know, they're not in from one to four, so if the answer is zero or five and so on, they're gonna say that isn't an option. So you're gonna do this without using the if statement. Obviously, just use the switch statement.
No if statements allowed, okay, so it's pretty simple, not too difficult. If you can understand the switch statement, you can get this. So yeah, I believe in you, and I'll see you next time.