Hello there, and welcome back to the Java development course. So, last time we went over getting random and solving problems with Google, and you have some homework to do. So your job was to look up on Google how we can print a random character in Java. So this is essentially what you could have done. There are some couple other ways to do it. So as long as you get the correct result, I don't really care how exactly you did it.
But this is just one of the ways to do it the easiest way I would say, so I just have a string characters, we can check which contains all the characters I want to print. And then I have a random integer, which is math dot random times 62, since that's all the characters, and then I just have system out println chars dot char and then random so it'll always give me a random character. We get to test it out. There we go. So yeah, so it just gives me a random character every single time. So if you got that very good, if not, if you did something else, and it still worked, that's also okay.
But yeah, so generally speaking, if you looked it up on Google, spend a little a couple of minutes. So you No researching it, it should, it should be no problem. So without further ado, then let's get into today's lesson. Alright, so today we're going to be going over conditional statements. So decision making in Java. And at this point, it can start to get a little bit confusing.
So we're going to be going over it together, we're going to take things very slowly, we're going to take our time do some exercises with them, you know, I do a lot exercise with them. And hopefully you can get this pretty simply. So we're going to create a new class. This is going to be if in Java, see if in Java as always public static void Main. And by the way, this is last time I'm going to be creating a class on camera. So later on, I'm going to have a a class already created when I start recording.
So yeah, let's go on and finish. And there we go. So we have in Java, let's go and delete the comment. Alright, so what is an if statement? Well, an if statement is a statement, obviously. And it essentially can do something if a Boolean is true.
So let's say we have a Boolean, okay? Actually, it's true to scanner First of all, scanner SC equals new scanner system.in. There we go. Then we're going to do int a equals SC dot next int. Actually, it automatically inputted scanner there. Okay?
There we can do int b equals sc. Actually, no, let's keep it at a okay. So what do we do need to do now? Well, we want to check if A is more than five. If it's more than five, we're going to print a is more than five. And if it's less than five, we're going to print a is less than five.
So let's do that. So we're going to do a Boolean a Is is more than five. And that Boolean is going to be equal to A is more than five. There we go. Okay. So now how do we do this?
How do we print a is more than five if it is more than five? Well, we just do if A is more than five. And then we do system dot out. Print ln. A is more than five. All right, there we go and close off our statement.
And there we go. Alright, so let's run this. Let's input a number, let's say four. And there we go. As you can see, nothing happens. So why nothing happened?
Well, here Boolean a is more than five is equal to A is more than five. In this case, eight is equal to four, so this is false. So then we say if A is more than five, which is false, then we print it. But this one will only execute if whatever is in here is true. And in this case, a is more than five is false, since a is less than five. So in result, we don't print A's more than five, and it just ends the program.
So what if we input six? There we go, a is more than five. So what happens? So first of all, int a equals st dot next int a is equal to six and two inputted six. Then we have a Boolean a is more than five checks if A is more than five. In this case, it is since six is more than five so it's true.
Then if A is more than five and a is more than five is true, we print system of ln A is more than five. So that is how an if statement works. works. So if we actually just put true in here, there we go. Don't always execute no matter what we inputs with number three, it will execute, no matter what. So if true, this will execute.
And if we put a false in here, it will not execute no matter what we put, as you can see. And actually we get an error a warning here it dead code, which just means that it won't ever be executed no matter what we do, it won't ever be executed, and there's no point in happy. So now we're just gonna have true here. So that is essentially how can we work with if statements. And if statements are actually pretty important. There's a lot of things we can check.
And actually, if you really think about it, a lot of what we program is going to be if statements. So for example, we just have a user that's logging into our web application. What we do is we take his username that he entered, and we get, you know, the user name. And then we check for every single person inside our, you know, database, you know, database, essentially where we store data, it's a more, more, sort of, it's a different topic altogether than Java. So, in general, so we check our database and we do if the username is equal to one that is in the database, then you know, we get that user and we can try to log him in. And then once we get that user, if he does exist, then we can check if the passwords match.
So if password is equal to the one that he entered, so if the password in a database is equal to the one that he entered, then we will perform the logging in operation and if it isn't equal, then we just return back, you know, incorrect username or password for just for example. That's actually how if statements are useful, and they are very useful. Okay, so now let's go a little bit over what exactly if statements does do and how we can use them. So we don't necessarily need to put an age more than five or create a Boolean ahead of time and then put it in, because we just do it inside the if statement itself. So A is more than five. We're going to do it.
So this is, remember that this just returns a Boolean. So we can put relational operators directly inside Boolean, you're gonna check it as well. So if a equals equals five, then we're gonna do it so if we do five now, a is more than five. So this case it's going to ask there needs to be is equal to is equal to five. And then so now it's more correct age equal to five There we go. But generally you understand how this generally works.
So now we can put these operations here we can also do and and operations so a is not equal. to six, for example. So we can do if A is more than five, and then A is not equal to six, we'll just print eight. Now. If we put six in here, it'll do nothing since a is equal to six. And if we do seven, it does print a since a is not equal to six, and that is more than five.
And the same thing if you put four in here, since a isn't more than five, it won't print a. So those are essentially if statements. So now let's create an application together, which will utilize if statements in a pretty complex way. Alright, so we're going to write an application which will check check whether a number is positive or negative. So we're going to do intake equals SC dot extent. And so now what we're going to do is do if A is more than zero.
We're going to do system dot out dot print ln. A is more, no actually is it a is positive. There we go. And then we're going to do if a is less than zero, we're going to print a is negative. All right, and so now, if I do if A is equal to zero, we do System dot out dot print ln. A is equal to zero.
There we go. All right, so now if we run it, and we do, one, a is positive, if we do, negative one is negative, and then if we do, zero, a is equal to zero There we go. So that is essentially how we work with if statements in Java and if statements are, as I said a couple of times, I think in this lesson already are very important. So if you can get this down, then you'll have a good understanding and you'll, you can get into the next lessons much, you know, with confidence. Plus last word there. Okay?
So without further ado, then let's send you some homework. Alright, so for homework, it's also pretty simple. So all we're going to do is check whether or not a is divisible by 10. And then if it is divisible by 10, we're going to print it is. If it isn't divisible by 10. We're going to print it's not.
So pretty simple. Try to think about it. Just a quick hint, you're going to use the mod looks modulus operator for this one. So yeah. Alright, so anyway, ah, I wish you luck and I'll see you next time too soon.