< Return to Video

Boolean Logic (11 mins)

  • 0:00 - 0:06
    In this section, I wanna show you can take
    a simple test, like startsWith("A"), and
  • 0:06 - 0:11
    use, the notion of AND, and OR, to combine
    it with other tests to sort of put
  • 0:11 - 0:18
    together a more complicated test. So this
    is called Boolean logic. So the way this
  • 0:18 - 0:23
    is gonna work, is, in code, the notion of
    AND is the, the symbol for that two
  • 0:23 - 0:29
    ampersands run together (&&). And the, the
    symbol for OR is two vertical bars (||),
  • 0:29 - 0:34
    put together. So let me show you what that
    looks like. So here's some code. And it
  • 0:34 - 0:39
    has, the first test just says if the name
    starts with "A", just as we've seen before,
  • 0:39 - 0:43
    and that test is, it's complete and
    functional. So that is not changed by
  • 0:43 - 0:48
    adding the notion of Boolean logic. So for
    Boolean logic, what we do is we take that
  • 0:48 - 0:53
    test and we follow it with two ampersands.
    So you would pronounce that as, AND. And
  • 0:53 - 0:58
    then it's followed by a second test. The
    second test is also complete and makes
  • 0:58 - 1:02
    sense on its own. So, what this does is it
    just takes two tests and it puts them
  • 1:02 - 1:07
    together, and it says, well. For this
    overall if-test to be true, both of these
  • 1:07 - 1:11
    subparts have to be true. So the
    components are, well here, I'll, I'll run
  • 1:11 - 1:15
    it. So what this says is, names that begin
    with "A". That has to be true. And the name
  • 1:15 - 1:20
    has to end with "y". So if I run it, we just
    get this, you know, kinda shorter list of
  • 1:20 - 1:25
    names that begin with "A" and end with "y".
    Syntactically like I said it has the two
  • 1:25 - 1:30
    tests, each of which is complete. They're
    joined with AND or, OR as we'll see in
  • 1:30 - 1:34
    a minute. And then finally there is still
    this one set of parentheses, a left most
  • 1:34 - 1:39
    parentheses and a right most, a right most
    parenthesis, around the entire thing. So
  • 1:39 - 1:43
    I'll do a bunch of examples, like this. A
    couple other, one other thing to point out
  • 1:43 - 1:47
    about this, in this case what happened is,
    the test is kind of long, right? I have
  • 1:47 - 1:51
    this whole startsWith part, and the
    ampersand, so if I did it all on one line,
  • 1:51 - 1:55
    it gets a little, a little long. So what's
    happened in this case is I, actually I hit
  • 1:55 - 1:59
    return after the ampersand. I went down
    here and I hit the space bar, to get the
  • 1:59 - 2:03
    row over here and kind of line it up. So I
    wrote the second test on a second line.
  • 2:03 - 2:07
    That's optional but you can do it. Putting
    in spaces and stuff like that doesn't
  • 2:07 - 2:11
    upset the code. So, I'm, always my
    examples I'll sort of neaten up in this
  • 2:11 - 2:15
    way, Where I'll tend to write one test per
    line, and then I'll align them this way.
  • 2:15 - 2:19
    So here's the second test. The only thing
    that's a little confusing is to note that
  • 2:19 - 2:23
    there are two parentheses are required
    here. This, this first one just balances
  • 2:23 - 2:28
    the parentheses for the endsWith. And then
    the second one, is the one that covers the
  • 2:28 - 2:32
    entire test, so it matches up to the, that
    first one. So if you leave that out,
  • 2:32 - 2:36
    actually I can illustrate this, so it's a
    pretty easy error to leave that one out,
  • 2:36 - 2:40
    'cuz you have to kinda realize alright I
    need two there. So in that case, not
  • 2:40 - 2:44
    always, but the run button for the, just
    for this class, will try to give you an
  • 2:44 - 2:48
    error message, like oh, it looks like
    there's a missing parentheses there. So
  • 2:48 - 2:53
    now form it that way we'll get them,
    we're still good. As I said before each of
  • 2:53 - 2:59
    these tests is complete and stand on its
    own. So just syntactly just the way we did
  • 2:59 - 3:04
    things before. There's this one form of
    the code that looks right but is wrong.
  • 3:04 - 3:09
    And so I'll create that here. So
    if you just write it this way. So I'll
  • 3:09 - 3:15
    just sort of pronounce it. row.getField("name"), that startsWith("a") AND
  • 3:15 - 3:20
    endsWith("y"). So just
    to the ear to a human, they would know
  • 3:20 - 3:25
    what that means, but this code does not
    work. The problem is the second term. On
  • 3:25 - 3:28
    its own, doesn't make sense. Right?
    Doesn't just stand on its own like ends
  • 3:28 - 3:33
    with, well it doesn't make sense. Usually
    what we need is, we're gonna put in a
  • 3:33 - 3:37
    cursor here, we would need row.getField("name")
    or "rank" or whatever it's gonna
  • 3:37 - 3:41
    be .endsWith. So this is not working.
    Each, each, to the left and the right of
  • 3:41 - 3:45
    the ampersand, each task has to be
    complete. So in this case, I'll just sort
  • 3:45 - 3:51
    of hit undo in Firefox here, the, if
    we wanna be talking about name, like we
  • 3:51 - 3:55
    spell it out for the first test, and then
    we also just completely spell it out for
  • 3:55 - 4:00
    the second test. So each test makes sense
    to the computer. Alrighty, So, I'm gonna
  • 4:00 - 4:05
    try another example here. Let's see. So
    right now, if I run this, it just shows
  • 4:05 - 4:09
    all the "A" names, which is quite a lot.
    What the problem statement says is, change
  • 4:09 - 4:14
    the code so it prints the names that start
    with "A" and the rank is less than 50. So,
  • 4:14 - 4:19
    oh here's, here's the first test, and I'll
    add two ampersands, and return let's
  • 4:19 - 4:24
    say row, that starts, that startsWith("A")
    is one test and now we're gonna add, I'm
  • 4:24 - 4:29
    gonna combine it with an AND, with the
    second test, where I'll say rank, oh let's
  • 4:29 - 4:34
    say less than or equal to 50. So I
    think I'll try it, yeah so that works. So
  • 4:34 - 4:39
    we still get "A" names but now we're just
    getting up to rank 50. I'm just gonna try
  • 4:39 - 4:45
    a third example here. Alright, now we'll
    do an example with OR. Change the code
  • 4:45 - 4:49
    below, so it prints rows where the
    following is true. Name starts with "X", or
  • 4:49 - 4:54
    the name stars with "Y", or the name stars
    with "C". So if I just run, run it right
  • 4:54 - 5:03
    now, the code that's there just does "X". So
    I wanna use OR. And we'll copy this.
  • 5:03 - 5:10
    So by using an OR-test. You make
    multiple ways for the overall if-test to
  • 5:10 - 5:16
    be true. It's like, well, the if-test were
    true if this one is true, or if this other
  • 5:16 - 5:21
    thing is true. So it's kind of a widening.
    So here I'll say, OR row.getField - "Y".
  • 5:21 - 5:28
    So, for the previous run, we only got "X".
    Alright? So I'm gonna run this. Oh, so now
  • 5:28 - 5:34
    we get "X" and "Y". So there's "Yusuf" and
    "Yeritza". Notice that I, my goal
  • 5:34 - 5:39
    ultimately was to show "X" or "Y" or "Z", but I
    think it is kinda nice if you get the code
  • 5:39 - 5:44
    into a kind of half built state, but where
    it makes sense. You know, just try running
  • 5:44 - 5:48
    it and kind of verify, oh that works
    before going on to do the whole thing.
  • 5:48 - 5:53
    That's kind of a classic computer code
    rule of thumb; don't try and do the entire
  • 5:53 - 5:58
    thing in one step, OR startsWith("Z"). So, I
    guess partly what I'm showing here is. You
  • 5:58 - 6:03
    can have multiple ORs and multiple ANDs and
    string things together. So let's
  • 6:03 - 6:07
    try that, There it goes, Quite a few "Z"
    names. So there we have it, "X" or "Y" or" "Z,
  • 6:07 - 6:12
    so this shows the two vertical bars, and
    then in this case I have strung together
  • 6:12 - 6:17
    three things. For this class, I will only,
    I'll use either a bunch of things
  • 6:17 - 6:21
    connected with AND or a bunch of things
    connected with OR. I won't combine them.
  • 6:21 - 6:26
    Combine them just brings up some other
    cases that are kind of interesting but I
  • 6:26 - 6:31
    not, I don't really want to get into. They
    don't really help a lot. Other thing I
  • 6:31 - 6:34
    should mention about this, alright so I
    should just apologize to this. This
  • 6:34 - 6:38
    ampersand business is sort of a historical
    accident, or the vertical bar business.
  • 6:38 - 6:41
    Language, influencial language chose this
    in, like the mid'70s. And once one
  • 6:41 - 6:45
    language had chosen this symbol to mean
    OR, some other languages thought, oh, well
  • 6:45 - 6:48
    we should just use that convention. And so
    it just kinda snowballed, where now,
  • 6:48 - 6:52
    that's a very common convention. So I felt
    like, well, it's kind of obscure, but.
  • 6:52 - 6:56
    That's the convention so we should just go
    ahead and learn that one. Alright, So,
  • 6:56 - 7:00
    let's scroll down here a little bit. So
    actually, what I have set up is just a
  • 7:00 - 7:04
    large number of examples here. And there's
    a show solution button here, so if you
  • 7:04 - 7:09
    want practice or to review these examples
    I've done, you could come back and it's
  • 7:09 - 7:13
    all sorted here. What I'll is I'll just
    try the, the first six. So, I'll type in
  • 7:13 - 7:17
    the code for those and I'll leave the
    others as kind of extra practice for
  • 7:17 - 7:22
    people who want it. Okay, so the first one
    says, name starts with "Ab", or starts with
  • 7:22 - 7:28
    "Ac". So I'll say there's some code here
    that we start with already. So it starts
  • 7:28 - 7:39
    with "Ab". And what was it? OR starts with
    "Ac". So this one starts with here, So this
  • 7:39 - 7:49
    should be an OR, Two different or's. Let's
    try that one. So we got "A", "Ac" Where's
  • 7:49 - 7:56
    the "Ac" name? Oh. Look I made a bug. I typed
    "Ab" twice. Okay. There we go.
  • 7:56 - 8:02
    Ok, there's an "Ac". That showed that interesting
    example of a bug, where sometimes bugs are
  • 8:02 - 8:08
    syntax errors where you hit the run button
    and it, it just, you know, it, it crashes
  • 8:08 - 8:12
    and you get some red output. But
    sometimes a bug is just that. I typed
  • 8:12 - 8:16
    something that does mean something to the
    computer, but it's not what I intended.
  • 8:16 - 8:20
    And so then the output is not what I
    expected, then I'm a little bit confused.
  • 8:20 - 8:24
    So that's the more higher level kind of
    bug in this course. It's fairly
  • 8:24 - 8:29
    common with computers. Okay, let's see,
    next one. Name starts with "Ab", or
  • 8:29 - 8:34
    "Ac", or "Al". Oh, I see. So this just
    extends. This just shows that you can have
  • 8:34 - 8:38
    three. So I'll say vertical bar.
    I'll paste this in. So it was "Ac",
  • 8:38 - 8:43
    so "Ab" and "Ac" doesn't give us a lot but
    with "Al" you know it's quite a lot.
  • 8:46 - 8:56
    Okay. So the next one was, name starts
    with O and ends with A. So I'll say
  • 8:56 - 9:09
    starts, I'll just translate it. Starts
    with "O" AND ends with "a". Alright. Let's
  • 9:09 - 9:16
    try that. Oh, there's only one, "Olivia". How
    bout, starts with O and gender is girl?
  • 9:16 - 9:24
    So, we'll just get gender right here, and
    then we test that is not with startsWith
  • 9:24 - 9:30
    endsWith, but just with ==.
    So, O AND girl. Oh, there's only two.
  • 9:30 - 9:36
    So we get Olive. Okay,
    name ends with "a" and gender is what?
  • 9:37 - 9:44
    Change this to name.endsWith("a"), and
    per my previous example, my previous claim, I'll just cut that out.
  • 9:44 - 9:51
    Now it's half built right, name ends with
    "a". But I could run that and sorta verify,
  • 9:51 - 9:59
    all rigt that's working. And then, take
    your working thing, and extend it. What
  • 9:59 - 10:06
    did I want? In this case I wanted oh,
    names ends with "a" and gender is blank.
  • 10:06 - 10:16
    Okay. Jesus, Lyle. Girl names neither.
    Boy. Oh, well, there's a few, Joshua,
  • 10:16 - 10:23
    Ezra. Okay, I'll do this last one, number
    six Rank is less than ten, and gender is
  • 10:23 - 10:29
    girl. So I'll change this to "rank". Instead
    of endsWith. I'll say, less than or equal to ten.
  • 10:29 - 10:34
    And, so, Now I'm not using startsWith, or
    endsWith for either of these but just
  • 10:34 - 10:40
    notice each one of those is a complete and
    correct test as we discussed before. That
  • 10:40 - 10:44
    one is, and that one is and I'm just
    joining them from there. So rank is less
  • 10:44 - 10:50
    than ten, and gender in this case is girl.
    Let's try that. So that makes sense. We
  • 10:50 - 10:54
    just get one, two, three, four of them.
    One through ten girl names. This one's
  • 10:54 - 10:58
    kind of interesting in that this, this I
    think has a sensible English translation.
  • 10:58 - 11:02
    What this is, is. What this says is top
    ten girl names, And then, We can phrase
  • 11:02 - 11:07
    that as this AND thing. Where rank is less
    than or equal to ten and gender is girl. Or indeed
  • 11:07 - 11:11
    ultimately we can phrase it as code. So
    just get it down to, to where the computer
  • 11:11 - 11:15
    can actually do it. So there's a few more
    problems here and the solutions all
  • 11:15 - 11:19
    available. So this is a good opportunity
    to come and review what I did or try a
  • 11:19 - 11:24
    little bit of practice before we do the
    exercises. Cuz these, in some sense, are -
  • 11:24 - 11:28
    computer languages have now gotten big
    enough that I can really we can have a lot
  • 11:28 - 11:30
    of different [inaudible] alright.
Title:
Boolean Logic (11 mins)
Video Language:
English
duvin1 edited English subtitles for Boolean Logic (11 mins)
duvin1 edited English subtitles for Boolean Logic (11 mins)
duvin1 edited English subtitles for Boolean Logic (11 mins)
stanford-bot edited English subtitles for Boolean Logic (11 mins)
stanford-bot edited English subtitles for Boolean Logic (11 mins)
stanford-bot added a translation

English subtitles

Revisions