1
00:00:05,130 --> 00:00:08,520
Welcome back, everyone, in this lecture, we're going to discuss while loops.

2
00:00:09,860 --> 00:00:15,890
Now, while loops are loops that are going to continue executing a block of code, while some condition

3
00:00:15,890 --> 00:00:21,920
remains true, unlike for loops that end up iterating through a sequence in order to execute blocks

4
00:00:21,920 --> 00:00:24,110
of code while loops keep going.

5
00:00:24,380 --> 00:00:30,740
So for example, while my pool is not full, keep filling my pool of water and then stop the loop when

6
00:00:30,740 --> 00:00:31,490
the pool is full.

7
00:00:31,790 --> 00:00:36,860
And we can do it with just a simple line of code saying while, for example, while my dogs are still

8
00:00:36,860 --> 00:00:38,240
hungry, keep feeding my dogs.

9
00:00:39,550 --> 00:00:45,400
So this means in the syntax of a wire loop, we're actually checking while some bullying condition is

10
00:00:45,400 --> 00:00:50,860
doing something, execute a block of code, you can almost think of this more like an if statement that

11
00:00:50,860 --> 00:00:56,500
just keeps going over and over and over again for that condition until that condition is false.

12
00:00:56,950 --> 00:00:59,920
In fact, let's go ahead and jump to our code editor and check this out.

13
00:01:00,160 --> 00:01:02,200
OK, so here I am back at the coding editor.

14
00:01:02,530 --> 00:01:08,830
But I'm going to first start off with is assigning some variable end to a number like one.

15
00:01:09,460 --> 00:01:11,570
And then what I'm going to do is set up my while.

16
00:01:11,710 --> 00:01:15,460
So I say while and then I have to figure out some sort of condition.

17
00:01:15,760 --> 00:01:20,650
Now you always want to be careful for a while, loop not to do something like while true colon.

18
00:01:21,600 --> 00:01:27,240
Prints some letters here, because this is going to basically execute forever.

19
00:01:27,690 --> 00:01:30,750
It's going to be while this is true, always run this block of code.

20
00:01:30,750 --> 00:01:35,180
So you really want to be careful of something like this because that essentially says, run this forever,

21
00:01:35,190 --> 00:01:36,630
never break out of this wild loop.

22
00:01:37,050 --> 00:01:43,140
So instead, we want conditions that are probably going to be affected either by some outside source

23
00:01:43,140 --> 00:01:47,790
that we're checking or internally within the block of code inside the wire loop.

24
00:01:48,240 --> 00:01:54,860
So going say something like while M is less than five, I'm going to print out the current value events

25
00:01:54,870 --> 00:01:59,040
I'm going to print out and is currently.

26
00:02:00,000 --> 00:02:02,850
And then let's use a string literal printing here.

27
00:02:03,420 --> 00:02:09,620
So I'm going to say, while an is less than five print F and is currently RN.

28
00:02:10,199 --> 00:02:14,340
And then if I just run this by itself, there's no change to an ever.

29
00:02:14,340 --> 00:02:15,870
So this is going to print out forever.

30
00:02:16,260 --> 00:02:19,080
Hey, and it's currently one and is currently one and is currently one.

31
00:02:19,290 --> 00:02:20,180
I want to avoid that.

32
00:02:20,190 --> 00:02:23,340
Otherwise I'd have to somehow figure out how to kill the Python kernel.

33
00:02:23,760 --> 00:02:26,730
Instead, I want to make sure I can update the current value of.

34
00:02:27,660 --> 00:02:30,990
So then I'm going to say NW is equal to and.

35
00:02:32,130 --> 00:02:32,760
Plus one.

36
00:02:33,270 --> 00:02:36,690
And so now let's think about what's happening as this keeps looping.

37
00:02:37,230 --> 00:02:39,420
So it's going to start off with an equal to one.

38
00:02:39,840 --> 00:02:42,930
It's going to report back and is equal to currently one.

39
00:02:43,380 --> 00:02:49,380
And then it's going to reassign men to be equal to the current value of N plus one, which is then going

40
00:02:49,380 --> 00:02:50,630
to be one plus one.

41
00:02:50,640 --> 00:02:51,480
So it's going to be two.

42
00:02:51,900 --> 00:02:54,600
There's going to run this again and it's going to be three, then it's to be four.

43
00:02:54,780 --> 00:03:00,000
And then suddenly, once it hits five five, it's no longer less than five and the while Loop will stop

44
00:03:00,000 --> 00:03:00,570
executing.

45
00:03:01,140 --> 00:03:07,740
So you should see the results look something like this and is currently one two three four and then

46
00:03:07,740 --> 00:03:08,910
it stops executing.

47
00:03:09,720 --> 00:03:16,110
So while loops can be really useful when you have the ability to check something that's happening outside

48
00:03:16,110 --> 00:03:22,380
the wire loop, we could do something like Prince Hey, Server is up.

49
00:03:23,040 --> 00:03:26,430
And then in the while loop, I could have some other function.

50
00:03:26,430 --> 00:03:32,430
Let's say somewhere else in some other piece of code that could be something like check server.

51
00:03:32,670 --> 00:03:38,580
And then I would have here something like, let's say, server condition or a server up.

52
00:03:39,000 --> 00:03:45,060
And then I would have this be something like, OK, have the check server report back whether or not

53
00:03:45,060 --> 00:03:46,680
my server is up true or false?

54
00:03:46,950 --> 00:03:51,060
And then I can print out, OK, the servers up continually until it's not up.

55
00:03:51,420 --> 00:03:55,200
So that's the kind of thing I could do if a while loop again, we haven't really learned about functions

56
00:03:55,200 --> 00:03:55,350
yet.

57
00:03:55,380 --> 00:03:56,760
We're about to learn that next.

58
00:03:57,080 --> 00:03:59,580
I just want you to give you some ideas of how this could be useful.

59
00:03:59,790 --> 00:04:05,220
It's when you want something to continually go over and over and over again in a loop until some condition

60
00:04:05,340 --> 00:04:06,570
is no longer true.

61
00:04:06,630 --> 00:04:08,760
Again, it's while some condition is true.

62
00:04:09,060 --> 00:04:10,050
Keep doing something.

63
00:04:10,470 --> 00:04:10,740
All right.

64
00:04:11,130 --> 00:04:12,630
So that's it for the wire loop.

65
00:04:13,080 --> 00:04:15,000
Coming up next, we're going to discuss functions.

