© Distribution of this video is restricted by its owner
Transcript ×
Auto highlight
Font-size
00:03 Alright, this is going to be short lecture discussing wild loops. Uh

00:10 summarizing what's covered in this section of course and showing some highlights and you

00:19 need to get most of the content the readings from the book. All

00:25 , let's go. So as we , loops and iteration are a very

00:34 part of any programming languages because loops us to specify something needs to be

00:41 repeating repeatedly many times. And computers really good at that. So there's

00:48 ways a language python provides us for for these iterations or reputations. We've

00:59 seen four loop in this course. today the focus will be on a

01:05 group. So before we get to second kind of loop called the wild

01:10 . Let's just quickly review what we about the four loop, which we've

01:15 in a while. So the fourth allows us to iterate through a sequence

01:22 here of values. So every time go through this loop body, the

01:30 takes one of the values in the and with that value, you execute

01:35 body. That is also called the index. And each successive iteration takes

01:41 different member of the sequence and And you're all familiar with this.

01:48 we'll move on. You are very example that we have seen before when

01:55 were looking at four loops, it fall fruit in apples, bananas,

02:00 , oranges, and then inside the , body. Body, we have

02:06 something, we have fruit and we know how that works the first time

02:11 the loop, variable fruit takes the , apples and we have apples.

02:20 time bananas, we have bananas, have grapes, we have oranges.

02:26 basically the loop variable went through every of the sequence and then executed the

02:33 um with that value. So very quick review another thing that we

02:40 very often with a four loop is here, we are adding numbers from

02:48 to 5, so we can simply 04 number in 012345. And before

02:56 we set this valuable sum to And within the body of the

03:00 every time you say some equal some number, so you'll get some equals

03:05 +10 plus two plus three. We've this many times and then outside of

03:11 loop were printing the truth. and as you all know, there's

03:18 better way of saying this sequence. . And that's called the range

03:26 Okay, exactly. The same thing be done with the range function

03:32 The content of these two programs is same except the sequence 0123453. Place

03:40 range. So that's a very quick of the type of things we do

03:47 just to set up the context as move to the a different kind of

03:56 . Okay, so why do we something more than four loops can do

04:03 many things for us. So the loop, the loop effort to as

04:10 means that the number of iterations in four loop is determined at the point

04:17 loop starts. The it is determined the number of elements in the sequence

04:25 and it works wonderfully when that's the . However, there are many scenarios

04:32 you start repeating something, you're doing thing again and again, but how

04:38 times you have to do it? do not, you know when you

04:41 started. So the number we're talking , the number of times the iterations

04:48 to repeat. That is not always . And that's basically the fundamental reason

04:57 a second kind of loop, but we get to the other kind of

05:01 , let's see some examples where there a loop you need to repeat but

05:07 do not know how many times. here is an example. Um we're

05:16 in terms of a program that asks user to enter a sequence of positive

05:23 . They're entering integers, one after other six, it to four,

05:29 . And Uh but we do not how many images are there. So

05:36 just tell the user that they don't to tell us ahead of time how

05:44 numbers are there, but when they done then they enter the number

05:49 And that's how we know that this sequence is done. And so a

05:57 input in this case -1. It be something else is entered to indicate

06:02 end of the sequence. So this could be doing anything. But let's

06:06 just you know, computing the imprinting sum of the numbers. Okay,

06:11 the point here is when you're taking input sequence, you do not know

06:17 long the sequence is. It just coming, it keeps coming, it

06:20 coming. And at some point you The -1. And that's when you

06:24 you're done. You did not know many elements were in the sequence.

06:28 many images were in the sequence until sequences through the loop was already

06:35 So the point of this, this common for a lot of things.

06:42 just for counting numbers or anything. that's one of the simplest examples we

06:47 think of. So we came up that, but there are others.

06:53 , this is a common pattern. where the end of the iterations is

06:58 by a special value. Special value was -1. It doesn't have to

07:03 -1. It could be something else is predetermined. So more examples just

07:11 for a minute that you have to a program counts owls and then

07:18 But uh import comes one character at time. It's a sentence. And

07:26 just, you know, somebody's asking say how many rivals are there?

07:30 many consonants are there or maybe how blank spaces are there? So the

07:35 question is how do you do It's obviously not hard. You look

07:44 one character at a time and then see if it's a vowel or it's

07:49 it's a consonant vowel. You do one and then you keep going.

07:55 the question is when do you know over and you can just say that

08:03 a simple world, a sentence is when you there's a period at the

08:08 of a question mark or something like that tells you that the sentence is

08:13 and once you know that and for sentence you can do this kind of

