WEBVTT 00:00:00.000 --> 00:00:05.737 In this section, I wanna show you can take a simple test, like startsWith("A"), and 00:00:05.737 --> 00:00:11.402 use, the notion of AND, and OR, to combine it with other tests to sort of put 00:00:11.402 --> 00:00:17.721 together a more complicated test. So this is called Boolean logic. So the way this 00:00:17.721 --> 00:00:23.313 is gonna work, is, in code, the notion of AND is the, the symbol for that two 00:00:23.313 --> 00:00:28.905 ampersands run together (&&). And the, the symbol for OR is two vertical bars (||), 00:00:28.905 --> 00:00:33.994 put together. So let me show you what that looks like. So here's some code. And it 00:00:33.994 --> 00:00:38.906 has, the first test just says if the name starts with "A", just as we've seen before, 00:00:39.081 --> 00:00:43.466 and that test is, it's complete and functional. So that is not changed by 00:00:43.466 --> 00:00:48.319 adding the notion of Boolean logic. So for Boolean logic, what we do is we take that 00:00:48.319 --> 00:00:53.056 test and we follow it with two ampersands. So you would pronounce that as, AND. And 00:00:53.056 --> 00:00:57.850 then it's followed by a second test. The second test is also complete and makes 00:00:57.850 --> 00:01:02.469 sense on its own. So, what this does is it just takes two tests and it puts them 00:01:02.469 --> 00:01:06.905 together, and it says, well. For this overall if-test to be true, both of these 00:01:06.905 --> 00:01:10.976 subparts have to be true. So the components are, well here, I'll, I'll run 00:01:10.976 --> 00:01:15.433 it. So what this says is, names that begin with "A". That has to be true. And the name 00:01:15.433 --> 00:01:20.054 has to end with "y". So if I run it, we just get this, you know, kinda shorter list of 00:01:20.054 --> 00:01:24.733 names that begin with "A" and end with "y". Syntactically like I said it has the two 00:01:24.733 --> 00:01:29.569 tests, each of which is complete. They're joined with AND or, OR as we'll see in 00:01:29.569 --> 00:01:34.406 a minute. And then finally there is still this one set of parentheses, a left most 00:01:34.406 --> 00:01:39.359 parentheses and a right most, a right most parenthesis, around the entire thing. So 00:01:39.359 --> 00:01:43.463 I'll do a bunch of examples, like this. A couple other, one other thing to point out 00:01:43.463 --> 00:01:47.422 about this, in this case what happened is, the test is kind of long, right? I have 00:01:47.422 --> 00:01:51.140 this whole startsWith part, and the ampersand, so if I did it all on one line, 00:01:51.140 --> 00:01:55.148 it gets a little, a little long. So what's happened in this case is I, actually I hit 00:01:55.148 --> 00:01:59.107 return after the ampersand. I went down here and I hit the space bar, to get the 00:01:59.107 --> 00:02:03.018 row over here and kind of line it up. So I wrote the second test on a second line. 00:02:03.163 --> 00:02:07.025 That's optional but you can do it. Putting in spaces and stuff like that doesn't 00:02:07.025 --> 00:02:10.780 upset the code. So, I'm, always my examples I'll sort of neaten up in this 00:02:10.780 --> 00:02:14.872 way, Where I'll tend to write one test per line, and then I'll align them this way. 00:02:14.872 --> 00:02:19.217 So here's the second test. The only thing that's a little confusing is to note that 00:02:19.217 --> 00:02:23.151 there are two parentheses are required here. This, this first one just balances 00:02:23.151 --> 00:02:27.530 the parentheses for the endsWith. And then the second one, is the one that covers the 00:02:27.530 --> 00:02:31.657 entire test, so it matches up to the, that first one. So if you leave that out, 00:02:31.657 --> 00:02:35.734 actually I can illustrate this, so it's a pretty easy error to leave that one out, 00:02:35.734 --> 00:02:39.660 'cuz you have to kinda realize alright I need two there. So in that case, not 00:02:39.660 --> 00:02:43.586 always, but the run button for the, just for this class, will try to give you an 00:02:43.586 --> 00:02:47.514 error message, like oh, it looks like there's a missing parentheses there. So 00:02:47.514 --> 00:02:53.111 now form it that way we'll get them, we're still good. As I said before each of 00:02:53.111 --> 00:02:58.640 these tests is complete and stand on its own. So just syntactly just the way we did 00:02:58.640 --> 00:03:03.900 things before. There's this one form of the code that looks right but is wrong. 00:03:03.900 --> 00:03:09.285 And so I'll create that here. So if you just write it this way. So I'll 00:03:09.285 --> 00:03:14.796 just sort of pronounce it. row.getField("name"), that startsWith("a") AND 00:03:14.796 --> 00:03:19.707 endsWith("y"). So just to the ear to a human, they would know 00:03:19.707 --> 00:03:24.531 what that means, but this code does not work. The problem is the second term. On 00:03:24.531 --> 00:03:28.457 its own, doesn't make sense. Right? Doesn't just stand on its own like ends 00:03:28.457 --> 00:03:32.598 with, well it doesn't make sense. Usually what we need is, we're gonna put in a 00:03:32.598 --> 00:03:36.901 cursor here, we would need row.getField("name") or "rank" or whatever it's gonna 00:03:36.901 --> 00:03:41.311 be .endsWith. So this is not working. Each, each, to the left and the right of 00:03:41.311 --> 00:03:45.237 the ampersand, each task has to be complete. So in this case, I'll just sort 00:03:45.237 --> 00:03:50.589 of hit undo in Firefox here, the, if we wanna be talking about name, like we 00:03:50.589 --> 00:03:55.292 spell it out for the first test, and then we also just completely spell it out for 00:03:55.292 --> 00:03:59.994 the second test. So each test makes sense to the computer. Alrighty, So, I'm gonna 00:03:59.994 --> 00:04:04.525 try another example here. Let's see. So right now, if I run this, it just shows 00:04:04.525 --> 00:04:09.399 all the "A" names, which is quite a lot. What the problem statement says is, change 00:04:09.399 --> 00:04:14.121 the code so it prints the names that start with "A" and the rank is less than 50. So, 00:04:14.121 --> 00:04:18.791 oh here's, here's the first test, and I'll add two ampersands, and return let's 00:04:18.791 --> 00:04:23.845 say row, that starts, that startsWith("A") is one test and now we're gonna add, I'm 00:04:23.845 --> 00:04:28.898 gonna combine it with an AND, with the second test, where I'll say rank, oh let's 00:04:28.898 --> 00:04:34.016 say less than or equal to 50. So I think I'll try it, yeah so that works. So 00:04:34.016 --> 00:04:39.495 we still get "A" names but now we're just getting up to rank 50. I'm just gonna try 00:04:39.495 --> 00:04:44.534 a third example here. Alright, now we'll do an example with OR. Change the code 00:04:44.534 --> 00:04:49.384 below, so it prints rows where the following is true. Name starts with "X", or 00:04:49.384 --> 00:04:54.233 the name stars with "Y", or the name stars with "C". So if I just run, run it right 00:04:54.233 --> 00:05:02.722 now, the code that's there just does "X". So I wanna use OR. And we'll copy this. 00:05:02.722 --> 00:05:10.222 So by using an OR-test. You make multiple ways for the overall if-test to 00:05:10.222 --> 00:05:15.516 be true. It's like, well, the if-test were true if this one is true, or if this other 00:05:15.516 --> 00:05:20.940 thing is true. So it's kind of a widening. So here I'll say, OR row.getField - "Y". 00:05:20.940 --> 00:05:27.648 So, for the previous run, we only got "X". Alright? So I'm gonna run this. Oh, so now 00:05:27.648 --> 00:05:33.878 we get "X" and "Y". So there's "Yusuf" and "Yeritza". Notice that I, my goal 00:05:33.878 --> 00:05:38.950 ultimately was to show "X" or "Y" or "Z", but I think it is kinda nice if you get the code 00:05:38.950 --> 00:05:43.961 into a kind of half built state, but where it makes sense. You know, just try running 00:05:43.961 --> 00:05:48.429 it and kind of verify, oh that works before going on to do the whole thing. 00:05:48.429 --> 00:05:53.260 That's kind of a classic computer code rule of thumb; don't try and do the entire 00:05:53.260 --> 00:05:58.203 thing in one step, OR startsWith("Z"). So, I guess partly what I'm showing here is. You 00:05:58.203 --> 00:06:02.927 can have multiple ORs and multiple ANDs and string things together. So let's 00:06:02.927 --> 00:06:07.301 try that, There it goes, Quite a few "Z" names. So there we have it, "X" or "Y" or" "Z, 00:06:07.476 --> 00:06:12.375 so this shows the two vertical bars, and then in this case I have strung together 00:06:12.375 --> 00:06:16.691 three things. For this class, I will only, I'll use either a bunch of things 00:06:16.691 --> 00:06:21.298 connected with AND or a bunch of things connected with OR. I won't combine them. 00:06:21.298 --> 00:06:25.964 Combine them just brings up some other cases that are kind of interesting but I 00:06:25.964 --> 00:06:30.552 not, I don't really want to get into. They don't really help a lot. Other thing I 00:06:30.552 --> 00:06:33.849 should mention about this, alright so I should just apologize to this. This 00:06:33.849 --> 00:06:37.565 ampersand business is sort of a historical accident, or the vertical bar business. 00:06:37.565 --> 00:06:41.144 Language, influencial language chose this in, like the mid'70s. And once one 00:06:41.144 --> 00:06:44.814 language had chosen this symbol to mean OR, some other languages thought, oh, well 00:06:44.814 --> 00:06:48.347 we should just use that convention. And so it just kinda snowballed, where now, 00:06:48.347 --> 00:06:51.880 that's a very common convention. So I felt like, well, it's kind of obscure, but. 00:06:51.880 --> 00:06:55.955 That's the convention so we should just go ahead and learn that one. Alright, So, 00:06:55.955 --> 00:06:59.927 let's scroll down here a little bit. So actually, what I have set up is just a 00:06:59.927 --> 00:07:04.209 large number of examples here. And there's a show solution button here, so if you 00:07:04.209 --> 00:07:08.543 want practice or to review these examples I've done, you could come back and it's 00:07:08.543 --> 00:07:12.670 all sorted here. What I'll is I'll just try the, the first six. So, I'll type in 00:07:12.670 --> 00:07:16.539 the code for those and I'll leave the others as kind of extra practice for 00:07:16.539 --> 00:07:21.716 people who want it. Okay, so the first one says, name starts with "Ab", or starts with 00:07:21.716 --> 00:07:28.182 "Ac". So I'll say there's some code here that we start with already. So it starts 00:07:28.182 --> 00:07:39.366 with "Ab". And what was it? OR starts with "Ac". So this one starts with here, So this 00:07:39.366 --> 00:07:48.795 should be an OR, Two different or's. Let's try that one. So we got "A", "Ac" Where's 00:07:48.795 --> 00:07:56.304 the "Ac" name? Oh. Look I made a bug. I typed "Ab" twice. Okay. There we go. 00:07:56.304 --> 00:08:02.010 Ok, there's an "Ac". That showed that interesting example of a bug, where sometimes bugs are 00:08:02.010 --> 00:08:07.578 syntax errors where you hit the run button and it, it just, you know, it, it crashes 00:08:07.578 --> 00:08:12.263 and you get some red output. But sometimes a bug is just that. I typed 00:08:12.263 --> 00:08:16.494 something that does mean something to the computer, but it's not what I intended. 00:08:16.494 --> 00:08:20.462 And so then the output is not what I expected, then I'm a little bit confused. 00:08:20.462 --> 00:08:24.482 So that's the more higher level kind of bug in this course. It's fairly 00:08:24.482 --> 00:08:28.713 common with computers. Okay, let's see, next one. Name starts with "Ab", or 00:08:28.713 --> 00:08:33.533 "Ac", or "Al". Oh, I see. So this just extends. This just shows that you can have 00:08:33.533 --> 00:08:38.359 three. So I'll say vertical bar. I'll paste this in. So it was "Ac", 00:08:38.359 --> 00:08:42.920 so "Ab" and "Ac" doesn't give us a lot but with "Al" you know it's quite a lot. 00:08:45.560 --> 00:08:55.578 Okay. So the next one was, name starts with O and ends with A. So I'll say 00:08:55.578 --> 00:09:08.877 starts, I'll just translate it. Starts with "O" AND ends with "a". Alright. Let's 00:09:08.877 --> 00:09:16.271 try that. Oh, there's only one, "Olivia". How bout, starts with O and gender is girl? 00:09:16.271 --> 00:09:23.756 So, we'll just get gender right here, and then we test that is not with startsWith 00:09:23.756 --> 00:09:30.329 endsWith, but just with ==. So, O AND girl. Oh, there's only two. 00:09:30.329 --> 00:09:36.400 So we get Olive. Okay, name ends with "a" and gender is what? 00:09:36.980 --> 00:09:43.974 Change this to name.endsWith("a"), and per my previous example, my previous claim, I'll just cut that out. 00:09:43.974 --> 00:09:50.968 Now it's half built right, name ends with "a". But I could run that and sorta verify, 00:09:50.968 --> 00:09:58.788 all rigt that's working. And then, take your working thing, and extend it. What 00:09:58.788 --> 00:10:06.272 did I want? In this case I wanted oh, names ends with "a" and gender is blank. 00:10:06.272 --> 00:10:16.198 Okay. Jesus, Lyle. Girl names neither. Boy. Oh, well, there's a few, Joshua, 00:10:16.198 --> 00:10:23.240 Ezra. Okay, I'll do this last one, number six Rank is less than ten, and gender is 00:10:23.240 --> 00:10:29.149 girl. So I'll change this to "rank". Instead of endsWith. I'll say, less than or equal to ten. 00:10:29.149 --> 00:10:34.181 And, so, Now I'm not using startsWith, or endsWith for either of these but just 00:10:34.181 --> 00:10:39.532 notice each one of those is a complete and correct test as we discussed before. That 00:10:39.532 --> 00:10:44.373 one is, and that one is and I'm just joining them from there. So rank is less 00:10:44.373 --> 00:10:49.581 than ten, and gender in this case is girl. Let's try that. So that makes sense. We 00:10:49.581 --> 00:10:53.668 just get one, two, three, four of them. One through ten girl names. This one's 00:10:53.668 --> 00:10:57.969 kind of interesting in that this, this I think has a sensible English translation. 00:10:57.969 --> 00:11:02.171 What this is, is. What this says is top ten girl names, And then, We can phrase 00:11:02.171 --> 00:11:06.576 that as this AND thing. Where rank is less than or equal to ten and gender is girl. Or indeed 00:11:06.576 --> 00:11:10.927 ultimately we can phrase it as code. So just get it down to, to where the computer 00:11:10.927 --> 00:11:15.116 can actually do it. So there's a few more problems here and the solutions all 00:11:15.116 --> 00:11:19.359 available. So this is a good opportunity to come and review what I did or try a 00:11:19.359 --> 00:11:23.603 little bit of practice before we do the exercises. Cuz these, in some sense, are - 00:11:23.603 --> 00:11:28.007 computer languages have now gotten big enough that I can really we can have a lot 00:11:28.007 --> 00:11:29.780 of different [inaudible] alright.