Else If Else

Learn the Basic Java Concepts Loops, arrays and methods
15 minutes
Share the link to this page
Copied
  Completed

Transcript

Hello there, and welcome back to this Java development course. So, last time we went over the if statement in Java, and you had some work to do, your job was to get an integer a from the console and print whether a was positive, negative or equal to zero, essentially what should have done. So here I have my class in Java homework, my main method scanner system.in, as to where we're going to get it from the console, then I get the next integer from scanner. So it's going to be an integer a, if A is more than zero, a is positive phase less than zero, then we print as negative. And if a is equal to zero, then we print a is equal to zero. So like this, we could use if statements and if statements are actually pretty useful.

So now let's go and actually run this. Whoops, apparently it's not really the right thing. That is odd. Most likely she's not running the right huh? That's, that's weird. Let me actually take a look at exactly what's happening in a car in a second is, Yo, I solved it.

I don't really know exactly what's happening, I just restarted Eclipse. Sometimes you get those kind of bugs where it can't really find something. Just try sometimes restarting Eclipse and a lot of times that solves the problem. If not, then then maybe you have a more specific problem they need to look up but in my case, I just had to restart Eclipse. Okay, so let's go and run this. Alright, there we go.

Okay, so let's go to now let's just say one is positive. And if we do negative one is negative and if we do zero is equal to zero. So there we go. So now together, we were able to create an application Which will tell us whether a number that we enter is positive or negative. All right, so there we go. That's how we use if statements.

And they're actually pretty useful. If again, I know I'm saying this for like the millionth time or so. But if statements are very useful, they are one of the basis, one of the very fundamental concepts of programming, so it's important to understand them. That said, this code does seem a little bit redundant. So here, what we're doing is we're checking the if statement three times. So we're saying if A is more than zero, then we're going to print a is positive.

So let's just for the case of example, let's just say that we entered one, so a is more than zero, and so it printed as positive. So what does this mean? Well, that means that a cannot be less than zero and it cannot be equal to zero since it's more than zero, right? But since we are due what we are doing here is we're doing if A is more than zero, we're then doing if less than zero. And so in result, we're still going to be checking it even though a is more than zero. And we know that it's still going to check phase less than zero.

And then it's also going to check phase equal to zero. And so in result, this just wastes time. So the program has to check, oh, look at more than zero will print days positive, then it checks. Oh, look, a is less than zero, it prints eight isn't less than zero. It's not it's more than zero. And so it doesn't do anything, and then checks.

Oh, look, a is not equal to zero either. So let's give this as well, and it spends time checking these two so it spends time you know, making sure that you know, executing or at least trying to execute these if statements. So in result, what we can actually do is we have something called the else if statement. Let's actually take a look at that. Now. We're going to go ahead and do new new class.

Else. If in Java, actually just do elsif and public static. We're actually First of all, we're going to take a look at the just the L statement without the if but for now, just okay. So if a equals, let's actually create the scanner, let's do the same exact thing. So scanner, SC equals new scanner. And remember, we have to assign where we're going to get our scanner value from.

In this case, we're going to get it from console or system.in. There we go import scanner. And now we're going to do int a equals SC dot next int. OK, so what is our objective? Well, our objective is to not check whether or not a is more a is less than zero, or equal to zero. If A is more than zero, that's actually what we're going to do.

And so, to do that, we'll just going to do if A is more than zero. We're going to print as positive, right? And then we're going to have if a is less than zero. We're gonna do system dot out. Print ln. a is negative.

There we go. Okay, so now we have these two statements, but again, it's going to check both of them. So even if a is more than zero, it's still going to go ahead and check if a is less than zero, even though it obviously isn't, since it's more than zero. Well, for that we actually have this other statement out and we don't need this anymore. Okay, so what is the else statement to do? So the L statement essentially just makes sure that the it if this returns false, so a is more than zero, it'll automatically execute this L statement.

So let's take a look at this. There we go. So we can now do one, and oh, print a is positive. And if we redo it, we can now do negative one, and it'll print as negative. So what is happening here? Well, when we insert negative one, it goes out and does if A is more than zero, in this case, a is negative one.

So is negative one more than zero? It's not. So this is going to be equal to false. There we go. So that will be equal to false. And then we will, so then since it is false, it'll just execute this else statement.

So if this is false, This will automatically get executed. Okay. Okay, let's actually take a take a look at a little bit of another example perhaps this will be a little bit easier to understand. Let's comment this out. Okay, so if false else. So, in this example, this else statement will always get executed no matter what.

There we go. As you can see a else statement is executing. let's actually go to actually do go here if statement is executing as well. There we go. So let's go and run this right now. elsif.

