All right. Before we dive into Boolean Expression statements, let me set the stage. So basically in C Control, control structures and Java and programming languages, we basically have three sorts of control that we can have. Okay. Events are another thing but these are the core ones we'll talk about a lot, events are not part of the AP CSA class and we're not going to be covering them here. But basically you can execute your code in sequences like straight line code, you could have selection which we're going to look at next which we usually you do with If Statements, although there are a few other options not covered in this class, or repetition which is various types of looping. The key difference between this is what's sequence is basically, if you only had straight-line code, your program would always do the exact same thing every time unless the input was different from maybe there's some initial input from the user or something like that. But it just follows these series of instructions straight, and honestly you could probably do it faster by doing it by hand. If Statements differ, selection differs in that it allows you in some way to skip certain instructions based on particular situations. Repetition is really where the power of computing comes because we can repeat a lot of the instructions and that's what computers can do that much much faster than we can, and that's where the power is. In this video and in this unit, we're going to focus on If Statements and Selection. Okay, two topics in Unit 3, Boolean Expressions and If Statements and a really good way to introduce them. I personally like introducing Boolean Expression with If Statements. If you took some of my other block-based programming course, I had some good examples of things that were Boolean. A smoke alarm, is it my smoke along going off? Yes or no, that's the key to Boolean Expressions, they evaluate only one of two values. We've previewed that by saying, we have had Boolean variables that are one of two values, we have a smoke alarm going off, can I take money out of my bank account? Is my bank account balance less than $10? If not, I can't do that. The key thing here is there's this Boolean condition that needs to evaluate one of two values, true or false. So for your kids, I like using the coin flip, it can either be heads, it can be tails and we're not going there because that doesn't actually really happened in real life. Okay. So thinking about block-based languages this is whether you're on Scratch or Snap or many others, this would often be under your palate under operators. Here's the set from Scratch, let's walk through and talk about how they're going to be different in Java. So let's start with these are less than equal or greater than. The one thing that's different here and you're kids are going to have so much struggle with this for a while, is that for comparing whether two things are equal, we're going to use a equal equal b. Because we already stole the equal sign for something else, it's assignment statement. That's where we use equal for now, if we set a equal b then try to put the value of b into a. If we say a equal equal b, is it like a is equal to b? I don't know, we got to ask. So double equals. There's also going to be some new ones that will fill out what we know from math or experienced from math as having we have a less than or equal to b or a greater than or equal to b. Not exactly how those are used in the math or the notation they use, but we don't have those things on our keyboards, so we just have to put less than or equal to, not usually difficult for students. The compound Boolean Expressions or at least the first two of these are compound And and Or. These are going to be things your kids are going to have to memorize. I know where they came from, so And is this double ampersand by the way that's called ampersand, and I can't draw it on the board to save my life, so don't worry if you can't either. The second thing is double pipe. So that's the straight up and down that usually right above your return key and you have to hit shift to get there, and it's two. Okay. Why don't we use something more interesting or normal. I don't actually know? I know where these came from it's a previous programming language C. I'll look around and see if I can find any interesting story about it. But again, this is just going to be one of the things your kids will have to memorize and get used to. Knot, by the way, is bang or exclamation point, and that gives you the inverse not true is false, not false is true. One of the things to be very wary about, the single ampersand or the single pipe, those are legal operators in Java. They just don't mean what you think they mean. There is something called Bitwise Operators, you're just students really don't need to know about. But the problem is, they will forget to put two of them, the code will compile, it is going to do what they expect. So you teacher keep an eye out for that. Let's go onto If Statements. Let me show you some examples of code that we might have done or we did in our previous class. In both, I think examples in Java and in block-based languages, If Statements really lead themselves to good explanations by doing score, calculations, making up some rules for scoring in a game. So here's something we had like, if the score is less than zero, say something, I'll say something else and then we had a separate If Statement and it turns out you could do this more efficiently. Another thing that you might remember if you've taken a previous class with me is building a soccer game. So in the two-dimensional grid world type block languages, you can use a compound Boolean Expressions and comparisons of x and y locations to determine whether an object is in a particular space on the screen. So we use this to determine that blue had scored a goal or red had scored a goal. So let's look at the actual block to Java comparisons. I love the block implementation and I think it really helps students understand If Statements better than before we had block languages and we just started kids on If Statements because they got used to this C structure or the E structure and we're going to see a lot of those. So those I don't think are too hard. Here is the nested If, which we also could do in block. Just to make sure it was really clear, I've pointed out that if Boolean Expression instructions starts with those instructions would be, and then we've got the Else, and then we've got another If whole If Else inside it, and you can see where those instructions will go. However, Java also has this, which is not exactly what is represented I guess in the nested If of the block language but it does this exact same thing. I'll tell you in a minute. Here's the difference, C Else and If are right there on the same line. In previous programming languages they even sometimes will put those words together Else If, like all one word or there's some other variations you don't really want to know about. But here's the thing, let me show you the two side-by-side in Java. They are different in terms of the actual typing. The one on the left that's truly what we call a nested If Statement, it's got the Else, it's got a bracket, it's got another If nested inside there could be multiple others nested, they're could be one after the other nested in there. The one on the right is Java's Else If structure, you'll notice there's no curly bracket. There's actually really good reasons to use that for a code readability which we'll get into later and probably aren't that important for our students but I wanted you to see that even though they look different, most importantly is they have the exact same logic behavior. These two particular where we have a very simple clearly If Else nested inside the original Else that can be done and maybe should be done with an Else If. Wanted just to show you one more example and also give an example from App Inventor. Again, scores, if the score is greater than 10, set the bonus to two. Else if the score is greater than five set the bonus to one else, do something else. One of the key things that you want to work with your kids is with a truly If Else, Else If, Else etc. You're executing only one of those statements. Now, if you do some other nesting approaches that may not be exclusively true. Here it's that same thing in App Inventor, and if you were in App Inventor, then you're already familiar with that Java Else If, because you were doing JavaScript essentially, and this is what Else If looks like in App Inventor. A few last small things that are not part of the AP CSA exam but are perhaps important for you to know as a teacher and talk to your students about. In many IDEs , Integrated Development Environments including Repl It, if you're using that, if you type in If and then maybe I hit enter and I type the left parentheses, it's going to automatically add that right parentheses for me. That's a really great thing as programmers we used to screw that up all the time, so most IDE's do that. The other thing I want you to know is that there are two completely valid and accepted styles for If Else and where you put those curly brackets, I tend to like the one on the left and that's a lot of what I've been showing you. Alternate style that is probably just as common if not more common, is to put that open bracket right on the same line as the If and then you keep that bottom curly brace bracket in the same place and that saves a line of code and makes it look a little shorter. I want you to just glance at that, I dare you to say that you can very quickly say, "I know exactly what this code is doing." Don't worry about it. Yeah, now I can see. If the score is less than 35, I do this. Else if it's less than 50, I do that, else if is less than 60, yeah that's nice, and that's using proper bracket placement and indentation. Here's the other option because I told you there were two that are acceptable. This is where you put the bracket up on the same line as the If, I think it's just close to as readable as everyone, that's why I like to have it a little bigger but this is legitimate too, you can very clearly see through the indentations what is being controlled by each of the If or Else If statements. So here's the takeaway though, indenting is not required in Java for correctness, but it's important, and you can grab those examples to show your kids and feel free to make them indent because there are actually in companies they have styled guidelines that people are required to follow, its style because it's not required by the language but you have to follow it if you want your code to be accepted and you want to keep having a job. So you can tell your students that's really important and it's also really important to get them in a habit, so that when they're doing code writing on the test, they can keep their own head clear about what is being controlled by what. Lastly, quick preview there's going to be a couple other things that you've not seen before. One is De Morgan's Law which maybe if you've got a background in math you might know and that's about how do we negate a particular compound Boolean Expression. This is going to get us into truth tables and there's definitely a couple of multiple trace questions on the AP CSA exam about that because it's so nice and easy to test. Then we're also going to talk about what happens when we compare objects. How do we know if one object is equal to another? We don't really have less than or greater than usually for objects, like how would one turtle be less than or greater than another one, but we might have some definition of two things being equal. We'll talk about that.