08:17 . The point is you didn't know length of the the sentence and the

08:23 of times you have to repeat this equals to the length of the

08:28 But you find out dynamically when the is over, one of them.

08:33 of these characters comes in used So that's another example. Again,

08:39 pattern at the end of iteration is by a special value rather values in

08:45 case which could be one of So something more practical and useful is

08:53 program that totals your grocery bill. seen those programs where you're using one

09:00 these machines to check out on from store. Then you keep entering your

09:08 and it keeps figuring out usually the automatically and keeps adding adding it to

09:15 total. How does it know when done. You say something like,

09:20 know, there's usually a key call , you say done and that's how

09:24 know it's it's over and it'll don't through this iteration every time you enter

09:32 item and do it that many not knowing at the beginning how many

09:36 were, but it just knows when say done internally, there's probably a

09:41 character that goes in and that's how stop. So the point is that

09:46 way of doing a reputation is to repeating until you see a special

09:52 a special character and all of these examples of those. All right.

10:00 that was one pattern. Now let's of a different problem. So this

10:10 says that you are running a charity you have a fixed goal, you

10:17 want $1,000 and then you're gonna stop fundraiser and people are coming in and

10:26 calling in coming in and giving you money. But of course you have

10:29 idea How many donations it will Maybe the first person will come and

10:36 you 2000 and you're more than done maybe the 1st 10 people will give

10:41 $2 and it will be a long before you get to 1000. So

10:47 this pattern, The condition at which stop trading is computed dynamically, you're

10:56 to keep track of how much money coming in and as soon as the

11:01 needs or exceeds 1000, you're gonna . So this is different from the

11:06 pattern where it is a particular input . The inputs vary but what you're

11:11 for a particular variable to reach a value and at that point you will

11:19 . So this is also a common . In fact a very common pattern

11:26 which this is an example um to some some other examples, how would

11:33 write a program that randomly picks a , say between one and 100.

11:40 then it asks the user to guess number and then it helps the user

11:46 let's say the number. The the picked is 40 p. And then

11:54 user guesses to and you say you that's too far. The user get

12:02 guesses se 30. And you say . That means you're getting closer after

12:09 the user guesses 60. And you cool you're getting further, you keep

12:16 the user guidance until they actually reached number and then they may get a

12:21 or something depending on how many tries took them to do it. And

12:27 is when they actually get it if you have to write a program

12:31 that, you can't fight a full because you do not know how many

12:36 it will take. You have to keep looking at the import comparing the

12:43 with what you have and then um based on that. Making the decision

12:50 to stop the computation. Uh We'll with one example, one more

12:58 And this is a somewhat mathematical, wants you to find the smallest fee

13:06 that three to the power P. greater than one million. Well you

13:11 think for a second how you would it. So one target simply you

13:22 you know, think of a As 100. And if three to

13:28 power 100 is more than a then you look for something smaller,

13:36 say 50. And then if 50 also too big, you think of

13:41 even smaller, 20 then if 23 the power 20 is too small than

13:47 million, then you go higher up so on until you reach that

13:52 So you can you can write a to do that. And once again

13:58 the time when this program will end when you find a P. For

14:06 three to the power P. Is than one million. And but then

14:12 you do the same thing with t one, uh then through to the

14:18 minus one is less than one something like that. The details are

14:22 important. The point really is that making a dynamic computation, which eventually

14:29 tell you when you write the region you wrote, sorry, when you

14:35 the right T. And that's when stop. And the point of all

14:39 these examples, we looked at two , let's review one was the pattern

14:47 the end of iterations is indicated by special value. And the second pattern

14:53 it's indicated by a computation whether it's total here or whether it's um the

15:01 and so on. So the commonality both of these is that in the

15:08 both these computations, the number of is not known ahead of time and

15:14 it's not at least not easy, not impossible to write them as a

15:19 loop where you need to have all list of values over which a loop

15:25 in the begin. So we need else. So that that's really this

15:30 really to motivate that there are computations are repeating that are iterating. But

15:37 , well for low doesn't work because number of iterations is not known.

15:42 you need something better. So the as you probably were guessing is a

15:51 kind of loop or the wind So this is what a wide loop

15:58 like visually. Very simple. There a condition, uh some expression usually

16:06 condition is uh bullion expression and statements be just about any python code.

16:16 while condition execute these statements and the it functions is you start off you

16:26 the condition since it's a brilliant it's either true or false. If

16:32 false, you're done, you don't anything and the execution is over can

16:39 you exit, but if it's true is here here. If it's false

16:46 , if it's true then you execute statement and after you're done executing the

