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