Friday, May 27, 2022

# AP Computer Science Unit 3: Boolean Expressions and If Statements

AP Computer Science a unit three boolean 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 I already saved it so basically remember that unit three the class name must match the file name which is up here you'll see where it says unit three dot Java and we've got .

Our main method here so in this unit we're gonna be looking at boolean expressions 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 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 four and we've got Y it is equal to two so what we can do we can 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 four 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 four does not equal two so let's change that just see what it see what happens if we change that number .

Around and we should get true let's try it 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 is gonna happen okay so it does kindly convert that for us we don't don't 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 and we've got naught 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 ok 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 it's 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 four we'll get quite a different result okay now it's a little hard because you're not sure which ones 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 little extra characters there to help you understand then where did I mess up ok ok so you just always read the error message tells you that line 19 couldn't find the symbol that know what was going .

On and let's run that again okay so is 4 equal to 4 yes it is it's 4 not equal to 4 false 4 is not less than 4 it's not greater than 4 but it is less than or equal to 4 and it is greater than or equal to 4 so I put that back the to just 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 three point two suicide three point two 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 I say system dot out dot print L and if you are over 25 and say wow you are really old I'm just sad for me I am definitely over 25 so we run that okay it says wow you are really old so cuz the age is 47 let's change that to .

17 and run that okay nothing happened because you're not over 25 let's try 25 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 and system dot out dot print it's print L in congrats you are still young so let's run that let's change that to 23 and congrats you are still young say 25 ok 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 sort of say this is section three point three if-else statements so an 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 you say if age is greater than it's greater than 25 because just copied I suppose and actually maybe I will do that since you're over 25 now there is only one possible alternative here okay so I need is I'm gonna else so otherwise .

Congrats you are still there okay so let's try that okay so wow you are really old that's from the first one that's for 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 then 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 if else if it's called right at four else if statements okay 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 yes and that should be three point four not three point three okay so if your age is over 25 else well that's what I do here if your age is I'm gonna say less than 25 okay 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.println 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 Ln I okay so when that again so you are still young and let's try 25 cuz that's a little bit different between the different options okay so still young so yeah or hmm you could go either way and 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 a double score so your score on your test was an eighty seven point five .

Percent not bad so what we want to 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 I do here and okay that means I am you don't type and type two 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 JIT .

Less than 100 because if it's a hundred you won't see anything and then what we do is go system that out dot print Ln you got in a go so keep that simple and I'm going to copy that stereotype there's 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 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 will 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 let 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 yeah that's C depending on what your school systems like may be more generous or less generous so 60 is a D this is the standard system the United States at .

Least and then once a else now the only thing left basically is else today's you know system.out.println sorry you got an F so let's try that and see what happens yeah I did I got my beat which I'm very happy about now let's what you need to do if you were if you were properly testing this .

You need to test not only yeah 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 make sure that'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 a compound boolean x' now this is and and but if we wanted to do or it is two 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 two 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 sir who it 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 about toward a true so what look at this code what do you think is gonna happen here if customer age is greater than or equal to 65 or is dis 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 dot println you are eligible for a 10% discount okay so we're gonna run that make sure it works and I'll explain what shirts are good evaluation ok cannot find symbol and I can I spot it incorrectly so let's run .

That again okay you're eligible and there we go a much better speller when I'm not talking and typing at the same time so short-circuit 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 and 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's a short-circuit evaluation is okay so if I do and so 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 and 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 it doesn't matter .

What this is if it's true it's still all false 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 is condition cannot be true or cannot be false based on the first evaluation yeah the next one is kind of a .

Interesting one equivalent boolean expressions so I'm just gonna say let's say we have two boolean we have a and it is false now can we have boolean B and it is true this is so that will come in handy you'll probably see a one question at least on the AP exam dealing with this concept okay so if I type system dot out dot print Ln and I'll watch what .

I do here not a there'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 okay now watch what I do it's not a and not B so let's try this .

Together so not a so that's true not be it's gonna be false going to reverse it so true and false is gonna be false and that's right 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 in the system out print no and so 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 I'll explain it okay so we've 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 and 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 ant same thing down here we distribute the exclamation point so we got expansion point a we distribute to B as well so exploration point B and we reverse the sign okay or the operator there okay so that is like that .

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 use intz we tried a couple doubles and we also used boolean .

Okay 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 it was called string name one string name two equals Christian so I'm gonna type system where's that here yeah .

System dot out dot that dot print Ln name one name two and then I'm gonna say system dot out dot println name one equals name too now what do you expect to happen does name one actually equal name too okay so let's try that okay true because Christian and Christian are the same at least that's what you think okay so now .

Watch what happens say I'm gonna change name to San Diego and I'm just gonna copy this I'm gonna see what happens when we do that okay so in this case is me echo equal to or is name one equal to name two and the answer is false so Krishna Mako are different so so far so good everybody's happy it's exactly what we .

Expected okay now watch what happens if you recall from an earlier video there are is another way of defining a string string named three equals new string say string name four and this is the this is how we create objects 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 gotta change the numbers there for okay so let's run this so now you see name three is Christian name four as Christian as well we're printing them out we're comparing them and what do you expect to happen hopefully gonna say true however it comes out as false okay .

So this is a was a little quirky thingies that quirk using the right word but this is just one it was 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 okay 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 okay 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 okay so their names are the same but they are not the same .

Person so try this here so now let me try name four equals me a goal okay I'm going to print it out one more time and what do you expect to happen here three four mr. semicolon somewhere as I do okay so Christian me echo 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 gonna go ahead and do since I already typed this my notes so this get a little long so we're making named five a name six again they are both Christian but again they're gonna be pointing to different memory locations so the way we do this and this is something we learned previously and that now you know why .

So say if name five dot equals we learned the equals method before name six say system.out.println the names are the same yeah you put like a little else here so I say else no system okay so let's run that know what side you're on okay if so it says reach end of file while parsing so this is reversed so .

Knew something was going wrong there and the names are the same okay so when we're comparing objects we're gonna 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 is equal there's two things are equal not equal less than greater than less than or equal to or greater than equal to again these are primitives so integers doubles and or integers and doubles and boolean sorry so then here we did some comparisons we .

Use an if statement basic if statements we used if-else in a case where there's two possibilities either yes or no and we use else–if in a case where there's three or more possibilities we learn how to check for two conditions using and the ampersand so double ampersand I should say and notice there are these are wrapped in parentheses because .

Parentheses inside parentheses and we can we use if else if and else at the bottom talk about short-circuit evaluation and it's something that happens in the background we don't really see it happening but it's just something to be aware of that it is something that does occur in case you're expecting something to happen and then .

We talk 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 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 equal method so stay tuned for more

RELATED ARTICLES