16:54 then you go back and again test condition and again if it turns out

16:59 be false you're done. If it's then you repeat the statement, you

17:04 doing that until this condition comes out be false. Let's let's look at

17:09 another way. So this is the chart description of a wild look.

17:19 execution starts from top here and you in and the first thing you do

17:26 you hit a condition condition is true false. If it's false you're

17:31 If it's true, you go through statements and once you're done executing the

17:36 you get go back in your test condition and then um again if it

17:42 you're done, it's true, you execute the statement and it continues until

17:47 condition is um is not true And uh if you're thinking, you'll

17:54 that something in the way this condition being computed must be changing here because

18:02 not then condition. If it's true first time it will always be

18:07 Uh That's how it works, you keep repeating until something becomes true and

18:13 pretty much all the white bloopers. way to look at it. A

18:22 expression and there's a loop body, keep repeating the loop body until the

18:27 expression becomes false. If it's true is executed, it's false. You're

18:37 . So it's basically we're saying exactly same thing. Three different ways and

18:44 you you have a pretty good handle this now. Alright so now we're

18:52 to show some examples with Y Initially we're going to show some things

19:01 are not particularly difficult to do with four loop but we'll see that they

19:07 be done with a wide loop two then we'll go through a couple examples

19:11 things that are not possible are not to do with wild loop.

19:19 Things that are not easy to do four loops and we'll just take a

19:23 how a wild can help. So is a simple um For look you

19:36 take a look at this for a . All it does is um some

19:45 zero K. In range five. K. Is gonna be 01234.

19:52 time is zero, next it's one it's two and three and then

19:56 And then all you're doing is some sample escape initially. Summit zero,

20:01 zero plus zero. Then when I some zero plus one plus two plus

20:07 plus four. Once the loop is you just print whatever you have.

20:12 so now we can do this as wide loop in this case. Some

20:19 casino still is the same. You to initialize the variable care. A

20:26 loop automatically assigns values to care but wide loop does not, you have

20:33 put that value in there and then you're doing here, you're going from

20:42 in the wild loop. What we're do is we're gonna put the initial

20:46 at zero, this would be the one. And uh and uh say

20:53 a different way we say that you doing while this K is less than

20:59 . It's the same thing as doing is what you start with and is

21:05 than five means when it reaches five gonna stop and then the final you

21:11 to do, you started at You have this condition that you're going

21:15 stop in case as soon as Casey is five and then you add the

21:21 K equals K plus one inside the loop. And uh and this sum

21:29 sum plus K just basically carries over here. So the way this loop

21:34 run is scale will be zero And then you say wild case less

21:39 five is K. Less than Yes zero is less than five.

21:43 it's true. You execute and when execute becomes K plus one. So

21:49 means zero equals zero plus one. will be one next time you come

21:55 K less than five. Yes one less than five. You repeat.

21:59 Kay will have value 01234 is less five. Yes. When K is

22:08 that's still true. You do one time. Then K becomes five.

22:13 you go back and the expression is than five is not true anymore.

22:23 K is five then K is less five is not true. Then at

22:27 point the loop is done and you'll whatever the sum is. It's Another

22:35 of what the four loop was Okay well okay one more example.

22:44 this is even simpler. The four says just count down four iron 54321

22:52 I and when you're done you just off. So loop is gonna go

22:58 54321. And that one the loop done. And then you print this

23:08 similar to the previous example you put equals five to get started because you're

23:15 go um downwards now. And in loop you test, the test question

23:22 n greater than zero. You're gonna down from five two in a decreasing

23:29 and you're gonna stop as soon as . Is equal to or less than

23:35 . So you say that and you print and and in the loop you

23:41 an equal than minus one. So first time you come five is greater

23:45 zero, you go in second time 432 and one you're still good and

23:56 it's zero. And at that 00.0 not driven in zero, it's equal

24:01 zero. This expression is not true . You're done executing the wild loop

24:07 the execution comes after the loop and the prints blastoff is printed. Um

24:15 should have been a little blank That was a piper. These two

24:20 supposed to be doing exactly the same . So those were some examples of

24:31 me back up a little. These were some examples of things you could

24:37 do with a four loop and you also do with a while loop.

24:43 it's not necessarily in those examples, not saying that while loop is

24:49 it's just that we're saying the wild is powerful enough to be doing those

24:56 and it's more to illustrate how wild works. Not necessarily showing you a

25:01 way of doing things. Okay, the next example is something we talked

25:08 in the beginning, right? The that asks the user to enter a

25:13 of positive integers. And when they're , they Individual that they are done

25:21 giving a -1 input. Now, is not easy or not possible to

