 Hello, time for today's mini lecture and let's share screen and we're going to start with the switch statement. And this program here, which I wrote a couple of minutes ago, where you enter a month one through 12. I get the month and then I have an integer for the number of days in the month. We're not just worried about leap year quite yet we'll worry about it later. And if the month is one days is 31 and since I have an if else if chain. Wow, that's a lot of work to go through, but it does work. And if the month is not one through 12 I set the number of days to zero. And then afterwards I'd use a binary if statement, if the days is not equal to zero that means that I have a legitimate month. Otherwise, it must not be in that range. Let's compile that and run it. So for example month numbers nine which is what we're in right now has 30 days. And let's say month number 11 has also yeah November hello. Let's try July has 31 days cool. Now the question is when I have an if else chain and I have the same variable on all of these is there a way to make this more compact or to do in a different way let's first think of a different way to do it and that is with a switch statement. We can say switch month. And then we can say, in case the month is one will set the days to 31, and then we will break out of this switch statement. Case two days is 28 case three days is I've got it right here in front of me. Case four days is 30. I'm going to pause the recording so you don't have to watch me type all of this stuff. And here we are. This is a very much more compact. This last case is default, which means if it's not one of one through 12, I'll set the days to zero and then break out of the switch statement. The last case or the default does not really need a break but for reasons of consistency, we're going to do that. While I was typing this I forgot to type in the break statement and there's a teachable moment here. Let's go and do this compile first. And let's run it. And let's say I have month number seven month seven has 31 days that works great. Let's say I have month three that has 31 days also. Now what if I say month 11. Let's see what happens. It says it has 31 days. The reason this happens is because whenever you leave out the break, it continues on to the next case. It's something that normally you don't want to have happen. You almost always want a break after every case. And I'm going to show you an exception to that in a moment. There are a lot of different ways that I could write this by the way. And I'm going to explore a few of them. And, oh my goodness, I forgot to save this under a different name. Okay, so let's call this days per month to Java. And I will bring back the other one later on. I'll type it after this is all done so that you don't have to watch me type all of that stuff again. One thing that we can take advantage of is if you have several cases that are all the same, then you can combine them. For example, I can say if the month is one or three or five, seven, eight, 10 and 12, then I can set the days of 31 and break. If I have case two, my days is 28 and break. And then I'm going to do something very different here. Let's do it the hard way first. Okay, let's do this. So now I case four, case nine, I case six, nine and 11 days is 30. And that will combine those cases. And finally, my default days is zero and break. So let's save this under different names that we don't lose our work on this. So if I have something like, let's say month seven has 31 days and month six has 30 days. So that works pretty well. Let's think of this yet another way of this, you notice that most of our months have 31 days, correct. So let's set days to 31, which is our most common case. And then we don't need the default. What will happen here is if the month doesn't match for any one of the cases days will never be changed it'll stay 31. Well, now you're going to say well that's great but what about the month being in the range of one through 12. And what we can then do here is use an if statement to take care of that. So we're going to say if the month is greater than one, greater than or equal to one, then check to see if the month is also less than or equal to 12. If that's the case, then we have something valid. And we can do all of this work. And since we know we have a valid month, we can print this out. Otherwise, we can say months cannot be greater than 12. Otherwise month cannot be less than one. So we have a nested if statement here to protect us. Once we get here, we now know that month must be from one to 12. If it were outside that range, the if statement would have caught us. And then we have a much shorter switch statement. So month zero month can't be less than one month 13 can't be greater than 12 months seven is 31 days and month six is 30 days. So that is an even more compact way of writing it using the if statements to protect ourselves from bad data. And let me put a comment here. And let me also put that in days per month three here. Oh, I don't don't have to do that in days per month three. That looks good. Let's go back to days per month three here to show you something here. If I had zero in here, it would say month must be in range one to 12. And if I said they're 14, it would also say month must be in range one to 12. That's an acceptable error message. By the way, when you, okay, time for some notes here. Let's save this error messages should tell the user what is wrong and how to fix it. So for example, if I say system dot print line of invalid data. Well, that's totally not you're not useful. What's invalid about it. That's slightly better month out of range. That's slightly more useful. At least we know that or there's something wrong with the month, but that really doesn't help us a lot. But when I say system dot out dot print line of month must be in range one to 12. That is definitely a useful error message. Why because it tells what's wrong excuse me here. Let me unplug my phone here so I don't get any more of these. All is all I'm doing my lecture sorry about that. Where was I, yes, it tells the user what's wrong, mainly it's out of range and also tells what the correct range is so that's a much better error message. And here in days per month for I told exactly what was wrong the month can't be greater than 12, or the month cannot be less than one. Let's go back to this one where we're happy with the message the month must be in the range one through 12. And that's what I've done. I'm getting a little bit confused here about I, I was thinking about this earlier and I my thinking led me to this point and I now have to start working without a net and doing improv so here we go. This bugs me the fact that I have a nested if statement there with the two separate cases for greater than or less than one and greater than 12. I like to combine those two if statements into one. So let's call this days per month five. And what I'm going to do is I'm going to say, if the month is greater than or equal to one, and the month is less than or equal to 12, then everything's okay. This is what is called a compound condition. So I'm going to use a compound condition with the and operator. I'm going to use two ampersands in a row to test that the month is within the range one to 12. Now I have to indent this a little bit. Again, I want to make sure everything lines up there. And this time I have to say month must be in the range one through 12, which again we think is an acceptable error message. So I'm using and to combine things here. As two can Boolean expressions, one on each side, and it gives us a true. When both expressions are true. So for example, let's say I have a three less than five and for less than six. Because both of those are true three is indeed less than five and four is indeed less than six that's going to give me true. If I say three less than five and four less than two. That's going to give me a false. They're not both true. Yeah, this one's true but that one isn't so they can't both be true. If I say three five less than three and four less than six. That's also going to give me false. Why, because five is not less than three and in fact that means I don't even have to look at this one because I know it can't they can't both be true the first ones are wrong. They can't both be right. Finally, I have my last one would be five less than three and four less than two. When both of them are false that gives me a false result. Now we can write this up as something called a truth table. If I have condition a and condition be what is the result of a and B. So if a is true and B is true, then a and B is true. True and false is false. False and true is false and false and false is false. So that's called the truth table for and the other way I can combine conditions is not just with that and operator. I can use the or operator, the or operator. And that's two vertical bars, and that will be on your back, usually on American keyboards, it's on the backslash key. The or operator, or again, has two brilliant expressions. One on each side. And it yields true. When either expression is true. For example, is three less than five, or is for less than six, I'm going to wait for that one for a moment. Let's do the other ones here is three less than five, or for less than two is at least one of them true the answer is yes. That means that comes out to true. How about five less than three that's not true but I need only one of them to be true. And that comes out to be true. And if I have five less than three. Nope. How about four less than two. Nope. That comes out to be false. Now, this is the tricky one. If I have true or true, this is both of them are true. That also means that at least one of them is true. So this comes out to be true. So let's look at our truth table, as we call it for a or B. And remember either or true and false. And finally there's one other operator that we can use. And these are called logical operators by the way. And that is the not operator, not reverses true and false. So let's say not five less than three. That comes out to be true is five less than three false what's not false true. Not three less than five comes out to be false. Why because three is less than five, but not true is false. In fact, you could actually try all of these in J cell now that I think about it. Let's for example say three less than five or three less than two. That comes out to be true. If I say not three less than five is false. Not three less than two is true. So those are logical operators and as we'll put that in the notes here for you, or and not. Those are the three main ones that we're going to be using. And as you can see here, I used it to very great effect. Or I can combine two conditions. Oh, this is an unpleasant topic, but I guess I'm going to have to talk about it in any event. And that is just as we have a priority for arithmetic operators. And as I said, this is an application has priority over addition. The logical operators also have priority order. And has priority over. I have to think about three s and his priority over or where does not fit into the hierarchy. I believe not is the most important. I'm not going to pause. Let's just go here real quick. And let's try looking for Java logical operator prior precedence. I guess precedence precedence priority same difference. Operators learning the Java language wonderful. Let's see here. And so it looks like that's interesting. Others unary is more important. Okay, so there we go. And as priority over both. I do not expect you to memorize this. I will almost certainly not put it on a test. However, the moral of the story is always use parentheses with logical operators to make sure that things combine the way you want. So for example, I could say if age is greater than 60, the 18 year and 18 and age less than or equal to 65 or the day in the week is let's say Wednesday or Thursday. And that's a lot easier for me to do than to write the whole thing out and hope hope for the best by using parentheses. I've made very clear what the order of operation is. First, I want to check to see if the age is okay. Then I want to check to see if it's either one of these days. And if either one of those becomes true, then I can do something. Sorry for that brief pause. I was thinking for a moment there. And I went back and I rewrote that first version of the days per month. Well, I just thought remember I told you there's a lot of different ways we could write it. I'll show you another one here. What we can do is again, we can say days becomes 31. In fact, we can just put it right in there. That'll work nicely. And now what I can say if the month is to we have 28 days. Otherwise, if month is April. Or month is June. Or month is September or month is November. Then we have 30 days. Get rid of all of this stuff here. And let's save this before I've I don't want to wipe out my work yet again here. Let's call this days per month six Java. And I'll print this out here. And I'll put in my compound condition here for the month, which is if the month is greater than or equal to one and the month is less than or equal to 12. Then presume there are 31 days. If month is to it's 28 if it's one of these April, June, September or November, then it's 30 days. And then print it out. Otherwise it's not in range. And that's that that's getting pretty compact here. Month nine has 30 days. Month five has 31 days. And month zero is out of range. So there's a lot of different ways that we can write this one one program. Okay, we still have leap years to do. Let's talk about leap years. Oh, before I go on to that, something very important on this that I'm going to put in the notes. Let's just write out each condition in a compound completely. So for example, I have to say age is less than 18 or greater than 65. Can't do that. I have to write it out completely ages less than 18 or age greater than 65. That's okay. In some languages, you can do something like 18 less than age less than 65. And that's okay in Python, but not in Java Java will not let you get away with that. You'd have to say 18 less than age and age less than 65 to get the same effect. In fact, I would never write it this way I would say easier than 18 and age less than 65. It's very more natural for coding. Namely this is what you'll see most of the time. You probably will not see something like this not very often. If you write it that way that's okay but not something that I would recommend. Oh leap year. Okay, here comes the fun part. Okay. A leap year a year is a leap year when ready for this. Okay. It is divisible by four, but not by 100 or it is divisible by 400. So for example if we take 2024 is divisible by four but not by 100. It's a leap year. What about 1900. It's divisible by four, but is divisible by 100 which means it's not a leap year. What about 2000. Not but is divisible. So we have to check to see if it's divisible by 400. It is so it's a leap year. So let's say we have to do this here as well. Because remember, we took 1900. It's divisible by four but is divisible by 100. So we have to check the or thing. It's not divisible by 400. So it's not a leap year. This is a weird formula. Now let's translate that into Java. What we have to say is that the year mod four is equal to zero. It's divisible by four. And also we have to make sure that the year mod 100 is not equal to zero. It's not divisible by 100. Or the year mod 400 is equal to zero. There's a formula for the leap year by translating this English here. We could also, by the way, use a logical operator to do it this way. We could say year mod four is equal to zero. And it is not the case that year mod 100 is equal to zero, which means it's not divisible by 100. The year mod 400 is equal to zero. This is why I said the parentheses are important. They become ultra important here. Okay, so for our grand finale, I guess what we're going to do here is we're going to not only ask for the month, but we're also going to ask for the year. And then we're going to take care of our leap years. So here we go. Let's save this as days per month seven. So we're going to get this. Our month is okay. Now we're going to have to ask for enter year. And we're going to have our year is going to be input dot next to zero. So we're going to have to check the year is greater than zero. Also. Now we have to check if the year is greater than zero. Because we're not going to go with negative years. That's just going to make our lives much more complicated than they have to be. That means that the years is not greater than zero. We're going to say some of that out. Again, a good error message is. Year must be greater than zero. We have to now update our description of what the program does. And this is gonna be days per month seven. We're gonna compile it. And lovely, I've reached the end of my parser. I haven't else without an if. Okay, that means somewhere along the line, I screwed up. Okay, these match up fine. I can tell because they're both blue. Oh, here's my opening parent brace and I forgot the closing brace. Again, this is a reason to keep indenting under control so you can see when things are missing. There, that's much better. And let's just check here if I have month five in the year negative 30 to 23. Year must be greater than zero, perfect. And if I say the month is six, two, and the year is 2023, 28 days. I still haven't done the leap year calculation. So month two in 2024 still says 28. Now, fixing this is going to be sort of interesting. If the month is two, then the days is 28. Unless it's a leap year. So we're going to have a nested if statement here. And we're going to copy and paste this here because I'm lazy. Then this case, the days will be 29. Otherwise, the days will be 28. Now, by the way, if I really wanted to be good about this, I might just put even these extra parentheses in here. I don't know if I'm getting too over parenthesized or not. And let's compile that and let's run it. Okay, first of all, let's try month seven in 2023. That should still work. And month seven in 2024 should have 31 days, cool. What about month two in 2023 has 28 days? Month two in 2024 has 29 days. Let's check 1900, which is not a leap year. And let's check 2000, which is a leap year. And there's our program. So wow, I told you there's a lot of different ways to write this program. I was going to go into the De Morgan laws today, but I think I'll reserve those for tomorrow. It's useful to know them, but you could go through your whole career without knowing them that once you know them, you will, and blah, blah, blah, blah, okay. Let me try and say this all in English. You could go through your whole career without knowing or using De Morgan's laws. Once you know them, you will see that there are opportunities to use them and they can help make your code more readable. So that's what I'm gonna talk about tomorrow. And I will also talk about Boolean variables and Boolean methods. And that will pretty much take care of most of what we have in chapter five. See you whenever.