< Return to Video

Count I (5 mins)

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

English subtitles

Revisions