25:27 with a four loop. And so not going to show an example of

25:31 it will be done with a four . The goal is to print computer

25:37 print the sum of the numbers. , so we're gonna try to do

25:41 with a wide loop. So the program is supposed to compute and

25:48 the sum of the numbers. So get started, we're just gonna say

25:51 equal zero. And then we go um read the first number. Let's

26:00 the user, we ask the user the first number. And let's say

26:04 a new number. The user inputs number in the next number.

26:11 And then we start the loop. is just to get before we start

26:17 loop. At that point, we to not just read it once,

26:21 read it again and again and again again until the user enters -1.

26:27 do we do do that? We while number is greater, new number

26:32 greater than zero. Hopefully, the first number users entered was greater than

26:39 . That's the case. And the will go inside the loop.

26:43 So what do we want to put the loop is basically start summing.

26:50 , So he says some equal SAm new number. Because after all,

26:54 goal of this simple program was to sum the numbers that the user's input

27:00 . What else we need to do at that point we need to read

27:05 because that number is done. Now time to ask the user for the

27:10 number. So inside the loop, put the same statement, one more

27:15 saying enter another number. So the enters a number, let's say over

27:21 . They entered it. And you'll in here, going in here and

27:25 the sum will be it. Then say over here, they entered

27:30 It will go back to while the is greater than zero, it plus

27:36 it'll be 15. Let's say the entered 100 again it'll go back and

27:42 greater than zero, it will be and 15, whatever that they entered

27:48 60 18. And then the user done and then they indicated by saying

27:54 one at that point, new number is that is basically reading the user

28:03 , its value will be minus And then when the execution goes back

28:07 hear the condition, new number is than zero is not through any male

28:14 one is not greater than zero. execution will stop and then the loop

28:22 will stop and you will continue the after this and uh You'll go and

28:30 the total at that point. Hopefully makes sense. Um just one more

28:40 , this is and this is an of the type of situation where you

28:45 that iterations are have ended. Very character is given as input in this

28:51 it was the user input of And now we're gonna look at the

28:57 example we discussed a few minutes What do you do if you have

29:03 iterate until this total reaches 1000 or general until a dynamic condition is

29:11 Okay, So let's see here. you're going to write a program keeps

29:19 or donations is the input and as as the reach a certain point then

29:27 stop. So you say max equals . That's the maximum you're collect.

29:34 this is your current. How much have before you start executing. You

29:38 nothing. Your current total of donations zero. And then you write the

29:45 at her Which will run until you collected 1000. So here is the

29:54 . You're going to continue the rest the cord until the current total is

30:00 , is less than max. As as it reaches max you're done.

30:05 in this you can guess what you do is you'll get the new input

30:10 added to the current total. That's all you need to do.

30:13 everything should work out. So here take the user input donation physical to

30:21 the import enter amount that is the donation. And And then you update

30:31 current total with the donation. So say the first donation is the

30:38 Then the current total Will be Then that's the next is 100.

30:44 at 1:45.5. That's a magically somebody you 900 and then your total becomes

30:54 plus 100 plus 44.5. It's over and current total is less than

31:02 Current total is 1000 something max is . That will not be true and

31:08 will not execute these statements. The loop is finished. You go to

31:14 court after that and there you are saying print Mission mission accomplished. You've

31:22 how many dollars and then you print total. Okay. So the point

31:26 was that you're running these iterations again again and again. You don't know

31:31 many times but it ends when the reaches 1000 and again. This pattern

31:36 also very common. So those are the examples we're gonna see. So

31:43 just gonna wrap up the while versus loop. Example, when do you

31:51 while and when should you use So the key insights are really a

31:58 loop is less effort on the programmer . So less effort easier. It

32:04 means it will be easier to debug you're less likely to make mistakes.

32:09 when a four loop works, it's to use a for loop And when

32:14 a full loop work? You the number of iterations needed? If

32:18 know, it's either this as you through a list of people's names or

32:23 list of numbers or a range of . If that's how many times it

32:28 to run, then most likely you be just fine with a four

32:33 However, when you don't know beforehand many iterations something will take then while

32:40 is either the best choice and sometimes only choice. So we'll end with

32:46 little um um little factoid uh theoretically four Lu program or problems can be

32:59 with a wide loop. So while is overall more powerful than a for

33:05 , it may not be the best to do a problem with a

33:08 but you can, but the reverse not true. The things you can

33:12 with a wild loop are beyond the of for loop. Alright, so

33:19 going to stop here uh and we'll up on this topic through the the

33:28 assignment and tutorials and everything. Thank

-
+