Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,330 --> 00:00:01,230
Welcome back.
2
00:00:01,230 --> 00:00:07,680
Up until now we've learned that with looping we can use four but there's actually a another way for
3
00:00:07,680 --> 00:00:08,670
us to loop.
4
00:00:08,670 --> 00:00:17,340
And it's called instead of a for loop a while loop and while loop is a little different in that we say
5
00:00:17,400 --> 00:00:24,140
while a condition is happening do something.
6
00:00:25,080 --> 00:00:27,870
So you can see that there's a bit of a difference here.
7
00:00:27,900 --> 00:00:41,010
For example I can say that while zero is less than 50 do something or I can say for example I equals
8
00:00:41,010 --> 00:00:53,380
to zero and I'm going to say while i is less than 50 print I now if I ran this program what do you think
9
00:00:53,380 --> 00:00:56,540
will happen let's find out if I click Run
10
00:00:59,990 --> 00:01:00,470
right.
11
00:01:00,500 --> 00:01:01,130
Do you see that.
12
00:01:01,670 --> 00:01:05,660
I don't know if you can tell but I'm scrolling right now through all of this.
13
00:01:05,660 --> 00:01:12,350
It's looping looping looping looping looping and you see that the programming still running as a matter
14
00:01:12,350 --> 00:01:19,640
of fact I have to stop it because what it was doing is it was looping and looping and looping because
15
00:01:19,640 --> 00:01:25,910
if we look at the code well zero is always going to be less than 50.
16
00:01:25,910 --> 00:01:34,880
So what happened was while this was true we printed AI and then the Python interpreter went back to
17
00:01:34,940 --> 00:01:37,850
line 2 says hey is zero still less than 50.
18
00:01:37,930 --> 00:01:38,080
Yeah.
19
00:01:38,220 --> 00:01:38,520
OK.
20
00:01:38,570 --> 00:01:41,600
Next line print is zero still less than 50.
21
00:01:41,610 --> 00:01:43,420
Yeah ok next line print.
22
00:01:43,490 --> 00:01:46,150
And it kept going keep going keep going.
23
00:01:46,190 --> 00:01:50,190
Now this is what we call an infinite loop.
24
00:01:50,330 --> 00:01:53,780
That is the program doesn't know when to stop.
25
00:01:53,780 --> 00:01:58,370
I mean we're telling it to just keep going forever for eternity.
26
00:01:58,580 --> 00:02:00,710
And I have some safeguards in place here.
27
00:02:00,720 --> 00:02:08,480
But if you did this in code you'd get this infinite loop until your program crashes if you actually
28
00:02:08,480 --> 00:02:09,910
ran this code.
29
00:02:10,070 --> 00:02:16,250
Well it's going to keep running until the machine just doesn't have enough power anymore enough resources
30
00:02:16,460 --> 00:02:18,310
to keep doing this over and over.
31
00:02:18,320 --> 00:02:20,350
Everything has limits right.
32
00:02:20,360 --> 00:02:26,660
So with a while loop we have to be really careful this infinite loop can be very dangerous.
33
00:02:26,690 --> 00:02:35,510
So how can we solve this we had this idea of a break statement and break is.
34
00:02:35,560 --> 00:02:39,150
Well just another key word in Python.
35
00:02:39,550 --> 00:02:47,560
And as it as you may have guessed if I click Run look at that it only runs once as soon as Python sees
36
00:02:47,710 --> 00:02:51,830
break it's going to say All right break out of the wild.
37
00:02:52,030 --> 00:02:55,940
So it's kind of go to line five and it's going to say I got nothing there.
38
00:02:55,940 --> 00:02:56,920
Program's done.
39
00:02:57,070 --> 00:03:01,330
I'm going to exit out of the program but OK.
40
00:03:01,360 --> 00:03:06,300
What if I want this to be looped 50 times.
41
00:03:06,400 --> 00:03:08,430
How can we solve that issue.
42
00:03:08,440 --> 00:03:20,590
Think about well we can just simply say I equals I plus 1 and then make sure that we change this too.
43
00:03:20,650 --> 00:03:27,220
I now what's going to happen if I click Run we stop.
44
00:03:27,330 --> 00:03:34,440
Well at forty nine we've done it 50 times we've lived through it 50 times because we kept incrementing
45
00:03:34,530 --> 00:03:42,100
I over and over so that I each time we came back to the Loop was incremented by 1.
46
00:03:42,180 --> 00:03:47,190
Now a shorthand for this we've seen it before is to do plus equals one.
47
00:03:47,250 --> 00:03:51,170
So that if I run look at that same thing.
48
00:03:51,190 --> 00:03:58,670
So we're also able to break out of the while loop as long as we're able to turn a condition into force.
49
00:03:58,750 --> 00:04:00,830
So you'll see this a lot as well.
50
00:04:02,110 --> 00:04:09,730
So to jump out of a while loop you can either turn the condition false or you can break out of the while
51
00:04:09,730 --> 00:04:11,560
loop.
52
00:04:11,570 --> 00:04:18,230
Now another interesting thing that we can do with a while loop is we can have else blocks.
53
00:04:18,230 --> 00:04:24,980
And here we can say print done with all the work.
54
00:04:25,310 --> 00:04:28,830
And if I click run here at the very end.
55
00:04:28,830 --> 00:04:31,270
Look at that done with all the work.
56
00:04:31,530 --> 00:04:33,750
And this should read like English right.
57
00:04:33,840 --> 00:04:41,070
While I is less than 50 keep looping through here and as soon as we get to the last part where I becomes
58
00:04:41,070 --> 00:04:48,530
50 where 50 is no longer less than 50 Python interpreter is going to say all right I don't care about
59
00:04:48,530 --> 00:04:49,580
anything under there.
60
00:04:49,610 --> 00:04:52,640
Let's go to the next line and I get otherwise.
61
00:04:52,670 --> 00:04:55,650
So in this case we don't have anything else happening.
62
00:04:55,790 --> 00:05:03,070
We can print done with all the work but you're probably asking yourself.
63
00:05:03,070 --> 00:05:04,830
Hold on.
64
00:05:05,020 --> 00:05:15,470
Can't I just not do this and just make sure my indentation is at the end click Run and that that works
65
00:05:15,470 --> 00:05:15,890
right.
66
00:05:15,890 --> 00:05:20,020
Why would I ever want to write else here.
67
00:05:20,100 --> 00:05:22,770
What else is a special case here.
68
00:05:22,770 --> 00:05:25,590
There is one thing that is useful.
69
00:05:25,590 --> 00:05:35,230
That is if we added a for example a break statement here and I click run you'll see that to the else
70
00:05:35,380 --> 00:05:47,610
statement does not print and that is because the else block will only execute if there isn't a break.
71
00:05:47,640 --> 00:05:54,970
That is if the while condition turns to false then it's going to keep going and do the else.
72
00:05:55,200 --> 00:06:00,570
But if there is a break it's going to break out of this whole thing and go to the eighth line.
73
00:06:01,050 --> 00:06:09,120
So a little gotcha here that you won't see else too often next to while but there is that special case
74
00:06:09,120 --> 00:06:17,400
where else is use when you want to make sure that this will be executed only if there isn't a break
75
00:06:17,540 --> 00:06:18,420
statement.
76
00:06:18,720 --> 00:06:25,320
Only if the loop is finished doing whatever it's doing without the breaks then let's take a break and
77
00:06:25,320 --> 00:06:27,390
explore while loops a little bit more.
7534
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.