Let's insert some value and as you can see else statement is executing. So since this is false, This will automatically run this else statement. So how else could we possibly use this? Well, we can do it like this. It's going to remove this. We can do if A is more than zero or a is less than zero, a is not zero.

Or actually would actually do just to make a code a little bit better to read eight, if A is not equal to zero, a is not zero, else a is zero. There we go. So now let's run this. Five a is not 00 a zero. All right, so what's happening? So when we insert a zero into here, we check is a not equal to zero?

That's, that will return false since a is equal to zero. So it will return false and so it won't execute this ad. In these square brackets, then we check else. We don't check anything we just say else. So if this is false, then we print. Actually, no, we just execute whatever is between these brackets.

So in this case, we print a zero. And that's why we get this a zero here. So that is how we use the else statement in Java. Now, what if we still want to check? Something? So in this case, if A is not equal to zero, let's say that we want to check if A is not equal to one as well.

So A is not zero. So if a is not equal to zero, a is not zero. But how do we check if A is not one so we want to do it so that if we enter zero will print a zero? If we insert one, it'll print as one and if we add or anything else, it'll just say a is not zero. So how do we check that? Well, to do that we have something called the else statement.

So how do we do this? First of all, let's go and check if A is equal to zero. We're going to restructure our code a little bit. So A is zero, okay? Then we're going to do else if A is equal to one. And for some reason it put it into here there we go.

A is equal to one System dot out dot print ln A is one, else a is not zero or one. All right, there we go. Okay, so what will happen here, so first, it'll check if A is equal to zero. If it is equal to zero, then it'll just stop executing this entire thing and jump right here to line 19 and start executing from there. But if it is Isn't equal to zero, it'll check this else. If so it'll do is a equal to one.

In this case, well, no, I mean, I mean not in this case, let's just say that right now, our number is two. So in this case, it'll turn false. And then in result, it'll just jump to this else statement and execute system a purlin. A is not zero, or one. So there we go. And we can have as many else if statements as we want.

So we can do else if A is not equal to one, two as well. So if a is equal to two, there we go. And if for some reason always puts it right here. So if a is equal to two, System dot out dot print ln A is equal to two. There we go. So A is not equal to 01 or two now as well.

Okay, so there we go. All right, so this application, it's checking if A is equal to zero, it'll print zero if A is equal to one, it'll print one. And if a is equal to two, it'll print to it basically to neither of those, it'll print a is not zero, or one or two. Okay, so the important thing to understand here with LC, and else if statements is that we use them in order to not check something that we already know. It's false. So let's just say that A is equal to zero.

What's the point of checking a z equal to one, a is equal to two, when we already know that it's equal to zero, there is no point. So in result, we have the the else if statement, which will only execute if this is false. So this is if this is true, then it won't even it'll just ignore all of these and we'll just jump right on to the rest of the code starting from line 21. understandable, okay. Okay, so another important thing to note there also went over is that we can have unlimited else if statements. So if we wanted, we could have hundred or 1000 of them. So how many however many we want.

But we can only have one else statement. And we don't necessarily require an L statement to be happy, so we can just remove it altogether if we don't want it. Okay, and also one last thing, you can't remove the if statement though, so you can remove the L statement, but you can't just do elsif it'll throw you an error. Okay, one last thing we need to go over with, if statements is that inner if statement. So we have or actually no nested if statement. So if we can do if A is equal to zero, we can then do if A is equal to one if A is equal to two, if A is equal to two, so technically, this will all So sort of solve it.

Let me go and do it right now. System dot out dot print ln A is equal to zero, but in this case as well we are checking we are in fact checking. So A is equal to zero what's the point of checking a is equal to one. So this is just to demonstrate that we can't have if statements inside if statements so there's no rule that we can only have if statements on on on on plain method text so we can have an inside if statements as well. That is, that is no problem. Okay, so that is essentially how we work with if statements as well as else if statements.

Now, let's assign you some homework. Alright, so homework today will be very simple. All it's going to be is you're going to take in an integer from the console, this is going to be a Age of a person. And so you're gonna print whether the person is able to vote or is not able to vote. So if it's over 18, than or equal to 18, then he is able to vote. If it's less than 18, then he or she is not able to vote.

So yeah, so very simple homework maximum, like I think it's gonna take like 10 lines of code or so. So yeah, pretty simple. Use it use if statements of course if else statements, of course. So don't just put two if statements that would be that would be kind of, you know, ignoring the whole thing we did today. So yeah, anyway, I wish look at the homework. It's pretty simple.

So I think you'll get it and I'll see you next time soon.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.