Placeholder Image

Subtitles section Play video

  • - So now we're gonna talk about Loop Idioms.

  • Loop Idioms are patterns that have to do

  • with how we construct loops.

  • We have the mechanics of "fors" and "whiles",

  • but ultimately we want to get something done.

  • We want to solve a problem with a loop.

  • And often what we have to do is,

  • if we have a set of things,

  • whether it's lines or strings or characters or numbers,

  • we're looking for something like the largest or the smallest

  • or we want to add them up or something like that.

  • So we can't just say, "Add 'em up",

  • we have to say, "Go through each one

  • "and do something to each one."

  • And somehow achieve adding them up.

  • The pattern that we're going to follow is

  • we're going to have this loop that gonna

  • run once for each thing in some chunk of data.

  • We're gonna set something at the beginning

  • and then we're going to do something to each one.

  • And then at the end we're gonna get the payoff.

  • We're gonna get the result.

  • So if we're summing things,

  • we're going to have a running total.

  • This will be like T equals zero.

  • And then this will be T equals T plus the thing value.

  • But this is not the real total.

  • It's the running total during the loop,

  • but at the end it is the real total.

  • So we're going to look at what you do

  • before the loop starts, during the loop,

  • and then what you get after the loop

  • and how you can use that.

  • We're gonna use this loop.

  • It's just going to loop through

  • a set of six numbers over and over and over again.

  • We're going to do something before the loop,

  • we're going to do something after the loop,

  • and we're going to run the loop some number of times.

  • In this case "thing" is our iteration variable

  • because I'm using unpneumonic variables now.

  • It's gonna run 9, 41, 12, 3, 74, and 15.

  • It's going to run and print these things out.

  • It runs its loop six times and away we go.

  • Now this loop does nothing except print stuff out.

  • Of course, I like to do that first.

  • I always print things out

  • to make sure that my brain is functioning.

  • So, to understand how these loops work

  • I'm gonna ask you to function as a program.

  • I'm gonna show you some numbers in succession

  • and I want you to mentally figure out

  • what the largest number is.

  • But more importantly, think about how

  • your brain is solving this problem

  • of what is the largest number,

  • given that I'm only going to show them to you

  • one at a time for a little while.

  • You brain has to do something.

  • Imagine I was going to show you thousands of numbers.

  • I'm not, but imagine I was.

  • How would you organize yourself in a way so that

  • for an hour and a half you could sit here

  • as I showed you numbers and you'd keep track

  • of the largest number that you've seen of all the numbers?

  • Okay, here we go.

  • Here's your first number.

  • Second number.

  • Third number.

  • Fourth number.

  • Fifth number.

  • Sixth and last number.

  • What was the largest number?

  • What was it?

  • Well, it wasn't too hard.

  • It was 74.

  • But that's not the question.

  • How did you brain arrive at 74?

  • Here's all the numbers.

  • If I was showing you all the numbers

  • and asked you, "What's the largest number?"

  • you're eyes would have sort of gone

  • (zooming of eyes moving around the screen)

  • and then you'd got to 74.

  • You wouldn't do it in any particular order.

  • Your eyes would just see the 74

  • and it would just throw the smaller numbers away

  • and it would move really quickly to what the answer is.

  • Even if there was several hundred numbers on the screen

  • your mind would sort of move fluidly

  • wherever it felt like moving and then arrive at it.

  • Probably what it would do is

  • it would do something like move like this,

  • find this, and then check to make sure that it's okay.

  • Then say, "Okay, I got 74.

  • "I'm done."

  • That's not how computers do it.

  • That is not how computers do it.

  • They do not move fluidly.

  • But they are highly dedicated.

  • They're gonna do something.

  • (buzzing imitating a computer)

  • (clicking)

  • 74.

  • But how would you construct a loop to achieve this?

  • Let's take a look.

  • You could create a variable called "largest_so_far".

  • This is the largest value that you have seen

  • in the list so far.

  • Now I haven't shown you any number yet,

  • so we'll just set this to -1 to get us started.

  • Now we see 3 and we're like,

  • "Oh, that's better than -1."

  • It's our first number so it's probably

  • the largest we've seen so far, right?

  • Great.

  • 41.

  • Oh, that's bigger than the largest we've seen so far,

  • so we'll keep it.

  • 12 is not bigger than 41 so we're not gonna keep it.

  • Notice this keeping thing.

  • 9 is not bigger than 41 so there's no point to keeping it.

  • 74 is bigger than 41, so we'll keep it.

  • Is this the largest number?

  • We don't know.

  • We don't know until we're done.

  • 15, not better than 74.

  • So now we're all done and hooray, hooray, hooray,

  • we have the largest number.

  • We have this variable that we kept the largest number

  • that we'd seen up to this point.

  • Then when we know that we're done at the end,

  • that becomes the largest.

  • So if you look at all the numbers,

  • keeping track of the largest so far,

  • at the end of all the largest so far and the largest

  • are the same thing.

  • That's how you get this idea of

  • something you're doing during the loop

  • is not really the answer,

  • but by the time the loop is done

  • you will have the answer.

  • So here's a bit of code that does this.

  • Use it with our numbers.

  • Let's take a look.

  • I have this variable called "largest_so_far".

  • I set it to -1 before the loop.

  • Remember there's a loop before, and a loop after,

  • and a loop in the middle.

  • Before, it's -1.

  • Now "the_num", remember underscores are okay.

  • That's my iteration variable.

  • If 9 is greater than largest_so_far.

  • Well, largest_so_far is -1 so that's true,

  • so this code is gonna run.

  • We're gonna remember the new number.

  • This is 9, so 9 ends up in largest_so_far.

  • And then print it out.

  • So largest_so_far is 9 after we saw the number 9.

  • Then we do it again.

  • So now 41 comes in.

  • Is 41 greater than 9?

  • The answer is, "Yes, it is."

  • So we're going to run this code,

  • copy 41 into largest_so_far and then print it out.

  • largest_so_far is 41 after we saw the number 41.

  • Now we're going to run the loop again with 12

  • and you get the idea, I hope.

  • Is 12 greater than 41,

  • which is the largest we've seen so far?

  • The answer is, "No, it is not."

  • So we skip.

  • The largest_so_far stays 41,

  • even though we saw 12.

  • Meaning we're sort of ratcheting up,

  • but we never ratchet back down.

  • We run it again with 3 and 41.

  • We skip this.

  • Then the largest_so_far is 41,

  • even though we just saw 3.

  • And now we see 74.

  • Is 74 greater than 41?

  • See, we never are looking at all the numbers.

  • We're only are looking at the window on the numbers

  • of the current number that we're looking at.

  • So is 74 greater than 41?

  • The answer is yes, so we run this code.

  • Then we capture the 74.

  • We just saw 74 and it is the largest_so_far.

  • And then we run it again with 15,

  • but 74 is our largest_so_far and so it skips.

  • So 74 remains largest so far after 15.

  • Now we're finished.

  • We just ran the last thing.

  • Our loop takes care of everything

  • and jumps to this print statement and says,

  • "Afterwards, largest_so_far is 74."

  • But at this point it's also the largest.

  • So largest_so_far became largest

  • when our loop finished.

  • So that sort of gives you this notion

  • of how we construct something at the beginning,

  • some kind of thing that we're going to do

  • over and over and over again,

  • and then something at the end.

  • We've put some print statements in

  • just so we can watch it and see what's going on.

  • Coming up next we're going to talk about

  • some more loop patterns.

  • Some counting, totalling, averaging,

  • and finding the smallest number.

  • (relaxed jazz)

- So now we're gonna talk about Loop Idioms.

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it