 AP computer science a unit 3 bullying expressions and if statements So I've already gone ahead and created the basic code outline. I'm gonna go ahead and save this As a reminder Well, I already saved it So basically remember that unit 3 the class name must match the file name Which is up here. You'll see where it says unit 3 dot Java and we've got our main method here So in this unit, we're gonna be looking at boolean expression. So 3.1 boolean Expressions very exciting stuff Boolean expression we talked about it earlier in one of the earlier videos Basically a boolean expression has two possible values. It is either true or it is false There's no half truths. There's no nothing like that. It is all true and false So let's take a look at this We'll say let's say we got x and that is equal to 4 and we've got y it is equal to 2 So what we can do We could do something like this x equals y now notice there are two equal signs Okay, one equal sign means we are assigning a value So in this case, we're saying that x is equal to 4 in the case where there's two equal signs We're actually comparing so we're asking does x or is x equal to y? So let's compile that run it and see what happens Okay, it comes out as false. Okay, because obviously 4 does not equal 2 So let's change that just see what see what happens if we change that number around and we should get True Let's try this. I'm kind of curious. I don't actually know the answer Let's say if we compare a double and an int. What do you think's gonna happen? Okay, so it does Kindly convert that force. We don't have to worry about that so it does tell us if those numbers are equal which is quite nice and a pleasant surprise so Some other comparisons that we can do let me just copy this We've got Not equal to so the exclamation point means not equal so is x not equal to y So 4 is not equal to 2 so that's going to evaluate to True. Okay, which is what I expected Let's try another one is x less than y And then I'm just go ahead and add a couple more is x greater than y And let's try those two so is x greater than y Oh is x less than y? No, it's not it gives us false is x greater than y. Yes, it is that gives us true and we can also do less than or equal and Greater than or equal don't make mistake of reversing the order. It's got to be less than or equal to and Let's try that Okay, so we got False and true because x is not less than or equal to y but it is greater than or equal to y So if I change this y to 4 We'll get quite a different result Okay, that's a little hard because you're not sure which one's which so since I've already typed it up nicely in my notes I will just go ahead and copy and paste that so we get some nice formatting. You can see better a little better What's going on? Okay, so it's just same thing. We just did but just with a little extra Characters there to help you understand and where I mess up. Okay Okay, so you just always read the error message. It tells you a line 19 couldn't find the symbol didn't know what was going on and Let's run that again Okay, so is 4 equal to 4. Yes, it is for not equal to 4 false 4 is not less than 4 It's not greater than 4 but is less than or equal to 4 and it is greater than or equal to 4 So I'll put that back to 2 just to put that back where it was So what we can do is we can use this concept these comparisons to make sure that or to basically kind of you know Control the flow of our coat. So this is section 3.2 So you say 3.2 And this is if statements And Control flow Okay, so let's say int age equals 47 Okay, so we can do something like this if age is greater than 25 or you can put it like this. You don't have to put the spaces. I tend to put it because I think it's easier to read Then we need curly braces for this condition and I say system dot out Oops down print ln if you are over 25 and say wow, you are really old Just sad for me. So I'm definitely over 25. So we run that Okay, it says wow, you are really old. So because the age is 47. So let's change that to 17 and run that Okay, nothing happened because you're not over 25. Let's try 25. Oops. Let's try 25 by itself And we'll come back to this in a second and Nothing happened because we don't have the a true condition for this. So let's put that back to 47 So basically what happens age is greater than 25. So that is true. So basically if true Then do these things. Let's try this if age is less than or equal to 25 System out that print it's print ln Congrats You are still young. Okay, so let's run that Let's change that to 23 hey, congrats. You are still young Say 25 Hey, congrats. You are still young, but sadly once you hit 26 It is game over apparently. Okay So now notice I've got two different if statements with two different conditions Okay, so we're gonna look at a different way here to kind of combine those So I say this is section 3.3 if else statements So if else statement Okay is used in a case where essentially you've got two conditions, which is what we have here Okay, we've got two possibilities if you're over 25 or not over 25 So in that case we can simplify our code when we say if age is greater than greater than 25 Could just copy that suppose And actually maybe I will do that So if you're over 25 now, there's only one possible Alternative here. Okay, so I do something else So otherwise Congrats You are still young There Okay, so let's try that Okay, so while you are really old that's from the first one. That's from 3.2 Okay, you see we get the exact same result here because age is 26 Now what we can do is if we say if you're not over 25, let's make that 23 again You'll see where it prints it out So that way instead of putting two if statements we can just use one if statement and an else That's kind of basically how that works Now let's imagine a case where we've got three possibilities or more So I'm gonna this is a if Else if it's called wait for Else if statements So what I'm gonna do is I'm gonna go ahead and Copy this and then I'll just go ahead and change it So that should be 3.4 not 3.3. Okay, so if your age is over 25 else What I try to do here if Your age is I'm gonna say less than 25 So notice those are two different conditions. You're over 25 or if you're less than 25 and then there's one more possibility Which is 25 we wouldn't put else if age equals 25 although we could But what we would do is the best thing is do is out print LN hmm You could go either way So we have three different possibilities and this could be You know three possibilities four possibilities five possibilities etc etc Okay, so let me run that just to make sure it's working properly Okay, got an error could not find symbol because I said print L and I Okay, so One that again So you are still young and let's try 25 because that's a little bit different between the different options Okay, so still young so young or hmm. You could go either way. Okay, so that's kind of how we do Comparisons now just a quick quick thing to remind you if we are comparing strings This won't quite work the way you think it will we'll get to that in a little bit Our next section is called a compound Boolean expression. Okay, so this is section 3.5 Oops compound Boolean Expressions okay, so let's say for example double score So your score and your test was an eighty seven point five percent not bad So what we want is we want to convert that to a letter grade. So what we do is like this if score Is greater than or equal to 90? Now watch right here and Hey, that means am you don't type and type to ampersands and Score is less than or equal to 100 be careful with the the edges there So 100 make sure you don't put minus or just less than the 100 because if it's a hundred you won't see anything and then What we do is we have system That out that print ln You got in a So keep that simple. I'm gonna copy that Retight those a few times Okay, so now what about a B? So we're gonna use else if And maybe I just copy this whole section And just change it Might be a little easier Okay, so in this case a B be greater than or equal to 80 Now is it less than or equal to 90 or is it less than 90? Okay, so I would use 90 here because 90 is part of the a so we'll give you the a if you got a 90 Now you can also do eighty nine point five if you're feeling generous But let's just keep it simple for now and we'll make it less than 90 and that's gonna be a B And then the same thing we can copy that Else if That's gonna do a C And we'll do D. So C is gonna be greater than or equal to 70 It's also be less than 80 That's a C Depending on what your school systems like it might be more generous or less generous So 60 is a D. This is the standard system in the United States at least And then we'll say else and the only thing left basically is else Say, you know system out that print on sorry you got an F So let's try that and see what happens Okay, I did I got my B, which I'm very happy about Now let's what you need to do if you were if you were properly testing this you'd need to test not only You know, let's say 85 95 75 65 55, but you need to test all these different edge cases So 100 is like right on one of the edges. So test that and make sure it works Okay, now this this is very simple code. So it's not such a big deal But you might want to check 90 and make sure it's working Okay, so 90 is working So you'd have to go down through and check all of those different values to make sure there's no errors in your logic of your program Okay, so that is that so we have compound Booleans now this is and and but if we wanted to do or it is to Pipes like that. Okay, so that is or you don't use or use two vertical pipes And if you're doing and it is to ampersands, so be aware of that Okay, next concept that we want to go over it's something called short-circuited evaluation and it's what sounds way more complicated than it actually is so Short-circuited evaluation Hey, so let's say for example, I have int customer age Equals 65. So that's the retirement age and I say Boolean Is disabled and I'll say is true not today. True. So what look at this code? What do you think is going to happen here? If customer age It's greater than or equal to 65 or Is this disabled Now notice here. I didn't put equal to true although I could have Because this is already evaluating to true or false. I don't have to put it inside of parentheses I can just leave it like that So I say system dot out that print l n You are eligible for a 10% discount Okay, so I'm gonna run that make sure it works and I'll explain what shirts are your evaluation Okay, cannot find symbol And I could I spell it incorrectly Let's run that again Okay, you're eligible And There we go. I'm much better speller when I'm not talking and typing at the same time So short-circuited evaluation what this means So take a look here customer age is greater than 60 or equal to 65 Okay, so is that true And the answer is yes So because this is true And this is or Does it matter what's over here So this is false It's still altogether true If this is true It's still altogether true So what that means is it actually will ignore this Because this is true It's smart enough to know that because this is true and this is an or This has got to be true. That's what short-circuit evaluation is Okay, so if I do and You have to have both conditions And You have to have both conditions Over 65 and be disabled But if I say If you're over 65 And this person is actually 64 Okay, I run that Okay, we won't see anything because there's no else here But short-circuit evaluation It will check for the customer age being greater than or equal to 65 This is false Now because this is an and No matter what this is, if it's true it's still all false If this is false, of course it's all false Okay, so this is what short-circuit evaluation is It's basically the compiler Basically, you know, realizes that this condition cannot be true Or cannot be false based on the first evaluation The next one is kind of an interesting one Equivalent Boolean expressions So I'm just going to say Let's say we have two Booleans We have A and it is false for now And we have Boolean B and it is true This is something that will come in handy You'll probably see one question at least On the AP exam dealing with this concept Okay, so if I type system.out.println And I'll watch what I do here Not A, it's A or B So let's evaluate this one together So A is false or B B is true So this evaluates the true But we have the not outside of this whole expression So true becomes not true So it's going to print out false Okay, good, I'm glad that worked Now watch what I do Not A and not B So let's try this together So not A, so that's true Not B is going to be false Because we reverse it So true and false is going to be false Let's try it again Okay, so these are equivalent Boolean expressions Let me do two more And then I'll explain how this works I got not A and B And system.out.println So it should be not A Maybe you see the pattern already This is something called De Morgan's law And let's run that Make sure it's working and I'll explain it Okay, so we got false false true true Okay, so what you see here Is notice that this exclamation point Is outside of the expression So A or B So if you want to find the equivalent expression here You distribute the exclamation point So the A gets an exclamation point The B gets an exclamation point And you reverse this So this is or, so we reverse that to and Same thing down here We distribute the exclamation point So we got exclamation point A We distribute to B as well So exclamation point B And we reverse the sign Or the operator there So that is something called De Morgan's law It will come in handy when you're trying to Solve these types of problems on the exam We'll do more practice with that in class of course And now the final part of this Comparing objects So thus far what we did with comparisons It was all primitives We used ints, we tried a couple of doubles And we also used booleans Now with objects things are very very different Sometimes they behave how you would expect Other times they do not So let's take a look at the following example So let's say we got string name equals Christian Oops, let's call it string name one String name two equals Christian So I'm going to type system Where's that? Yeah System.out.println Name one System.out.println Name two And then I'm going to say system.out.println Name one equals name two So what do you expect to happen? Does name one actually equal name two? So let's try that Okay true, because Christian and Christian are the same At least that's what you think So now watch what happens Say I'm going to change name two to Mieko And then I'm just going to copy this Let's see what happens When we do that So in this case is Mieko equal to Or is name one equal to name two? And the answer is false So Christian and Mieko are different So far so good Everybody's happy That's exactly what we expected Now watch what happens If you recall from earlier video There is another way of defining a string String name three equals new string Say string name four This is how we create objects This is how we instantiate an object So remember the dog class we did before? This is how we did it with dogs But in this case we're using strings So let's try that And I know I got to change the numbers there Okay so let's run this So now you see name three is Christian Name four is Christian as well We're printing them out, we're comparing them And what do you expect to happen? Hopefully you're going to say true Or it comes out as false So this is one of those little quirky things Not that quirky isn't the right word But this is just one of those really deep The way computers work type kind of things So without going into too much detail Basically this Christian string Christian And this string Christian are stored In different parts of the computer's memory So up here it automatically stored it In the same place It recognized the fact that they were the same characters So they actually pointed to the same place in memory That actually saves memory for you So that's why when we said name one equals name two It literally means they're actually pointing to The exact same spot in the computer's memory So this is useful in certain cases In this case they would be what's called An alias of each other However when you're comparing objects The value in this case is the same But the objects are different It's like if you have two people And they're both named Christian So their names are the same But they are not the same person So let me try this here So now let me try name four equals Diego I'm going to print that out one more time And what do you expect to happen here Three, four Still like holding somewhere As I do Okay so Christian and Diego false They are not the same They're not pointing to the same memory location And then we can also do one more time I'm going to go ahead and do Since I already typed this in my notes This is getting a little long So we're making name five and name six again They are both Christian But again they're going to be pointing to Different memory locations So the way we do this And this is something we learned previously And now you know why So we say if name five dot equals We learned the equals method before Name six Say system dot out Print ln The names are the same And we can put like a little else here So I say else Okay so let's run that What did I do wrong Here So it says reach end of file while parsing So this is reversed So knew something was wrong there And the names are the same Okay so when we're comparing objects We're going to be using the equals method And maybe if we have time We'll learn a bit more about how that works In later units we're writing classes Okay so I think that is about that That was a bit of a long one But just a quick review We did Boolean expressions They are either true or false And we can compare using two equal signs If something's equal Two things are equal Not equal Less than, greater than Less than or equal to Or greater than or equal to Again these are primitives So integers Doubles and Integers and doubles and Booleans So then here we did some comparisons We used if statement Basic if statements We used if else in a case Where there's two possibilities Either yes or no And we used else if In a case where there's three Or more possibilities We learned how to Check for two conditions Using and the ampersand So double ampersand I should say And notice that these are wrapped In parentheses So it's parentheses inside parentheses And again we used if, else if And else at the bottom Talked about short circuited evaluation Again it's something that happens In the background We don't really see it happening But it's just something to be aware of It is something that does occur In case you're expecting Something to happen And then we talked about equivalent Boolean expressions Again this is De Morgan's law And you will need to know this For taking the test Or I should say it's very helpful For taking the test And then comparing objects We talked about a little bit How to compare an object We can't use equals equals Because that is comparing to say If they are the exact same object Not just the same value You can see how we get inconsistent results Depending on the way the objects are defined And we don't want that to happen So to avoid that We use the equals method So stay tuned for more