WEBVTT 00:00:00.000 --> 00:00:04.685 Thus far, we've written code that, where there's an if-test. And if the test is 00:00:04.685 --> 00:00:08.975 true, we print the row in the data. So in this short section, I wanna show how, 00:00:08.975 --> 00:00:13.378 instead, you would just count the number of rows where the if-test is true. And 00:00:13.378 --> 00:00:17.620 that's, that's more akin to what you really wanna do with computers. So in 00:00:17.620 --> 00:00:21.965 order to do this, we're gonna have to add some novel code into, the code we've been 00:00:21.965 --> 00:00:26.004 doing. So, it's described, here but I'll, I'll point out the parts in this code 00:00:26.004 --> 00:00:30.401 example. So there's three things that have to be added to do counting. One is I'm 00:00:30.401 --> 00:00:34.593 gonna introduce a new variable, Which I'll be sure I'll call count. And I'm just 00:00:34.593 --> 00:00:38.529 gonna set it to zero, and I'm gonna do that before the loop starts running. So, 00:00:38.529 --> 00:00:43.165 count equals zero. Then, inside of the loop, inside the if-statement, where, you 00:00:43.165 --> 00:00:48.158 know, I, I print the row as we've done many times before. But then I'm also gonna 00:00:48.158 --> 00:00:52.909 have this code, count = count + 1. And that's kind of a funny looking 00:00:52.909 --> 00:00:57.473 line. I'll tell you, what that does. Is whatever value is stored inside of count 00:00:57.473 --> 00:01:02.008 it increases it by one. So it just bumps it up from five to six, or ten to eleven, 00:01:02.008 --> 00:01:06.485 Or whatever. But let me, let me explain how this works. So, in mathematics, this 00:01:06.485 --> 00:01:10.963 line doesn't really make any sense. Like, you know, what value is equal to itself 00:01:10.963 --> 00:01:15.951 plus one. But the reason this works in the computer is that the function of the equal 00:01:15.951 --> 00:01:20.485 sign in computer code is actually more simple than it is in mathematics. And the 00:01:20.485 --> 00:01:25.136 key thing to understand is that it first evaluates the right hand side. And then 00:01:25.136 --> 00:01:29.469 only once that has been resolved to get a value, then that value is stored into the 00:01:29.469 --> 00:01:33.854 variable in the left hand side. So imagine the very first time this runs. So count is 00:01:33.854 --> 00:01:38.188 zero and then it gets to this line. So the first thing it's going to do is evaluate 00:01:38.188 --> 00:01:42.364 the right hand side. So count is zero, so zero plus one is one. So it's gonna, this 00:01:42.364 --> 00:01:46.541 pile apart is one. So once it's figured that out, it stores one into count so now 00:01:46.541 --> 00:01:50.874 count is one. So you can see right there the action where coming into the line 00:01:50.874 --> 00:01:54.946 the count was zero and then coming out it's one. So it bumps it up. So then the 00:01:54.946 --> 00:01:59.098 next time it sees this line, if the if-statement is true. That little value at 00:01:59.098 --> 00:02:03.659 the right hand side, count as one. So one plus one is two, so having figured out 00:02:03.659 --> 00:02:08.103 that's it's two than it stores two in the count so now the count has two. So 00:02:08.103 --> 00:02:12.957 ultimately, you don't need to have a total command of the details of this thing, you 00:02:12.957 --> 00:02:17.343 just need to know that the form, x=x+1 for some variable, it just bumps it up by 00:02:17.343 --> 00:02:22.572 one, each time its run. So, I was saying there is three parts. We've got to set to 00:02:22.572 --> 00:02:27.784 zero and the count=count+1 inside the if-statement and then, finally we just, we 00:02:27.784 --> 00:02:33.197 just print out whatever value is left in the, in the count after all, after the 00:02:33.197 --> 00:02:39.475 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 00:02:39.475 --> 00:02:44.200 loop runs, and the if-statement here, checks for names beginning with "A". So, we 00:02:44.200 --> 00:02:48.984 just see all these "A"-names. And then, down at the end, there's this one line, "count: 258" 00:02:48.984 --> 00:02:54.128 So what that shows is, the for-loop, you know, the count was started at 00:02:54.128 --> 00:02:58.733 zero. The for-loop ran all its times. The if-statement was true, apparently 258 00:02:58.733 --> 00:03:03.633 times out of the 2,000 times. And then we get this "count: 258". Prints it out 00:03:03.633 --> 00:03:08.167 because of this line, So, that one runs after the loop is done, so all that these 00:03:08.167 --> 00:03:12.811 internal prints are done. Alright, So let me try some experiments here, So one easy 00:03:12.811 --> 00:03:17.289 thing to do, is like, well what if I just remove this print that's inside the loop? 00:03:17.289 --> 00:03:21.822 So, I'm still gonna run through the rows, I still have the if-statement, but then the 00:03:21.822 --> 00:03:25.969 only thing that happens inside if-statement is count=count+1. 00:03:25.969 --> 00:03:30.616 So just bump it up by one. So, now if I run this program. I just, I just get this 00:03:30.616 --> 00:03:35.795 one line of output. I run it and it just says, bam, 258. So this is beginning, this 00:03:35.795 --> 00:03:39.712 is beginning to resemble more what you think of as com-, you know, the form of 00:03:39.712 --> 00:03:43.883 computer is taking in some massive data and kind of sifting through it and giving 00:03:43.883 --> 00:03:47.901 you kind of a, a final answer. Alright, So, let try, try some more problems here. 00:03:47.901 --> 00:03:52.022 And as us ual, we've got the solutions variable. How many names start with "X" and 00:03:52.022 --> 00:03:56.193 then compare it to how many start with "Y". So, if I wanna count how many names start 00:03:56.193 --> 00:03:59.754 with "X", I just change, you know, the count, and the count equals plus one. 00:03:59.754 --> 00:04:03.620 count=count+1 I can just keep. So I'll just say, how many start 00:04:03.620 --> 00:04:07.412 with "X"? So, if I run it, it says, six. So now it says oh well what if I want 00:04:07.412 --> 00:04:11.537 to know how many start with "Y" well we can see all, all the structure we keep and 00:04:11.537 --> 00:04:16.151 I just have to change that one thing to "Y". And its seventeen, so I guess more names 00:04:16.151 --> 00:04:21.278 start with "Y", a lot more names start with "Y" than with "X". Then it says, for the third 00:04:21.278 --> 00:04:26.093 one, how many girl names begin with "A", then change to count how many boy names 00:04:26.093 --> 00:04:30.970 begin with "A". All right so this is going to bring in material from last time, I 00:04:30.970 --> 00:04:39.306 wan't to use an and here. So I'll say, startsWith("A"). I'll say and ("&&") row.getField("gender")=="girl" 00:04:39.306 --> 00:04:47.144 So that's the task, and then inside I just say 00:04:47.144 --> 00:04:55.498 count=count+1. Alright, a hundred sixty nine, So the, the follow up question is 00:04:55.807 --> 00:05:04.150 how many boy names. So for boy I would just change this. So this notion of the if 00:05:04.150 --> 00:05:10.199 and the task are really, is the same for counting. It's really just where as previously we 00:05:10.199 --> 00:05:16.021 would have said print(row) now I just have this count=count+1 so I can do the counting. 00:05:16.021 --> 00:05:21.163 [inaudible] so more grow names [inaudible]. Okay, so that's our first 00:05:21.163 --> 00:05:25.020 look at basic counting. So let's try some exercises.