 Okay, welcome to Intro2Java with an emphasis on AP Computer Science A. The unit in question today is Conditionals. I am Tokyo EdTech, your guide. We're gonna be looking at basic if statements, how to compare strings, how to compare primitives, user input with the scanner class, the comparison operators, the logical operators, something called short-circuit evaluation, else statements, and else if. And I've actually cut quite a bit out, because this would be like an hour long video if I didn't. So definitely check out my Intro2Java ebook for some more information and more details. So let's get started. So a basic if statement. When I talk to my students about coding, this is the core of programming. I tell them programming is four words. If this, then that. So your basic if statement is if some sort of condition, and then you have a code block, and then here you would just do something. So if this, do that. That is, that's all an if statement is. And all of the amazing coding, all the amazing things you see computers do, this is pretty much the fundamental core of it, in my opinion. So let's go ahead and take a look at an example of a comparison. Let's go ahead and do string password. We'll say equals Joshua, and this is, if you're not familiar, this is from the movie Wargames. If you haven't seen it, check it out. So basically, the correct password is Joshua. So what we want to do is we want to check. So if password dot equals, you should remember this from the strings units, Joshua. Just a reminder that small j Joshua is different from big j Joshua. Don't forget that second parenthesis there. And system system dot out dot print l print l n. Yeah, we'll say greetings, Professor Falcon. Let's go ahead and compile that file compile successfully and we'll run it. And we see greetings, Professor Falcon again. If you've seen the movie, you'll get the reference. But long story short, if this, so if the password equals Joshua, which is the correct password, then we say greetings, Professor Falcon. If this were not Joshua, let's go ahead and do the lowercase version of that. Compile it over to compile or run it. You see nothing because we didn't program it to do anything if it's not correct. We'll get to that kind of stuff later. Okay, so with primitives, comparisons work a little bit differently. So for example, I'll say int x equals 42. And then we'll say if x equals 42, notice the two equals signs there. Okay, so we use dot equals because strings are objects. And I go into much more detail on this later in this video series later in my ebook. So if x equals 42, we can say system dot ounce dot print l n x equals equals two. And don't forget your closing braces. I forget this quite often. So we compile it, we test it. And we can see x equals 42. So we say that this evaluates to true. So we check. Does the password equal Joshua? If so, it is true. If not, it is false. Does x equal equal does x equal 42? Is that true or false? So if this is true, we do that. So that's kind of an important point. It evaluates to a Boolean expression. And notice it says here Boolean expressions and if statements, I shortened it on the title, because the text gets too small. So now this is where kind of we can start doing some interesting, a little bit more interesting things. So so far in our programs, at least in this video series, we've hard coded all of the values, meaning, you know, we just said int x equals 42. But that's kind of boring. So we want to ask the user for some information. So to do that, we need to do something, use something called the scanner class. And to use a scanner class, we need to do a couple things, a few things. First, we need to import the scanner class. Notice this import comes before the class declaration. So don't forget that we have to import it. It's not part of the actual core Java. It's something that was kind of added on, I should say later, I'm not sure how to put it. So then we need to create a scanner object. So scanner SC equals new scanner system dot in. And you'll learn tons more about objects and object instantiation later. But for now, you can just kind of memorize this. Now the scanner part has to be exactly as is system dot in has to be exactly as it is. We just use SC. It's kind of a common thing to use for scanner. And we need to import the scanner class. So now that we have this scanner class, we can use it to get information from the user. And so for example, we can now do password. I shall do system dot out. Watch my dear dot print, write it like that. I feel old school. Now I can do I can do password. I didn't put string here because I already did it up here. If I do it, if I do string again, I'll get an error. You know, you only need to declare the type once. Now password equals SC dot next line. What this does is it stops the program waits for the user to enter some text, and that text is converted into a string. So now we can say if password, I'll go ahead and I'll go ahead and be lazy. I'll go ahead and copy this. Let's go ahead and copy that. So if password equals Joshua, go ahead and say greetings, Professor Falcon. So let's go ahead and compile that and compile. Good soon sign. So you see it's sitting there waiting for me to enter the password. So I'm going to go ahead and type Joshua and see what happens. And you can see it also says greetings, Professor Falcon. Again, now if I run it and I type password 123 terrible password, you see nothing happens because we didn't program it. What to do if password doesn't equal. Yeah, with this scanner object, we can ask for an integer instead. So I can say int, say num equals SC dot. Let's say next int. Let's go ahead and copy paste here. So I'll say number. Let's do something like so if I say it was like a guessing game if number equals, let's say seven system dot out dot print. Correct. Let's say like we're doing I guess the number type game. Now I could also do and I'm going to get to this a little bit in the next section, I believe, but I'll go ahead and just jump ahead to comparison operators. This is not equal. And I'll say incorrect. Let's go ahead and try it. Run it. Okay, so we'll just go off the password. And we'll go ahead and enter seven, see what happens. Oh, geez, what was that? So, okay, so you see how it says correct because I entered the integer next int. Let's go ahead and try it again. And we'll enter a five. Okay, it says incorrect. Okay, so it seems like it's working, which is pretty cool. Now here's something you should probably try. Let's go ahead and enter below the password. Now let's go ahead and enter like ABC for the number and see what happens. Okay, you can see here we get an input mismatch exception. Because it's looking for an int, but it can't convert this to an integer. So you got to be careful with that. Again, if you learn more advanced stuff, there's ways to deal with those. The AP doesn't really cover that. So we're just going to ignore it for now. But you can, you know, catch exceptions, it's called. And then we have one more, we could do like next double. But I think you get the idea. Okay, so we've got next line for strings, next int for integers. And you can also do next double for doubles. Okay, so you can see here here are two of the comparison operators. We have a double equal sign to compare. Are they equal? We have not equal. So the other ones are greater than. We also have less than also greater than or equal to and less than or equal to. So there are 123456 main comparison operators that we can use. Just a reminder with strings, which are objects, we use dot equals. We don't use two equal signs. There is a case where we do to use two equal signs, but it has a slightly different meaning when we're dealing with objects. And we'll get to that maybe a little bit later in either the book or in this video series. Okay, now we also have what are called the logical operators. And so the logical operators are when we have two or more conditions that we want to compare for. So the one I example I've been using is double gigawatts was 1.21 and double speed equals 88. So what I can do is I can say if parentheses here speed, say I'll say if gigawatts is greater than or equal to 1.21. And because there's two conditions, this is from back in the future, if you haven't seen it, you know what I'm talking about is greater than or equal to 88 miles per hour. And we can do system dot out that print ln. Welcome to 1985. Because what in the movie if you've never seen it, you need 1.21 gigawatts of electricity. And you need to be moving at a speed of 85 miles per hour to travel through time. So blah, 23. And you can see here, welcome to 1985 because both conditions are met. Now let's say we only had 0.5 gigawatts let's compile that. And we run it as for number. You can see how it doesn't print anything because we didn't meet the requirements. Okay, we also have or. So we also can do an or operator where one condition, one of two conditions could be true. So for example, if password dot equals Joshua or password dot equals Joshua lowercase, lowercase, then we can say, and copy that means Professor Falcon. So in this case, we have two possibilities. Go ahead and so say Joshua say seven. So you can see here seven is correct. And you can see because this is still less than 1.21 we didn't print that out. Let's go put that back in there. So you can see here because password was we entered the password up above. So it checks does it equal Joshua or Joshua with lowercase? Let's go ahead and run that and try it again. I'll do Joshua lowercase number six. And says greetings Professor Falcon because this is true. So if this is true, or this is true. In this case, if this is true, and this is true. So then this brings us to our next concept, which is sort short circuit evaluation. Basically, that's a very simple concept. And basically what it says is, well, let's take a look here. So if this is 0.5, and I compile it and run it. Now this is going to evaluate to false. So if this is false, does the computer need to check this? The answer is no, because it doesn't matter if this is true, the whole thing still falls. If it's false, the whole things false. So that's what short circuit evaluation is. If the left side of an end is already false, the computer will skip checking the right sky. The reason this is important is you might have a very rare case or it just say basically long story short, it just saves processing time. So let's leave it at that. So in the case where the left side is false, we have an and the right side is not evaluated at all. Now in the case of or similar, if this evaluates to true, does it matter what this is? If this is false, or if this is true, it doesn't really matter. So if this is true, the computer will not evaluate this because it knows the whole expression is going to be true. Again, it saves on processing time. So it's something to be aware of thing and how your code is set up. That's a short story evaluations. All right. And the last two topics in this section are else and else if. So I'm going to go ahead and copy this, I'm actually going to paste this, make it easy. So in this case, if the password, the password is either correct, or it's not correct. I wanted to go ahead and do else. Say system.out.print. And we only have two possibilities. I'm gonna say access denied. It's what it looks like in the movie. So I'm gonna go ahead and compile that. I'm gonna run it. I'm gonna type the password, you know, tic-tac-toe. If you watch the movie, you'll get why I'm typing that number. And you can see here, the access is denied. Because the password tic-tac-toe, it doesn't equal Joshua. And it neither does it equal Joshua with a lowercase. So this is, if this is false, it will run a section of code. So this is a section that runs if true. This is a section that runs if false. Now, there are situations where you would have multiple possibilities. But I should say multiple exclusive possibilities. Let's go ahead and say int grade. So, you know, if you're a student, so you have a grade level. So if you're in grade 12, you are not also in grade 11. Okay, they are mutually exclusive. Go ahead and set this up. System. Grade 12, leave it that and copy and paste. 1110. That's about right. We got grade 12. We've got grade 11. Now, if I run this code, execute it, you'll see where it says grade 12. So I just have a bunch of if statements. But what we want to think about here. So if it's grade 12, is there any reason to check if it's 1110 or nine? Of course, the answer is no, because they are mutually exclusive. So in that case, we use else if. So what that means is it will first check, is it grade 12? If it's grade 12, then it skips all of this. If it's not grade 12, it'll check. Is it grade 11? It's grade 11, it'll skip the rest. If it's not, it'll check. Is it grade 10? It is, grade 10 skips this. If not, it goes checks if it's grade nine. And then what we can do at the end is we can add an else, a final else system or not a high school student. So this will catch any other value other than 12, 11, 10, or nine. Let's go ahead and say six, see what happens. So you're a sixth grader. Password A number one. And you'll see here, you are not a high school student. So this is a really important way to organize your code. So that we're not executing code that we don't need to execute. We'll also see this a lot when you're converting. Just add this real quick. So let's say int score equals 95. What you're doing say as well, if score is greater than or equal to 90 system, system.out.printlnA. Okay, you got any congratulations. And we could go, okay, just go down through. I'll just I'll do this. I'll just do A and B just to make a point here. And then let me show you something that's interesting. So if I type this, if I run it, you can see how both A and B print out, because the code says if the score is greater than 90 print A, then it goes the next one says if the score is greater than or equal to 80 print B is 95 greater than or equal to B. Yes, of course it is. So in this case, we would use else if as if it's greater than 90, this part doesn't get executed one last time. You can see now it just says A, this part is skipped. So we're converting from usually like a number or two, like a range to a value that sort of thing. So you'll see it a lot, you know, in scoring or maybe can very different types of things, you know, when you're converting from usually like from a number to some sort of textual description like you have here. So that is that that is a basic introduction to conditionals. So we looked at the basic if statements, strain comparisons, primitive comparisons, using the scanner class. So don't forget to import it, create scanner objects. And again, we'll get to more of that later in the course. And how to compare strings versus how to compare primitives, comparison operators, the logical operators. And again, it's just a little bit of thought process goes into some of these things, something called short circuit evaluation, which you need to know for the AP for some weird reason, and then how to use else and else if statements. So that's that. Thanks for watching.