-
Thus far, we've written code that, where
there's an if-test. And if the test is
-
true, we print the row in the data. So in
this short section, I wanna show how,
-
instead, you would just count the number
of rows where the if-test is true. And
-
that's, that's more akin to what you
really wanna do with computers. So in
-
order to do this, we're gonna have to add
some novel code into, the code we've been
-
doing. So, it's described, here but I'll,
I'll point out the parts in this code
-
example. So there's three things that have
to be added to do counting. One is I'm
-
gonna introduce a new variable, Which I'll
be sure I'll call count. And I'm just
-
gonna set it to zero, and I'm gonna do
that before the loop starts running. So,
-
count equals zero. Then, inside of the
loop, inside the if-statement, where, you
-
know, I, I print the row as we've done
many times before. But then I'm also gonna
-
have this code, count = count + 1. And that's kind of a funny looking
-
line. I'll tell you, what that does. Is
whatever value is stored inside of count
-
it increases it by one. So it just bumps
it up from five to six, or ten to eleven,
-
Or whatever. But let me, let me explain
how this works. So, in mathematics, this
-
line doesn't really make any sense. Like,
you know, what value is equal to itself
-
plus one. But the reason this works in the
computer is that the function of the equal
-
sign in computer code is actually more
simple than it is in mathematics. And the
-
key thing to understand is that it first
evaluates the right hand side. And then
-
only once that has been resolved to get a
value, then that value is stored into the
-
variable in the left hand side. So imagine
the very first time this runs. So count is
-
zero and then it gets to this line. So the
first thing it's going to do is evaluate
-
the right hand side. So count is zero, so
zero plus one is one. So it's gonna, this
-
pile apart is one. So once it's figured
that out, it stores one into count so now
-
count is one. So you can see right there
the action where coming into the line
-
the count was zero and then coming out
it's one. So it bumps it up. So then the
-
next time it sees this line, if the if-statement is true. That little value at
-
the right hand side, count as one. So one
plus one is two, so having figured out
-
that's it's two than it stores two in the
count so now the count has two. So
-
ultimately, you don't need to have a total
command of the details of this thing, you
-
just need to know that the form, x=x+1
for some variable, it just bumps it up by
-
one, each time its run. So, I was saying
there is three parts. We've got to set to
-
zero and the count=count+1
inside the if-statement and then, finally we just, we
-
just print out whatever value is left in
the, in the count after all, after the
-
for-loop has run through all its times. So, let's just try this.
So if I run this. What we see is that the
-
loop runs, and the if-statement here,
checks for names beginning with "A". So, we
-
just see all these "A"-names. And then, down
at the end, there's this one line, "count: 258"
-
So what that shows is, the for-loop, you know, the count was started at
-
zero. The for-loop ran all its times. The
if-statement was true, apparently 258
-
times out of the 2,000 times. And then we
get this "count: 258". Prints it out
-
because of this line, So, that one runs
after the loop is done, so all that these
-
internal prints are done. Alright, So let
me try some experiments here, So one easy
-
thing to do, is like, well what if I just
remove this print that's inside the loop?
-
So, I'm still gonna run through the rows,
I still have the if-statement, but then the
-
only thing that happens inside if-statement is count=count+1.
-
So just bump it up by one. So, now if I
run this program. I just, I just get this
-
one line of output. I run it and it just
says, bam, 258. So this is beginning, this
-
is beginning to resemble more what you
think of as com-, you know, the form of
-
computer is taking in some massive data
and kind of sifting through it and giving
-
you kind of a, a final answer. Alright,
So, let try, try some more problems here.
-
And as us ual, we've got the solutions
variable. How many names start with "X" and
-
then compare it to how many start with "Y".
So, if I wanna count how many names start
-
with "X", I just change, you know, the
count, and the count equals plus one.
-
count=count+1 I can just
keep. So I'll just say, how many start
-
with "X"? So, if I run it, it says, six.
So now it says oh well what if I want
-
to know how many start with "Y" well we
can see all, all the structure we keep and
-
I just have to change that one thing to "Y".
And its seventeen, so I guess more names
-
start with "Y", a lot more names start with
"Y" than with "X". Then it says, for the third
-
one, how many girl names begin with "A",
then change to count how many boy names
-
begin with "A". All right so this is going
to bring in material from last time, I
-
wan't to use an and here. So I'll say,
startsWith("A"). I'll say and ("&&") row.getField("gender")=="girl"
-
So that's
the task, and then inside I just say
-
count=count+1. Alright, a hundred sixty
nine, So the, the follow up question is
-
how many boy names. So for boy I would
just change this. So this notion of the if
-
and the task are really, is the same for
counting. It's really just where as previously we
-
would have said print(row) now I just have this
count=count+1 so I can do the counting.
-
[inaudible] so more grow names
[inaudible]. Okay, so that's our first
-
look at basic counting. So let's try some
exercises.