2 00:00:11,118 --> 00:00:14,378 This presentation is delivered by the Stanford Center for Professional 3 00:00:14,378 --> 00:00:21,378 Development. 4 00:00:24,920 --> 00:00:28,109 All righty, so a couple things to cover real quickly. 5 00:00:28,109 --> 00:00:31,139 Last time we talked all about methods and some more about objects. There's two 6 00:00:31,138 --> 00:00:34,030 things you should know in the programs that you're going to be doing, is 7 00:00:34,030 --> 00:00:37,300 we talked a little bit about one of them last time in terms of how to get input 8 00:00:37,299 --> 00:00:40,899 from the user. There're these functions that you should know about. One is called 9 00:00:40,899 --> 00:00:42,600 READ INT 10 00:00:42,600 --> 00:00:47,010 and there's some prompt inside double quotes that you give and what that does is 11 00:00:47,009 --> 00:00:50,579 ask the user basically for an integer and gives you back some value that you can 12 00:00:50,579 --> 00:00:52,390 say, assign to an integer. 13 00:00:52,390 --> 00:00:55,920 There's also a version of this to get doubles, which surprisingly enough is 14 00:00:55,920 --> 00:00:57,510 called RE DOUBLE 15 00:00:57,509 --> 00:01:02,099 and has exactly sort of the same properties. So it's called RE DOUBLE; it 16 00:01:02,100 --> 00:01:06,189 has some string here as it's parameter or some text here in its parameter inside 17 00:01:06,189 --> 00:01:07,179 double quotes 18 00:01:07,180 --> 00:01:11,230 which it displays to the screen and then gets you back a value which is a double one you 19 00:01:11,230 --> 00:01:13,960 can assign to a double. Those are just two things off the bat that 20 00:01:13,959 --> 00:01:14,939 you should know about 21 00:01:14,939 --> 00:01:17,609 because that's how you're going to get input, at least for the time being, from the 22 00:01:17,609 --> 00:01:20,250 user in a lot of cases. Now, 23 00:01:20,250 --> 00:01:23,120 one thing you want to do once you actually get some input from the user is, you 24 00:01:23,120 --> 00:01:26,420 want to do some manipulation on it like some expressions that we talked about 25 00:01:26,420 --> 00:01:29,629 last time. We talked about some of the different operators like addition, 26 00:01:29,629 --> 00:01:33,908 subtraction or unary minus, it's the same symbol, 27 00:01:33,909 --> 00:01:36,780 multiplication, division and my favorite, the remainder. 28 00:01:36,780 --> 00:01:40,640 And so we talked about all those except for this little guy last time. 29 00:01:40,640 --> 00:01:43,858 All of the operators kind of work the way you would expect them to, 30 00:01:43,858 --> 00:01:47,280 okay. And we'll talk a little bit more about division in just a second. The 31 00:01:47,280 --> 00:01:50,590 interesting thing about division - so all of these things work with both - 32 00:01:50,590 --> 00:01:54,618 or I should say - all of these work with both integers and doubles. The 33 00:01:54,618 --> 00:01:57,329 remainder, as we talked about, only works with integers, right because it doesn't make sense 34 00:01:57,329 --> 00:02:00,159 to have a remainder when you have real values. 35 00:02:00,159 --> 00:02:03,789 These three guys work exactly the same for integers and double, just 36 00:02:03,790 --> 00:02:07,740 the way you would expect addition, multiplication, all that happy stuff, to work. 37 00:02:07,739 --> 00:02:11,068 Division kind of rears its ugly head because it actually works slightly 38 00:02:11,068 --> 00:02:15,888 differently if you're doing division for integers versus doubles. 39 00:02:15,889 --> 00:02:20,069 Okay? The whole point of that is, if you're doing a division 40 00:02:20,068 --> 00:02:24,238 and the two arguments that you're dividing, right if both of these things are 41 00:02:24,239 --> 00:02:27,679 integers; in this case I have integer constant which is what I mean, the values, 42 00:02:27,679 --> 00:02:28,598 right. 43 00:02:28,598 --> 00:02:32,058 If both of these integers, what it does is integer division which means it does 44 00:02:32,058 --> 00:02:33,408 the division and 45 00:02:33,408 --> 00:02:34,949 throws away any remainder. 46 00:02:34,949 --> 00:02:38,469 So what you get back is an integer. So 5 divided by 2 when these are 47 00:02:38,469 --> 00:02:42,049 integers gives you back the Value 2. That little remainder thing is just gone. 48 00:02:42,049 --> 00:02:45,088 If you want to get the remainder you use this guy. Okay? 49 00:02:45,088 --> 00:02:50,009 If either one of these particular values happens to be a real value, like a double, 50 00:02:50,009 --> 00:02:54,079 then it will do real-value division and give you back a real value. So if you 51 00:02:54,080 --> 00:02:55,209 happen to divide 52 00:02:55,209 --> 00:02:59,799 5, even if 5 is an integer, by the Value 2.0 and so it knows 53 00:02:59,799 --> 00:03:02,620 it's a real value because it's got a decimal point in it, 54 00:03:02,620 --> 00:03:05,370 this will give you back 2.5 55 00:03:05,370 --> 00:03:08,269 as a double and so you can assign that to a double. Okay? 56 00:03:08,269 --> 00:03:12,289 So if either one of the arguments is a double, you get real-value division; if 57 00:03:12,289 --> 00:03:18,709 they're both integers, you get back the integer portion. Un huh? I'm a little confused about the double; the double 58 00:03:18,709 --> 00:03:22,009 is just a real number? It's just a real number. Yes. So 59 00:03:22,008 --> 00:03:25,888 another thing that kind of comes up when you do expressions - yeah, 60 00:03:25,889 --> 00:03:28,199 sometimes you're taking notes and you just don't know; it's like 61 00:03:28,199 --> 00:03:30,959 candy raining from the sky. 62 00:03:30,959 --> 00:03:34,039 The other thing to keep in mind is just like arithmetic, 63 00:03:34,039 --> 00:03:37,750 sometimes you want operators to evaluate in different order. There's an order 64 00:03:37,750 --> 00:03:41,000 precedent for how these things actually evaluate in case you have to have some 65 00:03:41,000 --> 00:03:42,558 big honking expression. 66 00:03:42,558 --> 00:03:45,609 The order of precedent is you can have parentheses. Parentheses are the 67 00:03:45,609 --> 00:03:48,969 highest precedent. That means you evaluate everything in parentheses first, 68 00:03:48,968 --> 00:03:53,209 then multiplication, division and the remainder operator have the same level 69 00:03:53,209 --> 00:03:56,039 of precedents. And so if you have multiple of them; they're evaluated 70 00:03:56,038 --> 00:03:57,339 from left to right 71 00:03:57,340 --> 00:04:00,359 and then addition and subtraction. Again, if you have multiple, 72 00:04:00,359 --> 00:04:04,370 evaluate left to right. So it's just like regular rules of precedent in 73 00:04:04,370 --> 00:04:06,239 algebra, which hopefully you're familiar with, 74 00:04:06,239 --> 00:04:09,430 but to make that concrete let's say you have some integer X 75 00:04:09,430 --> 00:04:15,040 and we say X equals 1 plus 3 times 5 divided by 2. 76 00:04:15,039 --> 00:04:18,579 How does that actually evaluate? Well first of all, we say do we have any parens? 77 00:04:18,579 --> 00:04:21,658 No we don't have any parens. That would be the highest level of precedence. You can always 78 00:04:21,658 --> 00:04:23,938 force something to evaluate more 79 00:04:23,939 --> 00:04:25,580 highly by putting it in parens. 80 00:04:25,579 --> 00:04:29,149 So these guys are all at the same level, so we evaluate left to right. So we come 81 00:04:29,149 --> 00:04:31,579 across and we say here, here's multiplication, 82 00:04:31,579 --> 00:04:35,538 we evaluate this thing as 15, right? We don't do this addition first; this 83 00:04:35,538 --> 00:04:39,728 3 times 5 becomes 15. Then we divide it by 2. And, 84 00:04:39,728 --> 00:04:43,149 you remember what I just said? Hey, this is an integer, this is an integer so this is 85 00:04:43,149 --> 00:04:47,620 integer division, so 15 divided by 2 in integer division is? Seven. 86 00:04:47,620 --> 00:04:48,680 Seven. Rock on. 87 00:04:48,680 --> 00:04:52,629 So this whole thing is 7 and then we add the 1 to it because addition has 88 00:04:52,629 --> 00:04:56,079 the lowest level of precedence of all the operators here and what we get at the end of the 89 00:04:56,079 --> 00:04:58,500 day is that X is equal to 8. 90 00:04:58,500 --> 00:05:01,649 Okay, hopefully you can see that and if you can't, I we'll just tell you X is equal to 8. 91 00:05:01,649 --> 00:05:04,228 So those are the rules of precedent. They're exactly the same as, hopefully 92 00:05:04,228 --> 00:05:09,928 you know from algebra. Un huh? Isn't it true [inaudible] should be in parentheses? 93 00:05:09,928 --> 00:05:13,628 It's nice to clarify; it's nice to always put in parentheses. Sometimes you have to put in 94 00:05:13,629 --> 00:05:16,329 parentheses to get the right thing to compute and I'll show you that in just a second, but 95 00:05:16,329 --> 00:05:19,230 it's nice now to just to fully parenthesize everything. Uh huh? 96 00:05:19,230 --> 00:05:21,408 Where do exponents fall in there? 97 00:05:21,408 --> 00:05:24,408 There is no built-in exponent operator. Okay? 98 00:05:24,408 --> 00:05:28,118 So if you're sort of like a mat-lab person or something like that there're 99 00:05:28,119 --> 00:05:30,830 functions that we'll get to later on that compute exponents but there's no 100 00:05:30,829 --> 00:05:34,348 build-in primitive operation for exponents. Okay? 101 00:05:34,348 --> 00:05:35,408 So 102 00:05:35,408 --> 00:05:39,519 with that said, sometimes there's times in life when you say hey, but Marilyn, 103 00:05:39,519 --> 00:05:43,468 I have these two integers but I really want to get some 104 00:05:43,468 --> 00:05:44,579 value back 105 00:05:44,579 --> 00:05:46,939 which is some real value. Right? 106 00:05:46,939 --> 00:05:50,990 So what you can do is - let's say I have INT X, okay? 107 00:05:50,990 --> 00:05:56,329 And let me give X some initial value; so I can say X equals 108 00:05:56,329 --> 00:06:00,800 5. Then I want to take like half of that and assign it to some double Y. 109 00:06:00,800 --> 00:06:03,778 So if I say double Y equals 110 00:06:03,778 --> 00:06:08,158 X divided by 2, you might think, hey, this is a double right, isn't it going to do the 111 00:06:08,158 --> 00:06:11,459 right thing and give me back 2.5? No. 112 00:06:11,459 --> 00:06:15,288 It evaluates the right hand side first and then assigns it for left hand side. 113 00:06:15,288 --> 00:06:17,509 So you have 5 here. That's an integer. 114 00:06:17,509 --> 00:06:21,639 This 2 is also an integer. It does integer division which means you get 2. 115 00:06:21,639 --> 00:06:25,240 Once you get that 2 it says hey, but that two into a double and 116 00:06:25,240 --> 00:06:26,269 117 00:06:26,269 --> 00:06:28,588 says, okay, it's 2.0. So you're like, huh? That was totally weird, 118 00:06:28,588 --> 00:06:31,329 but that's because it was doing integer division. So what you need to do is, you 119 00:06:31,329 --> 00:06:33,089 need to tell it - you need to say, 120 00:06:33,089 --> 00:06:35,638 I want you to do real-value division 121 00:06:35,639 --> 00:06:40,079 by temporarily treating one of these things as a double. 122 00:06:40,079 --> 00:06:43,939 That's something that we refer to at a cast. It's kind of like you were making a 123 00:06:43,939 --> 00:06:46,839 movie and you need to come up with a cast and you know, you get someone who's going to 124 00:06:46,839 --> 00:06:50,008 like, play Harry Potter for you, but he doesn't really wear glasses so you say, 125 00:06:50,009 --> 00:06:52,310 for the purpose of being in the cast, 126 00:06:52,310 --> 00:06:54,209 I'm going to put some glasses on you. 127 00:06:54,209 --> 00:06:57,009 For this one thing, you're going to wear glasses and then we're going to take them 128 00:06:57,009 --> 00:06:59,949 off. It doesn't change intrinsically who you are, 129 00:06:59,949 --> 00:07:03,709 it just makes you appear different for this one operation. 130 00:07:03,709 --> 00:07:04,899 And the way we do that 131 00:07:04,899 --> 00:07:08,539 is we specify the type that we want to cast to 132 00:07:08,540 --> 00:07:11,189 in front of the thing that we want to cast. So 133 00:07:11,189 --> 00:07:13,579 we would say DOUBLE 134 00:07:13,579 --> 00:07:15,658 X divided by 135 00:07:15,658 --> 00:07:17,430 2. All this means is, 136 00:07:17,430 --> 00:07:20,790 take this thing, which is not normally a double, and for the purpose of this 137 00:07:20,790 --> 00:07:23,580 operation, treat it as though it were a double. 138 00:07:23,579 --> 00:07:27,849 It does not change X from being an integer intrinsically; X remains an integer 139 00:07:27,850 --> 00:07:28,960 after this operation, 140 00:07:28,959 --> 00:07:32,500 but now it becomes 5.0 and when we do the division, we do real-value division, 141 00:07:32,500 --> 00:07:35,668 we get 2.5 and that's what goes into Y. Okay? 142 00:07:35,668 --> 00:07:37,149 So that's what's called a cast. 143 00:07:37,149 --> 00:07:40,778 You can also do that, interestingly enough - let's say you did that and 144 00:07:40,778 --> 00:07:42,028 now Y 145 00:07:42,028 --> 00:07:44,848 is some box somewhere that has the value 2.5 in it and you say, you 146 00:07:44,848 --> 00:07:47,519 know what, I really like the integer part of Y. 147 00:07:47,519 --> 00:07:52,278 And so I'm going to have some integer Z and I'm just going to set that to be 148 00:07:52,278 --> 00:07:56,459 equal to what I would get if I cast Y to be an integer. Well, if I 149 00:07:56,459 --> 00:07:59,969 cast Y to be an integer, you might say, oh Marilyn, does it round? Like do I get 150 00:07:59,970 --> 00:08:03,280 3 because I remember if it's a .5 we round up, that was always the way it 151 00:08:03,279 --> 00:08:05,888 was when life was good. No. 152 00:08:05,889 --> 00:08:08,819 You don't round up. As a matter of fact, it could be 2.9; it could be 153 00:08:08,819 --> 00:08:12,009 2.9999, you still don't round up. 154 00:08:12,009 --> 00:08:15,610 This is computer science. It's not necessarily forgiving. We always round 155 00:08:15,610 --> 00:08:19,819 down. You take the integer of a real value, it truncates it; it drops anything after 156 00:08:19,819 --> 00:08:22,310 the decimal point; it doesn't matter how big it is, 157 00:08:22,310 --> 00:08:24,889 sorry. It's just like the dollar; we just devalued your 158 00:08:24,889 --> 00:08:26,509 currency, right. It just - 159 00:08:26,509 --> 00:08:31,069 drop everything after the decimal point. That's life in the city. Okay? So, 160 00:08:31,069 --> 00:08:33,340 if we go to the computer for a second, 161 00:08:33,340 --> 00:08:36,389 we can put this all together in a little program and actually see what's going on. 162 00:08:36,389 --> 00:08:36,789 So 163 00:08:36,789 --> 00:08:40,409 I wrote a little program that averages two numbers and it's running right now. I'll show 164 00:08:40,409 --> 00:08:42,819 you the text for that program over here. 165 00:08:42,820 --> 00:08:46,250 So here's some program average two integers. I say this program averages 166 00:08:46,250 --> 00:08:49,649 two numbers; I read in one number from the user; I read in another integer from the 167 00:08:49,649 --> 00:08:50,100 user 168 00:08:50,100 --> 00:08:53,170 and then I come here, and notice I commented for you that it's buggy, I say hey, 169 00:08:53,169 --> 00:08:56,509 the average is just adding the two together and diving by 2 right? That's 170 00:08:56,509 --> 00:08:57,450 what I remember 171 00:08:57,450 --> 00:09:00,730 with a mathematical expression for average of two numbers when I looked it 172 00:09:00,730 --> 00:09:04,879 up in my book of mathematical expressions for averaging two numbers. 173 00:09:04,879 --> 00:09:06,529 And then I write out the average. 174 00:09:06,529 --> 00:09:12,449 So what's the problem here? Uh huh? The [inaudible]. 175 00:09:12,450 --> 00:09:15,350 Yeah, so there's two problems here. 176 00:09:15,350 --> 00:09:18,800 Da da daa. And then we reveal the comment and there they are. 177 00:09:18,799 --> 00:09:21,569 First, we need to parenthesize the expression because 178 00:09:21,570 --> 00:09:25,278 in terms of precedent, the division has precedent over the 179 00:09:25,278 --> 00:09:28,309 addition. So if we were to say take the average of 5 and 6, 180 00:09:28,309 --> 00:09:31,109 we don't get 5.5, we get 8. 181 00:09:31,110 --> 00:09:35,149 Why? Because it took half of 6; it took that 6 and divided it by 2 182 00:09:35,149 --> 00:09:37,559 and then added it to the 5. So 183 00:09:37,559 --> 00:09:38,859 it's actually at - 184 00:09:38,860 --> 00:09:41,240 doing this operation over here first 185 00:09:41,240 --> 00:09:45,029 and we want to say, hey, add some parentheses to force the addition to happen first. 186 00:09:45,029 --> 00:09:48,629 Now ever after we force the addition, this whole expression here 187 00:09:48,629 --> 00:09:51,179 is still going to be an integer because it adds two integers, that's just going to 188 00:09:51,179 --> 00:09:53,109 give us an integer back 189 00:09:53,110 --> 00:09:56,680 and then we divide that by an integer. There's two ways we can fix this. One is, 190 00:09:56,679 --> 00:10:00,519 we can change the 2 to a 2.0, so we can explicitly say that that 191 00:10:00,519 --> 00:10:03,909 constant in there that we're dividing by is real value and that will force real-value 192 00:10:03,909 --> 00:10:04,519 division. 193 00:10:04,519 --> 00:10:08,529 Another way we can do it is to say treat this whole thing as a double for 194 00:10:08,529 --> 00:10:11,589 this operation. So after you add those numbers together you get something which 195 00:10:11,590 --> 00:10:15,889 is the total of those two numbers, treat that total as though it were a double and 196 00:10:15,889 --> 00:10:18,269 then do the division. So 197 00:10:18,269 --> 00:10:19,480 if we do that, 198 00:10:19,480 --> 00:10:26,000 then we're good to go. Uh huh? 199 00:10:26,000 --> 00:10:30,200 If you put a double in there, do you still need that other double before you add it? If I put a double in where? Where it is now is now, [inaudible]. Uh huh, 200 00:10:30,200 --> 00:10:33,750 yeah because this is the type of average. Right? So if average is not - I need to 201 00:10:33,750 --> 00:10:36,159 declare average first of all; I need to give it a type. 202 00:10:36,159 --> 00:10:39,189 If I don't give it a type double it can't store a real number anyway. 203 00:10:39,190 --> 00:10:42,040 So that's what that double's about. Uh huh? If you cast a double can you expect everything 204 00:10:42,039 --> 00:10:44,789 after to double on the line? 205 00:10:44,789 --> 00:10:48,980 Yeah, it affects the most immediate thing after it. If it's parenthesized, it's a parenthesized 206 00:10:48,980 --> 00:10:55,980 expression; it's just one variable, it's just that variable. Uh huh? If the user enters 207 00:10:56,690 --> 00:11:00,090 a double into the computer, what difference would it make? Ah good questions because someone wrote this little read integer for us so let's 208 00:11:00,090 --> 00:11:01,899 run it and see what happens because it's just that 209 00:11:01,899 --> 00:11:04,720 smart. 210 00:11:04,720 --> 00:11:08,379 So here's - and one, we're like hey, here's 5.5, I want 5.5 and it 211 00:11:08,379 --> 00:11:12,129 says illegal numerical format. Right? It has a big cow; it's in red. 212 00:11:12,129 --> 00:11:15,250 Right? All that means is, I want an integer; you didn't give me an integer. Give it 213 00:11:15,250 --> 00:11:20,200 to me again and notice it prompts you again and you're like, okay, sorry. I meant the letter A. It's 214 00:11:20,200 --> 00:11:23,520 still illegal in numeric format. I would do that with like my friend's computer; like you 215 00:11:23,519 --> 00:11:26,819 know, they write their programs and they didn't do like, nice error checking or whatever that all the 216 00:11:26,820 --> 00:11:29,620 READ INT stuff gives you and they're like, give me an integer and I'd be like, how about 217 00:11:29,620 --> 00:11:30,720 A. 218 00:11:30,720 --> 00:11:32,829 And they'd be like, we'll that's not an integer and I'm like, 219 00:11:32,828 --> 00:11:37,829 to me it is. I'd put it in and watch their program crash and, you know, I 220 00:11:37,830 --> 00:11:39,940 was mean. 221 00:11:39,940 --> 00:11:43,700 Yeah, there's 5.5, it's working. You laugh now, wait until I do it to 222 00:11:43,700 --> 00:11:45,580 you. 223 00:11:45,580 --> 00:11:52,580 Uh huh? [Inaudible]. Pardon: 224 00:11:52,789 --> 00:11:55,699 [Inaudible]. There's something you can do in java having to do with formatting. We're not 225 00:11:55,700 --> 00:11:58,220 going to get into that now. If you're really interested, you could come talk to 226 00:11:58,220 --> 00:12:00,879 me afterward. Oh, , 227 00:12:00,879 --> 00:12:06,980 that's going to be the goal for this class; to see if I can get one to stay up on the camera. All right, so 228 00:12:06,980 --> 00:12:10,769 with that said, hopefully you kind of understand the notices of castings for an 229 00:12:10,769 --> 00:12:14,129 operation and the rules of precedence that are actually involved. 230 00:12:14,129 --> 00:12:20,639 There's a couple shorthands you should also know. Uh huh? [Inaudible] can I skip the top [inaudible]? 231 00:12:20,639 --> 00:12:24,419 In that particular case with Z, you can skip the cast because if it says, hey if you have 232 00:12:24,419 --> 00:12:26,748 Y here and you want to assign it to an INT, 233 00:12:26,749 --> 00:12:30,359 the only way it can do that is to cast it automatically to be an INT, so it will do it for 234 00:12:30,359 --> 00:12:37,359 you automatically. The site, the part that I want it to 235 00:12:39,139 --> 00:12:42,779 return it contains much information [inaudible]? Yeah, and so over here if you add a double and you try to assign it to an INT 236 00:12:42,779 --> 00:12:45,809 it just - you can't because it doesn't have space; it doesn't store what's after the 237 00:12:45,809 --> 00:12:47,939 decimal so it just truncates it for you. Yeah. 238 00:12:47,940 --> 00:12:51,530 I just put INT to explicit about the truncation. 239 00:12:51,529 --> 00:12:54,679 There's a couple other things we want to do which are shorthands for arithmetical 240 00:12:54,679 --> 00:12:58,169 expressions because there are some arithmetical expressions - arithmetical, 241 00:12:58,169 --> 00:12:59,629 is that even a word? 242 00:12:59,629 --> 00:13:01,350 Arithmetic expressions - you've 243 00:13:01,350 --> 00:13:04,360 gotta keep me honest, sometimes I just make up words. I'll tell you stores about that 244 00:13:04,360 --> 00:13:06,800 at some point, but not right now. 245 00:13:06,799 --> 00:13:10,559 Let's say we have INT X, our old friend, after I told you all this stuff - good variable name, 246 00:13:10,559 --> 00:13:12,859 everything's X. 247 00:13:12,860 --> 00:13:16,080 So let's say we say X equals 3. Now there's a lot of things sometimes you 248 00:13:16,080 --> 00:13:19,350 want to do with X. Something that we want to do a lot is to add 1 to X. 249 00:13:19,350 --> 00:13:23,960 Right, so if we want it to add one to X, how do we do that? 250 00:13:23,960 --> 00:13:25,099 Some people already know. 251 00:13:25,099 --> 00:13:26,759 We'll do it the long way. 252 00:13:26,759 --> 00:13:28,519 X equals X plus 253 00:13:28,519 --> 00:13:31,259 1. That adds 1 to X and stores it back in X. Turns out, 254 00:13:31,259 --> 00:13:35,089 this notion of taking a variable, adding something to it and storing it back to itself is 255 00:13:35,089 --> 00:13:39,149 very common and so there's shorthand for doing that which is X plus 256 00:13:39,149 --> 00:13:42,979 equals and then some value. So if I change this, say to a 5, and I said X equals 257 00:13:42,979 --> 00:13:47,169 X plus 5, that's the same thing as saying X plus equals 5. It just means 258 00:13:47,168 --> 00:13:48,240 take that value 259 00:13:48,240 --> 00:13:52,039 that's over here and add it to X and store it back into X. 260 00:13:52,039 --> 00:13:55,389 In the case of adding 1, it's not adding 1 because often times you want 261 00:13:55,389 --> 00:13:59,279 to count. It's something we do so often there's even a special shorthand 262 00:13:59,279 --> 00:14:00,730 just for adding 1. 263 00:14:00,730 --> 00:14:05,250 Which, is X plus, plus. This may look a little familiar to you from the Karel world. 264 00:14:05,250 --> 00:14:08,850 You're like, oh, I remember I plus plus, is that the same thing? Yeah, you were adding 1 to 265 00:14:08,850 --> 00:14:10,389 I in Karel's world. 266 00:14:10,389 --> 00:14:11,840 Here you're adding 1 to X. 267 00:14:11,840 --> 00:14:15,310 So there's no spaces here or anything, X plus plus just means add 1 to 268 00:14:15,309 --> 00:14:19,079 X. If you've ever head of language C and C plus plus, that's where C plus plus 269 00:14:19,080 --> 00:14:21,980 gets its name from. They sort of started with C and the made it a little bit better by doing 270 00:14:21,980 --> 00:14:24,330 the plus plus. Okay? 271 00:14:24,330 --> 00:14:27,920 You can also do sort of subtraction in a similar way. So if you want to subtract 1 272 00:14:27,919 --> 00:14:30,449 from X, that's the longhand form of doing it, 273 00:14:30,450 --> 00:14:34,509 you can say X minus equals 1, which is a slightly shorter hand and there's a 274 00:14:34,509 --> 00:14:38,659 complete shorthand for it which is X minus minus, which means subtract 1 from X 275 00:14:38,659 --> 00:14:40,889 and store it back in X. Okay? 276 00:14:40,889 --> 00:14:43,139 There's a couple other shorthands, you can also 277 00:14:43,139 --> 00:14:46,210 think about multiplication. So if you want to say X 278 00:14:46,210 --> 00:14:50,660 equals X times 2, like you want a double 2, you could say X times 279 00:14:50,659 --> 00:14:55,079 equals 2. Same sort of effect, sort of a times equal. There is no super shorthand for, 280 00:14:55,080 --> 00:14:57,560 you know, times equals 2, it just stops there. 281 00:14:57,559 --> 00:15:02,138 There is also divide equal, as you can image; so X equals X divided by 2, X divide equal 2. 282 00:15:02,139 --> 00:15:06,289 You know, the value here is basically the value there, so if 283 00:15:06,289 --> 00:15:10,370 you want to multiply it by 5, you would just put a 5 there, same kind of thing. But these 284 00:15:10,370 --> 00:15:12,389 little shorthands - you can use these with integers, 285 00:15:12,389 --> 00:15:13,610 you can use them with doubles, 286 00:15:13,610 --> 00:15:16,879 they work for both, you're fine. Okay? 287 00:15:16,879 --> 00:15:20,189 So besides the shorthands, these are just some little syntactic things you should 288 00:15:20,190 --> 00:15:24,200 know, right. Hopefully all these things are fairly clear and they make sense. 289 00:15:24,200 --> 00:15:27,690 There's also a notion, kind of time for a new concept; 290 00:15:27,690 --> 00:15:30,510 the notion of a constant. 291 00:15:30,509 --> 00:15:32,899 I'll write it in big letters. 292 00:15:32,899 --> 00:15:36,639 And the idea behind the constant is it differentiates it for a variable and 293 00:15:36,639 --> 00:15:41,759 that it does not vary, right. Variables vary, constants remain the same. 294 00:15:41,759 --> 00:15:45,389 And so you might wonder why do I care about what is a constant, right? Like 295 00:15:45,389 --> 00:15:48,230 isn't the value like 3 here a constant? Yeah 296 00:15:48,230 --> 00:15:50,110 but some constants are meaningful 297 00:15:50,110 --> 00:15:54,190 and sometimes you want to give them a name. So for example, PI. Right? 298 00:15:54,190 --> 00:15:58,200 PI is some constant that has a value like - let's just say for right now, 3.14. 299 00:15:58,200 --> 00:16:02,509 So in your program, you could have some double called a PI, all upper case 300 00:16:02,509 --> 00:16:05,659 that's the conventional use for constant, separate words 301 00:16:05,659 --> 00:16:07,909 have underscores to differentiate them, 302 00:16:07,909 --> 00:16:09,409 equals 3.4. 303 00:16:09,409 --> 00:16:12,870 And you might say, oh that's great, now anywhere in my program, like if I'm computing like, you 304 00:16:12,870 --> 00:16:16,029 know, the area of a circle and it's PI R squared, right, 305 00:16:16,029 --> 00:16:17,848 I could just say something like, 306 00:16:17,849 --> 00:16:23,189 take the radius, multiply it by PI, multiple it by PI again and this would be, 307 00:16:23,188 --> 00:16:26,849 you know, my - assuming I had R somewhere and I have area, 308 00:16:26,850 --> 00:16:30,590 I could compute it like that. And oh, isn't that so good. My program's readable now, 309 00:16:30,590 --> 00:16:32,240 it's so good, I know - yeah, 310 00:16:32,240 --> 00:16:35,169 that's all wrong. . 311 00:16:35,169 --> 00:16:37,870 No one says anything, like sometimes I've gotta throw things at you to be 312 00:16:37,870 --> 00:16:38,698 like, is 313 00:16:38,698 --> 00:16:39,789 anyone paying attention? 314 00:16:39,789 --> 00:16:42,649 Yeah, we just squared PI. 315 00:16:42,649 --> 00:16:47,110 PI R squared, remember that? Yeah, there's R times R times PI. 316 00:16:47,110 --> 00:16:48,909 I can just do this 317 00:16:48,909 --> 00:16:51,959 for computing the area and everything's just fine, right? 318 00:16:51,960 --> 00:16:54,709 And it makes it a little bit more readable. But the problem is 319 00:16:54,708 --> 00:16:58,299 PI is a variable here right? Nothing prevents someone from coming along and 320 00:16:58,299 --> 00:17:01,089 saying, yeah, you know what, I don't really like your PI. 321 00:17:01,090 --> 00:17:06,799 I like my own PI and my PI is 6 because it's just bigger. Right? 322 00:17:06,799 --> 00:17:10,109 Then you go compute the area and your area got a whole lot bigger and you're like, why's it so much bigger? I 323 00:17:10,109 --> 00:17:11,698 don't understand. 324 00:17:11,699 --> 00:17:14,170 Right? Because someone changed your PI. 325 00:17:14,170 --> 00:17:16,870 Get your hands off my PI, 326 00:17:16,869 --> 00:17:19,639 all right. The way you want to do that is, you want to force 327 00:17:19,640 --> 00:17:23,560 PI to not change. You want to tell the machine this is a constant. I will give you a 328 00:17:23,559 --> 00:17:25,428 value, it will remain unchanged. 329 00:17:25,429 --> 00:17:29,139 And the way we do that just happens to have some sort of bulky syntax 330 00:17:29,138 --> 00:17:29,898 associated with it, 331 00:17:29,898 --> 00:17:32,750 but I'll show you what that looks like. 332 00:17:32,750 --> 00:17:33,329 Okay. 333 00:17:33,329 --> 00:17:35,109 So the way we say that - 334 00:17:35,109 --> 00:17:36,928 sort of follow along. 335 00:17:36,929 --> 00:17:40,919 First, we say private because we want to keep our constants to ourselves. So what 336 00:17:40,919 --> 00:17:44,140 private means is this constant I'm going to define, I'm going to define the constant 337 00:17:44,140 --> 00:17:45,330 in a class. 338 00:17:45,329 --> 00:17:49,119 It's not defined in a - you can define one in a particular method, but generally we 339 00:17:49,119 --> 00:17:51,478 define our constants in our entire class - 340 00:17:51,479 --> 00:17:53,559 I'll so you where they go in just a second - 341 00:17:53,559 --> 00:17:57,349 and we say private because we don't want anyone to be able to see our constants 342 00:17:57,349 --> 00:17:59,730 outside of our class. So first, we say private. 343 00:17:59,730 --> 00:18:01,710 Then we say static, 344 00:18:01,710 --> 00:18:03,690 which is just kind of a funky word which means 345 00:18:03,690 --> 00:18:07,299 that this constant sort of lives for the class and there's only one of these for 346 00:18:07,299 --> 00:18:09,049 the WHILE class. So it's not like - 347 00:18:09,049 --> 00:18:12,279 you, if you're an object for the class, you don't have your own version of PI and 348 00:18:12,279 --> 00:18:14,889 some other object has their own version of PI. The way you can think it is, remember I 349 00:18:14,890 --> 00:18:15,620 told you 350 00:18:15,619 --> 00:18:17,428 a couple days ago you're all objects? 351 00:18:17,429 --> 00:18:21,540 You're all objects and we have one constant, which is the amount of 352 00:18:21,539 --> 00:18:24,149 mass in the world, or universe, right. 353 00:18:24,150 --> 00:18:27,780 And so you don't have a separate mass and I'm like, hey, I have my different amount of 354 00:18:27,779 --> 00:18:31,250 mass for the universe and it's different from your amount of mass for the universe. We 355 00:18:31,250 --> 00:18:33,930 share the same mass for the universe. So all of 356 00:18:33,930 --> 00:18:36,360 humanity, or you could think of all objects that exists 357 00:18:36,359 --> 00:18:38,229 share the same mass for the universe. 358 00:18:38,230 --> 00:18:41,740 That's what static means. Everything of a class shares the same thing. You don't 359 00:18:41,740 --> 00:18:45,900 have - each object doesn't have its own version. Okay? Then, 360 00:18:45,900 --> 00:18:48,070 we say final, 361 00:18:48,069 --> 00:18:49,200 which means, 362 00:18:49,200 --> 00:18:52,740 get your hands off my PI. I'm going to give you a value and it's the final value. No one else is going to be able to give you a 363 00:18:52,740 --> 00:18:55,470 value. No one else is going to be able to give you a value to this because the value I give 364 00:18:55,470 --> 00:18:57,210 you know is the final value. 365 00:18:57,210 --> 00:18:59,279 Then, we specify the type. 366 00:18:59,279 --> 00:19:02,410 Right? So we'd have DOUBLE for PI 367 00:19:02,410 --> 00:19:05,590 and then if we can zoom out a little bit this is going to be a two-board 368 00:19:05,589 --> 00:19:07,819 extravaganza because it's just that big. 369 00:19:07,819 --> 00:19:12,779 Public, or private, static, final, double, name of the constant PI and then its 370 00:19:12,779 --> 00:19:14,230 value 371 00:19:14,230 --> 00:19:16,960 3.14. 372 00:19:16,960 --> 00:19:18,730 So all of that means 373 00:19:18,730 --> 00:19:22,390 you're going to get something who's of type double, it's value's not going to 374 00:19:22,390 --> 00:19:26,110 change after I give its initial value, there's only one of these for the entire 375 00:19:26,109 --> 00:19:30,729 class and it only lives inside the class, but we just put all those things there 376 00:19:30,730 --> 00:19:32,650 just so you know. Okay? 377 00:19:32,650 --> 00:19:35,990 Now the important thing about doing this is you want to give your constants good 378 00:19:35,990 --> 00:19:36,828 names 379 00:19:36,828 --> 00:19:40,668 and - well, good names is one reason, right, so when someone reads your 380 00:19:40,669 --> 00:19:44,040 program they can say oh, he's multiplying by PI, that's a good time. 381 00:19:44,039 --> 00:19:47,430 The other thing is that it allows the program to change easily. Right? So I could 382 00:19:47,430 --> 00:19:50,529 have an area computed somewhere; I could have could have circumference computed somewhere else 383 00:19:50,529 --> 00:19:51,609 that also uses PI. 384 00:19:51,609 --> 00:19:54,579 I could go somewhere else and do something somewhere else funky with PI 385 00:19:54,579 --> 00:19:58,480 and then someone comes along and like some physicist comes alone and sees my program and says, 386 00:19:58,480 --> 00:20:01,779 oh, you must be an engineer because you're like 3.14. Like in 387 00:20:01,779 --> 00:20:04,859 physics, if we say 3.14, like particles miss each other when we accelerate 388 00:20:04,859 --> 00:20:06,069 them to the speed of light. 389 00:20:06,069 --> 00:20:08,879 You're like, well, don't accelerate particles to the speed of light. 390 00:20:08,880 --> 00:20:13,000 But they come along and they say, no, no, no, no. PI is 391 00:20:13,000 --> 00:20:14,400 392 00:20:14,400 --> 00:20:15,300 393 00:20:15,299 --> 00:20:18,000 3.14159622, there's always a 2 before the 394 00:20:18,000 --> 00:20:19,140 395 00:20:19,140 --> 00:20:20,950 6. I'll stop there. 396 00:20:20,950 --> 00:20:21,590 397 00:20:21,589 --> 00:20:23,949 I have a friend who actually has PI memorized 398 00:20:23,950 --> 00:20:25,410 the 400 digits. 399 00:20:25,410 --> 00:20:29,850 I was like, that was just a tremendous waste of your time. . 400 00:20:29,849 --> 00:20:33,399 But that's okay, I'm sure it's probably useful somewhere. 401 00:20:33,400 --> 00:20:36,350 If I really need to know, I'll go ask him or I'll write a program to 402 00:20:36,349 --> 00:20:37,740 compute it. 403 00:20:37,740 --> 00:20:39,970 So we can get more exact 404 00:20:39,970 --> 00:20:43,150 values of PI, right. 405 00:20:43,150 --> 00:20:45,840 The other thing that kind of comes up is that now, 406 00:20:45,839 --> 00:20:49,230 if I use PI all throughout my program, I'll I need to do is change it in that one 407 00:20:49,230 --> 00:20:52,180 place and I get more precision all throughout my program. So that's one 408 00:20:52,180 --> 00:20:55,650 thing in terms of nice software engineering, right? It changes everywhere 409 00:20:55,650 --> 00:20:59,000 so I don't need to go through and hunt down, oh, where did I put 3.14 in my program and 410 00:20:59,000 --> 00:21:00,700 I have no inconsistencies 411 00:21:00,700 --> 00:21:03,340 because if everyone's referring to that same constant value, they're all 412 00:21:03,339 --> 00:21:04,909 referring to the same thing. 413 00:21:04,910 --> 00:21:07,100 The other thing you want to keep in mind, 414 00:21:07,099 --> 00:21:10,500 in terms of good names is think about names that are 415 00:21:10,500 --> 00:21:11,140 readable. 416 00:21:11,140 --> 00:21:14,630 No joke, I actually worked on a program for a large corporation once that will remain 417 00:21:14,630 --> 00:21:15,490 nameless, 418 00:21:15,490 --> 00:21:22,490 where this constant was in the program. I don't 419 00:21:23,579 --> 00:21:26,730 know, he had some extra stuff there and I looked at it and I was like, 420 00:21:26,730 --> 00:21:29,679 yeah, that's very descriptive. 421 00:21:29,679 --> 00:21:32,920 72 equals 72 and I was like, I was really tempted, I 422 00:21:32,920 --> 00:21:34,900 just wanted to come along and be like, 423 00:21:34,900 --> 00:21:39,960 la, la, la, la,la. Like, 424 00:21:39,960 --> 00:21:43,460 does the universe end? Like what would happen? I have no idea where this 425 00:21:43,460 --> 00:21:44,850 is getting used. 426 00:21:44,849 --> 00:21:48,990 And it turned out, what this - anyone want to venture a guess what this was used for? 427 00:21:48,990 --> 00:21:53,370 It's the number of pixels in an inch on most monitor resolutions. That's what 428 00:21:53,369 --> 00:21:56,709 it was. It was like this gooey program, or user-interface program 429 00:21:56,710 --> 00:22:00,000 and that's what this was because someone somewhere in some class had told them, 430 00:22:00,000 --> 00:22:03,769 hey, if you're going to have any values in your program other than 0 and 1, 431 00:22:03,769 --> 00:22:04,798 which are very common 432 00:22:04,798 --> 00:22:07,900 and occasionally some other value will come up, like you want to divide by 2, it 433 00:22:07,900 --> 00:22:09,019 doesn't make sense to say 434 00:22:09,019 --> 00:22:12,139 this is the thing I use to divide by 2 and its value is 2, right. 435 00:22:12,140 --> 00:22:15,110 There's some values that come in your program that it's fine to just have the 436 00:22:15,109 --> 00:22:16,439 actual number in there. 437 00:22:16,440 --> 00:22:20,298 But other than these things, most numbers that appear in a program actually have 438 00:22:20,298 --> 00:22:23,429 some meaning and you want to have them assigned to a constant. So this person 439 00:22:23,429 --> 00:22:26,400 probably heard some rule somewhere that says oh, you should have constant for every value 440 00:22:26,400 --> 00:22:27,220 in your program 441 00:22:27,220 --> 00:22:30,170 and they didn't know what to name it so they just called it 72. 442 00:22:30,170 --> 00:22:30,999 That's bad. 443 00:22:30,999 --> 00:22:35,150 You want to give it real names for what it actually stands for. All right, 444 00:22:35,150 --> 00:22:36,670 so any questions about 445 00:22:36,670 --> 00:22:40,340 any of this stuff? Uh huh? 446 00:22:40,339 --> 00:22:44,808 If the rationale like behind name constant and using that whole privacy thing 447 00:22:44,808 --> 00:22:49,019 that so nobody can change it but like, to change, if you have like just double PI equals 3.14 you'd 448 00:22:49,019 --> 00:22:53,440 still have to go to like the guts of the program. So how's it any better? 449 00:22:53,440 --> 00:22:56,980 Well, the difference between this is that programmatically no one changed it. 450 00:22:56,980 --> 00:23:00,210 Right? So if I just said double PI equals 3.14, 451 00:23:00,210 --> 00:23:03,519 somewhere in my program someone could have come along and written this line 452 00:23:03,519 --> 00:23:05,490 and I can't stop them. Right? 453 00:23:05,490 --> 00:23:08,589 So I - it's bad software engineering because 454 00:23:08,589 --> 00:23:10,829 I'm not preventing them from doing this. 455 00:23:10,829 --> 00:23:13,798 This actually prevents them from doing this, so if they go into the program and 456 00:23:13,798 --> 00:23:17,618 say PI equals 5, when they try to compile, the compiler's going to uh huh, 457 00:23:17,618 --> 00:23:20,859 you told me PI was final now someone's trying to change it. 458 00:23:20,859 --> 00:23:21,750 Bad times. 459 00:23:21,750 --> 00:23:25,109 So that's the thing. You always want to force other people to also have good 460 00:23:25,109 --> 00:23:28,639 practice by doing stuff like this. Uh huh, question? Is there a 461 00:23:28,640 --> 00:23:32,170 library that we can import that has standard constants? 462 00:23:32,170 --> 00:23:35,320 There is a math library but for the purpose of this class - if you're really 463 00:23:35,319 --> 00:23:38,169 interested in math kind of stuff, come talk to me, but for the purpose of this class, 464 00:23:38,170 --> 00:23:40,279 there's some basic functions that are in the book 465 00:23:40,279 --> 00:23:43,839 and so you can use those, kind of basic mathy functions but if you are really interested 466 00:23:43,839 --> 00:23:45,169 in other kinds of stuff 467 00:23:45,170 --> 00:23:48,230 we can talk about it separately. I need to push on a little bit so I'm just going to hold 468 00:23:48,230 --> 00:23:50,860 questions for just one second because we need to go to an entirely different 469 00:23:50,859 --> 00:23:51,778 topic. 470 00:23:51,778 --> 00:23:54,169 And the entirely different topic we're going to go to 471 00:23:54,169 --> 00:23:56,799 is something called Booleans, 472 00:23:56,798 --> 00:24:00,849 and there was a time when I told you about this type called Booleans, which is 473 00:24:00,849 --> 00:24:04,740 just the type for variables whose value is true or false. So we can say BOOLEAN 474 00:24:04,740 --> 00:24:08,200 P and P will take the values either true 475 00:24:08,200 --> 00:24:09,940 or false. 476 00:24:09,940 --> 00:24:11,840 Anyone know where the word 477 00:24:11,839 --> 00:24:14,449 Boolean comes from? 478 00:24:14,450 --> 00:24:15,789 Yeah, George Boole. 479 00:24:15,789 --> 00:24:19,769 So know 480 00:24:19,769 --> 00:24:26,250 it and learn it. I should ask if anyone knows how George Boole died? 481 00:24:26,250 --> 00:24:29,589 I asked this once, I'm going to say it, he got sets of false. I thought that was kind of 482 00:24:29,589 --> 00:24:30,970 funny. 483 00:24:30,970 --> 00:24:32,660 He actually died of pneumonia 484 00:24:32,660 --> 00:24:36,519 and the reason why he died of pneumonia - this is a true story - was he was out walking 485 00:24:36,519 --> 00:24:39,859 one time from his house to the college where he was a professor 486 00:24:39,859 --> 00:24:43,179 and he was in the rain and he got wet and he caught pneumonia and so when he came home, 487 00:24:43,180 --> 00:24:45,629 his wife actually, who happened to be the niece of 488 00:24:45,628 --> 00:24:50,829 the person who Mount Everest is named for - just a totally random 489 00:24:50,829 --> 00:24:53,668 side note - history's just fun - 490 00:24:53,669 --> 00:24:56,929 so it turns out she had this belief that the way you get over a particular 491 00:24:56,929 --> 00:25:00,778 illness is you experience the same conditions under which you got the illness. 492 00:25:00,778 --> 00:25:01,599 So she's like, 493 00:25:01,599 --> 00:25:04,769 oh you were in freezing rain, that's why you got a cold, 494 00:25:04,769 --> 00:25:08,058 so while he was lying in bed, she would get buckets of ice water and just 495 00:25:08,058 --> 00:25:09,149 douse him 496 00:25:09,150 --> 00:25:12,930 and he died. So 497 00:25:12,930 --> 00:25:17,080 medicine's come a long way; true story. 498 00:25:17,079 --> 00:25:18,669 All right, so 499 00:25:18,670 --> 00:25:21,910 returning to our friend, the Boolean; Boolean 500 00:25:21,910 --> 00:25:24,950 has the value of true-false which means what we want to do on the - when we assign 501 00:25:24,950 --> 00:25:28,600 something a - assign a Boolean value to some Boolean variable, we need to 502 00:25:28,599 --> 00:25:31,668 figure out some expression that evaluates to true or false. Right? It doesn't 503 00:25:31,669 --> 00:25:34,179 evaluate to 5 or 10 or something, it 504 00:25:34,179 --> 00:25:35,489 evaluates to true or false. 505 00:25:35,489 --> 00:25:38,759 Like, 3 greater than 5 506 00:25:38,759 --> 00:25:41,089 is a true-false expression, right. 507 00:25:41,089 --> 00:25:44,029 You can look at this and say Marilyn, 3's not greater than 5, yeah, and the value of 508 00:25:44,029 --> 00:25:46,649 this is false and that's what gets assigned to P. 509 00:25:46,650 --> 00:25:50,570 So there's certain operations we can use called relational operations 510 00:25:50,569 --> 00:25:54,399 who, when we evaluate them on two values, give us back something 511 00:25:54,400 --> 00:25:57,690 that its value extensively is true or false. 512 00:25:57,690 --> 00:26:02,049 So let me show you some examples of that by going to the overhead. 513 00:26:02,049 --> 00:26:06,079 Let's see my little - oh, the remote mouse, I love the remote mouse. 514 00:26:06,079 --> 00:26:09,408 So Boolean expressions is just a test, basically, for condition 515 00:26:09,409 --> 00:26:12,940 and it evaluates to true or false. Right? You can actually use the words true and false in 516 00:26:12,940 --> 00:26:16,789 your program and say P equals true, for example, those are actually parts of language. So 517 00:26:16,789 --> 00:26:20,629 there's a bunch of different value comparisons you can do, like equal equal, 518 00:26:20,630 --> 00:26:22,889 two equals in a row, no space, 519 00:26:22,888 --> 00:26:24,109 is equals. 520 00:26:24,109 --> 00:26:28,149 It means like, does A equal equal B. It is not a single equal. What does single 521 00:26:28,150 --> 00:26:30,100 equal mean? Assignment. 522 00:26:30,099 --> 00:26:33,889 Assignment, right? When you say X equals 5, that's an assignment. When you say X equal 523 00:26:33,890 --> 00:26:37,190 equal Y, that is a Boolean expression that evaluates the true if X and 524 00:26:37,190 --> 00:26:38,538 Y are the same. 525 00:26:38,538 --> 00:26:43,038 Not equal is exclamation point equal, or sometimes, now that you're all programmers, 526 00:26:43,038 --> 00:26:43,839 after Karel, 527 00:26:43,839 --> 00:26:45,470 this is called a bang. 528 00:26:45,470 --> 00:26:49,079 Because it's kind of like loud. It's like bang, exclamation point. Right? Caught 529 00:26:49,079 --> 00:26:50,990 your attention, hopefully you weren't sleeping. 530 00:26:50,990 --> 00:26:52,859 If you were, you're not. 531 00:26:52,859 --> 00:26:55,889 Bang equal means not equal and if you're familiar with some other language where 532 00:26:55,890 --> 00:26:58,640 you say like less than or greater than means no equal, nuh, uh, 533 00:26:58,640 --> 00:27:00,150 not in Java baby, doesn't happen. All right, 534 00:27:00,150 --> 00:27:02,298 so then there's greater than, 535 00:27:02,298 --> 00:27:05,648 less than, greater than or equal to and less than or equal to. And the order of 536 00:27:05,648 --> 00:27:08,108 the symbols for these two actually makes a difference. 537 00:27:08,108 --> 00:27:11,759 But as you can imagine, those are some common things you might want to consider 538 00:27:11,759 --> 00:27:14,859 for two values to see whether or not, or two variables to see whether or not, for 539 00:27:14,859 --> 00:27:18,479 example, they have equal value. Okay? Now, 540 00:27:18,480 --> 00:27:22,048 Boolean comparisons, order of precedents, it turns out 541 00:27:22,048 --> 00:27:25,500 there's a bunch of operations you can do on Booleans. There's an operation which 542 00:27:25,500 --> 00:27:26,279 is the 543 00:27:26,279 --> 00:27:30,639 bang, or the exclamation point, which means not. That's the logical not. So if you're a 544 00:27:30,640 --> 00:27:34,200 logical person, this is familiar to you. If you're not a logical person, I'll tell you what it means. If you 545 00:27:34,200 --> 00:27:37,309 put not in front of some Boolean expression, or some Boolean variable 546 00:27:37,309 --> 00:27:38,519 like P, 547 00:27:38,519 --> 00:27:42,420 if P is true then NOT P is false and vice versa. It just means give me the 548 00:27:42,420 --> 00:27:43,169 549 00:27:43,169 --> 00:27:46,679 inverse of the logical value that P has or whatever that expression would have 550 00:27:46,679 --> 00:27:48,130 been. Okay? 551 00:27:48,130 --> 00:27:51,820 There's AND, which is ampersand, ampersand. Get to know where your ampersand is on your keys on 552 00:27:51,819 --> 00:27:52,879 your keyboard are. 553 00:27:52,880 --> 00:27:57,210 This is logical and. What that means, if I have two Boolean expressions, P and Q, 554 00:27:57,210 --> 00:27:59,990 they can either be variables or expressions. 555 00:27:59,990 --> 00:28:04,630 P and Q is only true if both of the sub-expressions P and Q are both true. 556 00:28:04,630 --> 00:28:08,600 If either one is false or both are false, it is false. Okay? 557 00:28:08,599 --> 00:28:11,709 And last but not least, there's OR. If you've never used the vertical bar keys 558 00:28:11,710 --> 00:28:15,410 on your keyboard, find them. You're like, I may have never used them in my life. 559 00:28:15,410 --> 00:28:17,700 You will probably use them for the first time in this class. 560 00:28:17,700 --> 00:28:20,380 Or, if I have two expressions P or Q, 561 00:28:20,380 --> 00:28:25,040 it's true if P or Q or both of them are true. So if either P or Q or both 562 00:28:25,039 --> 00:28:26,339 of them are true, 563 00:28:26,339 --> 00:28:29,740 then what you get when you take the OR of them is also true. 564 00:28:29,740 --> 00:28:33,519 These are in order of precedents so you're NOT's get evaluated first, 565 00:28:33,519 --> 00:28:36,838 then your ANDs, then your ORs. Much in the same way the parentheses get evaluated 566 00:28:36,838 --> 00:28:37,509 first, 567 00:28:37,509 --> 00:28:39,039 then multiplication, division, 568 00:28:39,039 --> 00:28:42,599 then addition and subtraction; same kind of thing. So we can look at an expression, 569 00:28:42,599 --> 00:28:45,298 for example, Boolean P equals 570 00:28:45,298 --> 00:28:48,759 something like this. Notice I've fully parenthesized it to make it a little bit 571 00:28:48,759 --> 00:28:49,858 more clear. 572 00:28:49,858 --> 00:28:53,848 X is not equal to 1 or X is not equal to 2, so this might be a common 573 00:28:53,848 --> 00:28:56,048 thing you might think you want to do if you want to say, 574 00:28:56,048 --> 00:29:00,400 well I want to figure out if X is not equal to 1 and X is not equal to 2. So 575 00:29:00,400 --> 00:29:04,170 if it's not equal to 1 or it's not equal to two, isn't that the right thing? 576 00:29:04,170 --> 00:29:07,220 And in fact, this is not the right thing. This is buggy. 577 00:29:07,220 --> 00:29:11,730 P will always be true in this case. Why? Because if P's equal to 1, well the 578 00:29:11,730 --> 00:29:15,539 not equal 1 part is false but it's not equal to 2 and that's become true, so 579 00:29:15,539 --> 00:29:20,019 yeah, false or true and it becomes true and vice versa if X 580 00:29:20,019 --> 00:29:20,849 equals 2. 581 00:29:20,849 --> 00:29:25,048 So what you really want to say is, X is not equal to 1 and X is not equal to 582 00:29:25,048 --> 00:29:25,720 2. 583 00:29:25,720 --> 00:29:28,960 That will make sure if you want to make sure that X is not equal to 1 584 00:29:28,960 --> 00:29:31,980 and X is not equal to 2. That's how you would write it although English people 585 00:29:31,980 --> 00:29:35,349 tend to say X is not equal to 1 or X is not equal to 2 and so it's 586 00:29:35,349 --> 00:29:39,129 confusing if you try to write that out in logic. This is what you really want in that 587 00:29:39,130 --> 00:29:41,430 case. This is 588 00:29:41,430 --> 00:29:44,720 buggy. Okay? Hopefully this gave you an example of a Boolean expression. Is there 589 00:29:44,720 --> 00:29:47,390 any questions about this? 590 00:29:47,390 --> 00:29:48,999 All righty, then let's move on. 591 00:29:48,999 --> 00:29:52,809 Short circuit - question? Where do you get the [inaudible]? 592 00:29:52,809 --> 00:29:54,048 Just fine it on your keyboard, 593 00:29:54,048 --> 00:29:56,950 it depends on your keyboard, but it's a vertical bar, it's somewhere on your 594 00:29:56,950 --> 00:29:57,720 keyboard. 595 00:29:57,720 --> 00:30:01,250 It will be and if it's not on your keyboard, throw your laptop away and get a new 596 00:30:01,250 --> 00:30:02,109 one 597 00:30:02,109 --> 00:30:05,269 and get angry at someone, whoever sold it to you. 598 00:30:05,269 --> 00:30:07,670 It's gotta be on there. Trust me. 599 00:30:07,670 --> 00:30:10,920 Short-circuit evaluation. We actually stop evaluating Boolean expressions as soon 600 00:30:10,920 --> 00:30:13,230 as we know the answer. What does that mean? 601 00:30:13,230 --> 00:30:16,740 Okay, so let's consider something like this. P, which is a Boolean, equals 5 is 602 00:30:16,740 --> 00:30:18,259 greater than 3 or 4 is less than 603 00:30:18,259 --> 00:30:19,509 2. 604 00:30:19,509 --> 00:30:23,000 Well you know what, 5 is greater than 3 so as soon as we evaluate that part, 605 00:30:23,000 --> 00:30:24,109 that's true. 606 00:30:24,109 --> 00:30:28,089 True or anything is true, right? It doesn't matter what the value of this guy is 607 00:30:28,089 --> 00:30:31,378 because we already got a true and true or if anything is going to be true. 608 00:30:31,378 --> 00:30:33,778 So in fact, Java does an optimization 609 00:30:33,778 --> 00:30:36,740 and this second test is not an evaluated at all. 610 00:30:36,740 --> 00:30:40,569 That's why it's called short-circuit evaluation. As soon as we know the answer, 611 00:30:40,569 --> 00:30:44,058 we short circuit out and we don't evaluate the rest of it. You might say, oh, 612 00:30:44,058 --> 00:30:47,599 yeah Marilyn, that's interesting. Why should I care? 613 00:30:47,599 --> 00:30:50,609 And the reason why you care is there are sometimes making use of this actually makes 614 00:30:50,609 --> 00:30:54,109 a difference. So here's a useful example. That's a not-so useful example. Where's a 615 00:30:54,109 --> 00:30:55,109 useful example? 616 00:30:55,109 --> 00:30:58,758 Let's say you want to avoid dividing by 0 because dividing by 0 is an error. 617 00:30:58,759 --> 00:31:01,029 You can say, well P is equal to; 618 00:31:01,029 --> 00:31:02,898 X is not equal to zero; 619 00:31:02,898 --> 00:31:05,808 if X is equal to 0, this is false. 620 00:31:05,808 --> 00:31:09,579 False and anything is false so you want to evaluate the second part over here and 621 00:31:09,579 --> 00:31:11,879 you'll never actually divide by the 0. 622 00:31:11,880 --> 00:31:16,360 If X is not equal to 0, this is true. So to know the value of true and 623 00:31:16,359 --> 00:31:19,779 something else, it needs t actually come over here and evaluate the second part because 624 00:31:19,779 --> 00:31:22,450 it got a true for the first part it needs to know that this is true. 625 00:31:22,450 --> 00:31:25,720 But if it evaluated the first part and got past it, you know that X is not 626 00:31:25,720 --> 00:31:28,630 equal to 0 so you won't divided it by 0. Okay? 627 00:31:28,630 --> 00:31:32,579 So these kinds of little tricks are actually used sometimes in code 628 00:31:32,578 --> 00:31:36,028 and it's called - sometimes it's called a guard to prevent this from happening. 629 00:31:36,028 --> 00:31:38,148 But that's why short-circuit evaluation is something you should 630 00:31:38,148 --> 00:31:41,659 actually know about because you can actually use it for usefully things. 631 00:31:41,660 --> 00:31:45,330 Okay? Any questions about that? Any questions about Booleans or the operations or 632 00:31:45,329 --> 00:31:48,859 that kind of happy news? Uh huh? What happens if you divide by 0? 633 00:31:48,859 --> 00:31:52,619 If you divide by 0 you - usually you'll get - well, you will get an error, you'll get an 634 00:31:52,619 --> 00:31:53,379 exception but 635 00:31:53,380 --> 00:31:56,150 we won't talk about exceptions just think of it as an error. 636 00:31:56,150 --> 00:31:59,290 So let's actually move on. We're just going to cruise through tons of stuff today 637 00:31:59,289 --> 00:31:59,769 because 638 00:31:59,769 --> 00:32:01,129 life is just good. 639 00:32:01,130 --> 00:32:04,200 So it's time to talk about statements. Okay? 640 00:32:04,200 --> 00:32:07,640 Just like in Karel when you had statements like, move and turn left and all 641 00:32:07,640 --> 00:32:11,980 that happy news, now we're gonna do all that happy stuff in Java as well. Okay? 642 00:32:11,980 --> 00:32:15,230 So one thing we first need to know about is this thing called a statement 643 00:32:15,230 --> 00:32:15,990 block, 644 00:32:15,990 --> 00:32:19,490 or a compound statement. They are referred to as the same thing. Usually I say block, the 645 00:32:19,490 --> 00:32:22,599 book likes to say compound statement, it's just a set of statements enclosed in 646 00:32:22,599 --> 00:32:26,598 braces. So you have some opening brace, a bunch of statements and then a closing brace. 647 00:32:26,598 --> 00:32:28,589 This is what we would refer to as a block. 648 00:32:28,589 --> 00:32:32,169 Why do we care about blocks? The reason why we care about blocks is, remember when we 649 00:32:32,170 --> 00:32:33,580 declare variables, a 650 00:32:33,579 --> 00:32:36,099 variable has something called a scope. 651 00:32:36,099 --> 00:32:39,089 All a scope is - the way you can think about it, it's not a mouthwash - the way you can 652 00:32:39,089 --> 00:32:40,209 think about scope 653 00:32:40,210 --> 00:32:44,140 is that it's the lifetime of the variable because variables come into the 654 00:32:44,140 --> 00:32:45,400 world when they're declared 655 00:32:45,400 --> 00:32:49,590 and one thing I didn't tell you is that at some point variables die. It's very sad. 656 00:32:49,589 --> 00:32:53,899 But where a variable dies and lives is the block in which it is declared. So 657 00:32:53,900 --> 00:32:56,298 when we say X equals 5 up here, 658 00:32:56,298 --> 00:33:00,598 X is alive until we get to the end of the block in which X was declared. Which 659 00:33:00,598 --> 00:33:02,458 means when we get to a closing brace, 660 00:33:02,459 --> 00:33:03,969 that X goes away. 661 00:33:03,969 --> 00:33:06,420 That's it's scope, or it's lifetime. Okay? 662 00:33:06,420 --> 00:33:09,050 If we declared X outside of these, 663 00:33:09,049 --> 00:33:13,119 the scope, it would not die when it came here. But if X was declared out here, 664 00:33:13,119 --> 00:33:17,139 this scope is some which is referred to as an interscope. X is still alive inside the interscope 665 00:33:17,140 --> 00:33:20,019 and it does not die when we get it out. It just dies 666 00:33:20,019 --> 00:33:22,628 when we get to the closing brace of the scope 667 00:33:22,628 --> 00:33:29,628 in which X was declared. Uh huh? Can you declare 668 00:33:29,849 --> 00:33:33,269 a final end [inaudible]? It - well it doesn't die because of where we declare it. So if we declare it 669 00:33:33,269 --> 00:33:36,069 inside a method, it could die when we get to the end of the method. 670 00:33:36,069 --> 00:33:38,888 We're going to declare it in the class so it's never actually going to be at the end of 671 00:33:38,888 --> 00:33:45,888 a method so it won't die, it will be alive for the whole time that class is alive. Uh huh? When you declare [inaudible] Uh huh. Will it actually pull out of the sub block? 672 00:33:51,808 --> 00:33:55,109 Yeah, as long as the variable's declared outside of the sub block, 673 00:33:55,109 --> 00:33:57,178 that's fine. And well - 674 00:33:57,179 --> 00:34:00,890 this is kind of technical point and sort of, in most intensive purposes 675 00:34:00,890 --> 00:34:04,150 you don't need to really worry about the nuances here but it's just something 676 00:34:04,150 --> 00:34:07,320 important to remember and we'll look at some examples as we go along. 677 00:34:07,319 --> 00:34:10,590 So other statements you should know about; the IF statement. You're like, oh, IF, 678 00:34:10,590 --> 00:34:14,000 it's just like Karel, and yeah, it's just like Karel, right? 679 00:34:14,000 --> 00:34:15,059 We say if 680 00:34:15,059 --> 00:34:18,789 some condition, and that condition is something that evaluates to true or false. 681 00:34:18,789 --> 00:34:22,109 It can be a Boolean variable or it can be some Boolean expression, anything that evaluates to 682 00:34:22,108 --> 00:34:22,909 true or false. 683 00:34:22,909 --> 00:34:27,269 So you can use, and, or's, not's, all that happy news, now in a condition and then you 684 00:34:27,268 --> 00:34:30,718 have some opening brace and close brace just like Karel and if the condition is 685 00:34:30,719 --> 00:34:32,690 true the statements get executed. 686 00:34:32,690 --> 00:34:36,470 So we want to check if something is even, we can say if that number, 687 00:34:36,469 --> 00:34:40,500 when it's divided by 2, if it's remainder equal the 0? Its 688 00:34:40,500 --> 00:34:43,019 remainder is equal to 0 and it's divided by 2, it's even so we're going to write 689 00:34:43,019 --> 00:34:46,898 out NUM is even. Now notice here, I don't have the braces 690 00:34:46,898 --> 00:34:50,148 and it turns out there's this one special case that says if you have an 691 00:34:50,148 --> 00:34:53,079 IF statement, and it actually applies to a couple of other things, 692 00:34:53,079 --> 00:34:54,039 and it's only - 693 00:34:54,039 --> 00:34:58,429 the body is only one statement, you don't have to have it inside braces. If it's 694 00:34:58,429 --> 00:35:00,828 more than one statement, like this, 695 00:35:00,829 --> 00:35:04,339 then you have to have the braces. So this is just a special case that exists. 696 00:35:04,338 --> 00:35:05,929 But I like to think of 697 00:35:05,929 --> 00:35:07,220 use braces with IF; 698 00:35:07,219 --> 00:35:10,789 you have to if there is more than one statement inside the braces. 699 00:35:10,789 --> 00:35:14,019 But I like to think of something that I refer to as the orthodontist rule. Right? What 700 00:35:14,019 --> 00:35:16,489 do the orthodontist say when you go to an orthodontist? You 701 00:35:16,489 --> 00:35:17,999 need braces. Right? 702 00:35:17,998 --> 00:35:20,798 It doesn't matter if your teeth are straight or you only have one statement 703 00:35:20,798 --> 00:35:21,739 or whatever, you 704 00:35:21,739 --> 00:35:23,578 need braces. 705 00:35:23,579 --> 00:35:27,269 It's always a good idea to use braces, which defines a block, right, so this now 706 00:35:27,268 --> 00:35:31,548 is a block, even if there's only one statement in the IF. There's only one 707 00:35:31,548 --> 00:35:35,458 special case that I'll show you in just a second, but in general always use braces. Right, 708 00:35:35,458 --> 00:35:39,139 think orthodontist. Always use braces, it's just a good idea. Okay? 709 00:35:39,139 --> 00:35:41,659 Any questions about IF? 710 00:35:41,659 --> 00:35:44,458 Hopefully not because you hopefully were using them a lot with Karel. 711 00:35:44,458 --> 00:35:46,719 We have the IF ELSE's just like Karel. 712 00:35:46,719 --> 00:35:50,750 Same kind of thing going on here. We have some condition, either we do the 713 00:35:50,750 --> 00:35:53,190 statements or we do the ELSEs part if the condition is false. 714 00:35:53,190 --> 00:35:54,119 So if we - 715 00:35:54,119 --> 00:35:55,949 the remainder of NUM 716 00:35:55,949 --> 00:35:58,969 divided by 2 is 0, we say NUM is even; or if the 717 00:35:58,969 --> 00:36:02,469 remainder is 0 and if it's not then we know it's odd and we write 718 00:36:02,469 --> 00:36:04,789 out NUM is ODD and so are you. All right? 719 00:36:04,789 --> 00:36:08,519 So not a whole lot of excitement going on there. You saw this in Karel; same thing 720 00:36:08,518 --> 00:36:09,409 exists in Java, 721 00:36:09,409 --> 00:36:12,068 now it's just a little bit different because you're not checking a front that's clear 722 00:36:12,068 --> 00:36:16,480 anymore, all right. We're not in Kansas anymore. You're actually doing some Boolean 723 00:36:16,480 --> 00:36:18,048 expression for that condition. 724 00:36:18,048 --> 00:36:20,530 Let me just give you a couple more and then 725 00:36:20,530 --> 00:36:25,009 we'll take some questions. There's something known as the cascading IF. This is kind of funky. A 726 00:36:25,009 --> 00:36:26,139 cascading IF says, 727 00:36:26,139 --> 00:36:30,129 what I want is I'm going to check for at least just one condition to be 728 00:36:30,130 --> 00:36:32,860 true. I'm going to start off with something that looks like a regular 729 00:36:32,860 --> 00:36:34,858 IF. This is like high school grading, right? 730 00:36:34,858 --> 00:36:38,278 If your score is greater than 90, then print out A. 731 00:36:38,278 --> 00:36:39,650 Otherwise 732 00:36:39,650 --> 00:36:40,690 ELSE 733 00:36:40,690 --> 00:36:43,789 and what are we going to do with the ELSE? We're going to do another IF. 734 00:36:43,789 --> 00:36:47,619 ELSE IF your score is greater than 80, print out B; 735 00:36:47,619 --> 00:36:48,838 ELSE 736 00:36:48,838 --> 00:36:51,889 IF your score is greater than 70, print out C 737 00:36:51,889 --> 00:36:55,918 and otherwise it's kind of - 738 00:36:55,918 --> 00:36:58,848 bad times. All right? So 739 00:36:58,849 --> 00:37:02,568 if you think about this, only one of these IFs gets executed. Why does that? 740 00:37:02,568 --> 00:37:06,509 Because if we come up there and we say score's greater than 90, we do this. 741 00:37:06,510 --> 00:37:08,100 We skip the ELSE part. 742 00:37:08,099 --> 00:37:09,358 Well, what's the ELSE part. 743 00:37:09,358 --> 00:37:11,739 The ELSE part is everything else. 744 00:37:11,739 --> 00:37:14,130 Why is it everything else? Because it's an ELSE 745 00:37:14,130 --> 00:37:17,289 and then it's one statement, right. There's no brace here. Remember I told you, if you 746 00:37:17,289 --> 00:37:19,210 have no braces it's one statement. Well 747 00:37:19,210 --> 00:37:21,449 what is that one statement? It's an IF 748 00:37:21,449 --> 00:37:22,650 followed by an ELSE 749 00:37:22,650 --> 00:37:26,070 and that ELSE has one statement contained within it so it's part of this 750 00:37:26,070 --> 00:37:26,900 bigger IF 751 00:37:26,900 --> 00:37:29,610 and that has an ELSE which is contained as part of that IF. So all of 752 00:37:29,610 --> 00:37:30,309 this stuff, 753 00:37:30,309 --> 00:37:32,719 in some sense, all cascades down. 754 00:37:32,719 --> 00:37:36,269 So if I do the IF part up there and I don't do the ELSE, I skip over this whole 755 00:37:36,268 --> 00:37:37,169 thing. 756 00:37:37,170 --> 00:37:41,009 If I don't do the IF part, then I come and I evaluate the ELSE which means I 757 00:37:41,009 --> 00:37:44,298 evaluate this first condition and it's true then I print out B and I skip 758 00:37:44,298 --> 00:37:47,858 the rest. If it's false, then I evaluate the next condition. So this is just sort 759 00:37:47,858 --> 00:37:51,848 of the idiomatic form - or a pattern form that you see, it's called a cascaded 760 00:37:51,849 --> 00:37:52,318 IF 761 00:37:52,318 --> 00:37:55,170 for when you want to check a bunch of conditions in sequence 762 00:37:55,170 --> 00:37:57,019 and the first one's that true 763 00:37:57,019 --> 00:37:59,679 does its IF part and the it skips the remainder of it. 764 00:37:59,679 --> 00:38:03,279 It's so useful for things like grades where you're like, is it greater than 90? No. Is it greater than 80? No. 765 00:38:03,280 --> 00:38:06,439 Is it greater than 70? As soon as I find one I'm 766 00:38:06,438 --> 00:38:06,998 done. Otherwise, 767 00:38:06,998 --> 00:38:09,699 I have some catch all at the end if it says 768 00:38:09,699 --> 00:38:11,698 things are bad. All right? 769 00:38:11,699 --> 00:38:14,909 So that's the cascading IF. There's something called a SWITCH statement. I'll touch 770 00:38:14,909 --> 00:38:18,588 on this very briefly. The SWITCH statement you can get it in excoriating detail in the 771 00:38:18,588 --> 00:38:21,858 book. It's just kind of nice for you to have. You can actually do anything with 772 00:38:21,858 --> 00:38:25,068 the SWITCH statement - anything you could do with a SWITCH statement you could also do 773 00:38:25,068 --> 00:38:28,458 with IF statements so it's just kind of a nicety. The way the SWITCH statement 774 00:38:28,458 --> 00:38:31,928 works is, it says IF you have some integer value, so here we're going to ask 775 00:38:31,929 --> 00:38:35,519 user for day of the week, right, and the first day of the week in Day 0, which is 776 00:38:35,518 --> 00:38:39,578 Sunday. So they enter some number hopefully between 0 and 6 777 00:38:39,579 --> 00:38:43,528 and we say depending on what number they entered - so that thing that goes inside 778 00:38:43,528 --> 00:38:45,070 the parens for a SWITCH statement 779 00:38:45,070 --> 00:38:49,039 has to be an integer value. Okay? Has to be an integer value. 780 00:38:49,039 --> 00:38:52,089 There're some other things later on in the class where we look at the boil down to 781 00:38:52,088 --> 00:38:56,108 integer values but it cannot be a double, interestingly enough. And we specify 782 00:38:56,108 --> 00:38:58,518 the syntax goes like this, case, 783 00:38:58,518 --> 00:39:02,118 then what value matches that case. So this is Case 0. 784 00:39:02,119 --> 00:39:05,880 And you have sequence of statement until you hit something called a break. 785 00:39:05,880 --> 00:39:09,980 So notice, this is funky because the only braces here are this brace down here 786 00:39:09,980 --> 00:39:13,199 and that brace up there which encloses this whole SWITCH statement. 787 00:39:13,199 --> 00:39:14,759 There's no braces in the middle. 788 00:39:14,760 --> 00:39:18,710 The way it knows where to stop inside a SWITCH, so if someone enters 0 it says let 789 00:39:18,710 --> 00:39:20,949 me find Case 0. Oh, here's Case 0. 790 00:39:20,949 --> 00:39:23,680 I start executing from this line 791 00:39:23,679 --> 00:39:25,589 and keep going until I hit a break. 792 00:39:25,590 --> 00:39:29,890 When I hit a break, I jump out here to the end of the closing brace. 793 00:39:29,889 --> 00:39:34,298 So if the user types in, let's say 6, it says it is Case 0, no, so it skips 794 00:39:34,298 --> 00:39:34,969 that. Is 795 00:39:34,969 --> 00:39:38,789 it Case 6? Yeah, so it prints out Saturday and then it breaks which means 796 00:39:38,789 --> 00:39:40,039 it skips to default. 797 00:39:40,039 --> 00:39:43,210 If they enter any other value other than 0 and 6 it says does it match 798 00:39:43,210 --> 00:39:45,150 0,no, so it skips that; 799 00:39:45,150 --> 00:39:46,289 does it match 6, no, so it 800 00:39:46,289 --> 00:39:47,349 skips that; 801 00:39:47,349 --> 00:39:50,400 it comes to the default with is kind of the be-all if it doesn't match any other 802 00:39:50,400 --> 00:39:53,528 cases and it rights out hey, it's a weekday. Okay? 803 00:39:53,528 --> 00:39:55,429 So you can do this with an IF, right? 804 00:39:55,429 --> 00:39:58,098 But this is just a nice way - there's a lot of times - the 805 00:39:58,099 --> 00:40:00,939 idea is to think like you have some mechanical switch and you've got to pick 806 00:40:00,938 --> 00:40:02,168 one of the options 807 00:40:02,168 --> 00:40:06,808 and that's why it's called a SWITCH statement. Uh huh? Do you have to do in that order, or... 808 00:40:06,809 --> 00:40:10,869 No, your order can be anything that it wants. You can have - you know, the things can 809 00:40:10,869 --> 00:40:12,970 even be out of order. So they don't even need to be sorted. 810 00:40:12,969 --> 00:40:18,828 Was there a question in the back? Uh huh? [Inaudible]. Pardon? Can you 811 00:40:18,829 --> 00:40:22,179 break out of the method? You can't break out of the method all it does is break out of the switch, so it just 812 00:40:22,179 --> 00:40:25,878 takes you - you start executing it at that bottom brace again. You don't actually leave the 813 00:40:25,878 --> 00:40:27,918 method that you're in. But if you have like ac 814 00:40:27,918 --> 00:40:29,098 Let's take it off line. 815 00:40:29,099 --> 00:40:31,359 Let's take it off line. Uh huh? 816 00:40:31,358 --> 00:40:33,708 Was there another question over here? All 817 00:40:33,708 --> 00:40:35,948 right. Let's press on then. 818 00:40:35,949 --> 00:40:37,880 So in terms of four loops, 819 00:40:37,880 --> 00:40:40,690 you've also seen before hopefully in the context of Karel, so 820 00:40:40,690 --> 00:40:42,449 we're just zooming through. 821 00:40:42,449 --> 00:40:46,358 The way the four loops looks like, here's the general form of the four loop. 822 00:40:46,358 --> 00:40:49,748 It has four, just like you saw before in Karel and now it gets a little bit 823 00:40:49,748 --> 00:40:53,509 more complicated but not much. We have something called the INIT. 824 00:40:53,510 --> 00:40:57,369 What the INIT is, is when you come to a four loop, it does whatever statements 825 00:40:57,369 --> 00:41:02,079 are in that INIT once and only once at the beginning of the loop. Okay? 826 00:41:02,079 --> 00:41:03,930 Then you have something called the condition. 827 00:41:03,929 --> 00:41:08,698 The condition is checked every time before we go through the loop. So 828 00:41:08,699 --> 00:41:12,170 the first time we come to a four loop, we do the INIT, we check the condition. 829 00:41:12,170 --> 00:41:14,489 If the condition, which is a Boolean 830 00:41:14,489 --> 00:41:18,429 or it can be any Boolean expression and it's true, we execute what's in the loop. 831 00:41:18,429 --> 00:41:22,079 If it's not true, we skip over the loop and just execute immediately after that 832 00:41:22,079 --> 00:41:23,119 closing brace. Okay? 833 00:41:23,119 --> 00:41:26,309 So similar with what you may have done with Karel, for example, except in 834 00:41:26,309 --> 00:41:30,278 Karel you probably executed some number of time. And the 835 00:41:30,278 --> 00:41:30,768 step - 836 00:41:30,768 --> 00:41:32,449 so we execute the statements 837 00:41:32,449 --> 00:41:34,338 inside the loop only if the condition is true. 838 00:41:34,338 --> 00:41:35,568 And the step 839 00:41:35,568 --> 00:41:39,048 is done every time after the loop. 840 00:41:39,048 --> 00:41:41,708 Okay? So what does that mean? This is kind of a whole bunch of stuff to keep in mind. Let me 841 00:41:41,708 --> 00:41:43,278 just give you a simple example. 842 00:41:43,278 --> 00:41:46,668 Here's a loop that you may have written sort of in the Karel world except now 843 00:41:46,668 --> 00:41:49,898 we're going to print out the value VI instead of making Karel do something, 844 00:41:49,898 --> 00:41:52,420 right? So this was a form of syntax used for Karel 845 00:41:52,420 --> 00:41:56,479 to do something five times. Remember that? If you remember that nod your head. 846 00:41:56,478 --> 00:41:57,578 Yeah. Hopefully. 847 00:41:57,579 --> 00:42:00,899 So what is this really doing? Right now we can pull back the covers. 848 00:42:00,898 --> 00:42:02,958 Well the INIT was to say 849 00:42:02,958 --> 00:42:06,818 create some new variable named I and set its initial value to be 0. 850 00:42:06,818 --> 00:42:11,409 The test you were doing was to say as long as I was less than 5 851 00:42:11,409 --> 00:42:13,068 execute what's inside the loop. 852 00:42:13,068 --> 00:42:17,398 And the step says every time you go through this loop I plus, plus. Add 1 853 00:42:17,398 --> 00:42:21,938 to I and store it back to I. So when we execute this, what happens? The first time 854 00:42:21,938 --> 00:42:26,548 it comes to execute it says I equals 0, it checks to see if 0's less than 5. It is 855 00:42:26,548 --> 00:42:30,268 and it says okay, I execute the loop so it prints out a 0, 856 00:42:30,268 --> 00:42:34,798 adds 1 to I and goes back and checks the test. So you can think of the I plus 857 00:42:34,798 --> 00:42:38,449 plus as happening after it's executed the statements in the loop, but before it does 858 00:42:38,449 --> 00:42:40,668 the test again. So now it 859 00:42:40,668 --> 00:42:45,058 adds 1 to I, I has the Value 1, it's still less than 5, it prints it out, it adds 1 to I, it's 860 00:42:45,059 --> 00:42:46,249 now 2; 861 00:42:46,248 --> 00:42:50,118 less than 5, it prints it out, adds 1 again, it's 3; it prints it out, adds 1 862 00:42:50,119 --> 00:42:51,269 again, it's 4. 863 00:42:51,268 --> 00:42:56,008 Now after it prints out 4, it adds 1 to I again. Now I has the Value 5. 864 00:42:56,009 --> 00:42:59,440 It checks the test again. Is 5 less than 5? 865 00:42:59,440 --> 00:43:03,438 No. Five is not strictly less than 5 so it's done. 866 00:43:03,438 --> 00:43:06,878 Okay, so you get the values from 0 to 4 but notice it still went through 867 00:43:06,878 --> 00:43:11,409 the loop five times. It did the body; we just started counting at 0. And as 868 00:43:11,409 --> 00:43:14,499 computer scientists, that's a very common thing to do is, really when you want to 869 00:43:14,498 --> 00:43:15,958 count something 10 times, 870 00:43:15,958 --> 00:43:18,998 you count from 0 to 9 instead of from 1 to 10. 871 00:43:18,998 --> 00:43:25,998 That's just what we do because zero's a real number and we love it, we care about it. Uh huh? Is the scope of I is the four loop after we're done? 872 00:43:26,789 --> 00:43:31,079 Good question. Exactly, the scope of I is the four loop. So when that four loop - oh that was 873 00:43:31,079 --> 00:43:31,840 real bad - 874 00:43:31,840 --> 00:43:35,019 when this four loop is done, I goes away. 875 00:43:35,018 --> 00:43:40,429 Okay? So the lifetime of I is until we get that next closing brace 876 00:43:40,429 --> 00:43:43,929 in the scope of which I's the clairton. Sort of, we think of the scope of I's 877 00:43:43,929 --> 00:43:46,949 being the four loop so when we get to the end of the, when the four loop is 878 00:43:46,949 --> 00:43:49,599 finished executing and we sort of move on down here, 879 00:43:49,599 --> 00:43:50,588 I is gone away. 880 00:43:50,588 --> 00:43:53,619 But we can create another one in some other loop, that's fine. 881 00:43:53,619 --> 00:43:56,409 There's other funkier things we can do with the four loop rather than just counting 882 00:43:56,409 --> 00:44:00,759 from 0 up to some value. We can actually start with a value like 6 and 883 00:44:00,759 --> 00:44:03,818 count down. So we can say I's initial value is 6, 884 00:44:03,818 --> 00:44:08,679 as long as it's greater than 0, subtract 2. So we're going to use sort of that minus-equal-to 885 00:44:08,679 --> 00:44:12,759 funkiness. And so when we start off, I starts with the Value 6. Six 886 00:44:12,760 --> 00:44:15,490 is greater than 0 so it writes out 6, 887 00:44:15,489 --> 00:44:16,828 subtracts 2, 888 00:44:16,829 --> 00:44:19,920 4 is great than zero, it writes it out, subtracts 2 again, 2 is great 889 00:44:19,920 --> 00:44:21,789 than zero so it writes it out again, and it 890 00:44:21,789 --> 00:44:23,559 subtracts 2 form I and 891 00:44:23,559 --> 00:44:28,589 so now I have the Value 0. Zero is not greater than zero so, it's done. 892 00:44:28,588 --> 00:44:31,338 Okay? So that 0 is not displayed 893 00:44:31,338 --> 00:44:36,058 because after we do the step, we always check the test again before we execute 894 00:44:36,059 --> 00:44:38,269 the loop one more time. Okay? 895 00:44:38,268 --> 00:44:40,478 Any questions about that? 896 00:44:40,478 --> 00:44:43,088 So let's do the WHILE loop super quickly 897 00:44:43,088 --> 00:44:45,699 because you've already seen the WHILE loop in Karel's world. 898 00:44:45,699 --> 00:44:49,048 Same kind of thing. While condition, if that condition is true we execute some 899 00:44:49,048 --> 00:44:53,329 statements. The condition's checked before every iteration of the loop, just like in 900 00:44:53,329 --> 00:44:56,249 Karel. Right? So that's why we did all this stuff in Karel because it carries all over 901 00:44:56,248 --> 00:44:59,728 directly in Java and we execute the statements only if the condition's true. Let me 902 00:44:59,728 --> 00:45:01,278 show you an example. X 903 00:45:01,278 --> 00:45:04,639 starts with a Value 15 while X is greater than 1, every time through we're 904 00:45:04,639 --> 00:45:06,239 going to divide X by 2 905 00:45:06,239 --> 00:45:08,639 and write out the value. So first 906 00:45:08,639 --> 00:45:11,859 time X has 15, 15 is greater than 1, we divide by 2, we do integer 907 00:45:11,860 --> 00:45:13,380 division so we get 7, 908 00:45:13,380 --> 00:45:16,369 we write that out and go back up there. Seven's greater than 1, 909 00:45:16,369 --> 00:45:19,039 we get 3, we do it again, we get 1, 910 00:45:19,039 --> 00:45:23,628 when we do 1 divided equal 2 what do we get? Zero. 911 00:45:23,628 --> 00:45:24,199 Zero, 912 00:45:24,199 --> 00:45:30,869 which is not greater than 1 so we're done. Okay? Nope, let me go back. Nope, 913 00:45:30,869 --> 00:45:33,528 let me just end the show. All right, 914 00:45:33,528 --> 00:45:36,259 so any questions about the WHILE loops and we'll review our friend the loop and a 915 00:45:36,259 --> 00:45:38,798 half next time. All right. 916 00:45:38,798 --> 00:45:40,159 So I will see you next week.