1
00:00:04,480 --> 00:00:09,500
Let's talk about while loops in this
video. Python provides two main ways to

2
00:00:09,500 --> 00:00:14,450
loop around a block of code; for loops
and while loops. We've seen how a for loop

3
00:00:14,450 --> 00:00:18,560
can be used to iterate through an
iterable. In our examples we use the

4
00:00:18,560 --> 00:00:23,390
items in a sequence and the numbers in a
range. Sometimes we need to keep looping

5
00:00:23,390 --> 00:00:28,130
as long as some condition is true and
stop when it becomes False. We do this

6
00:00:28,130 --> 00:00:36,200
using a while loop. So the basic form is
while condition : execute block of code.

7
00:00:36,200 --> 00:00:40,420
The condition can be anything that can
evaluate to True or False, and so long as

8
00:00:40,420 --> 00:00:44,640
the condition is True, the code in the
loop will be executed. When the condition

9
00:00:44,640 --> 00:00:50,680
becomes False, the loop terminates.
Alright, so I think a few examples will help here.

10
00:00:50,680 --> 00:01:04,099
Let's create a new Python
file. We'll call this whileloop.py.

11
00:01:04,099 --> 00:01:06,800
Alright so I'm going to start here by
using a while loop to do the same as the

12
00:01:06,800 --> 00:01:10,250
for loop that we've seen in previous
videos. Firstly, I'm gonna type in the

13
00:01:10,250 --> 00:01:18,500
original loop, so it's for i in range
parentheses 10 colon print parentheses

14
00:01:18,500 --> 00:01:25,180
double quotes i is now, left and right
curly braces dot format

15
00:01:25,180 --> 00:01:31,180
and i in parentheses. Then we've got two closing
right parentheses there. Remember that

16
00:01:31,189 --> 00:01:34,759
the starting value of a range is
optional and will default to zero if not

17
00:01:34,760 --> 00:01:39,040
specified. You should be able to work out
what this code will do before running it.

18
00:01:39,040 --> 00:01:45,110
So when I do run it now, you can see the
output down the bottom - we've got the

19
00:01:45,110 --> 00:01:52,620
values zero through nine. Alright so I'm
going to comment that code out,

20
00:01:52,620 --> 00:01:56,420
and we're gonna start instead on line four
and we're going to use a while loop to do

21
00:01:56,420 --> 00:02:01,860
the same thing. So I'm gonna type i
equals zero on line four. Next line while

22
00:02:01,860 --> 00:02:11,720
i is less than 10 colon, then print
indented parentheses double quotes i is now.

23
00:02:11,720 --> 00:02:17,740
We've got left and right curly braces,
format then i in parentheses, then our two

24
00:02:17,740 --> 00:02:24,520
closing right parentheses. Then
we'll put i plus equals 1. Okay.

25
00:02:24,520 --> 00:02:28,180
So there's our code. So line seven just adds
one to the current value of i,

26
00:02:28,180 --> 00:02:35,410
and basically, that line is identical to
this line, i equals i plus 1. Now we're

27
00:02:35,410 --> 00:02:38,230
going to talk about this later in this
section. The code on line seven is

28
00:02:38,230 --> 00:02:42,430
preferable to the code on line eight, and
I want you to see things written in the

29
00:02:42,430 --> 00:02:47,760
best way whenever possible. Alright so
I'm going to delete line eight.

30
00:02:47,760 --> 00:02:51,540
What I'm also going to do now is set a breakpoint
on line five before running the code in

31
00:02:51,549 --> 00:02:58,540
the debugger. Now i is bound to the value
zero on line four and the debugger is

32
00:02:58,540 --> 00:03:04,180
going to be stopped on line five. Let's
do that. We'll run the debugger, and you

33
00:03:04,180 --> 00:03:08,340
can see we stopped on line five there.
If we click over to the console tab,

34
00:03:08,340 --> 00:03:11,560
we'll see the output as we step through
the code. So we run line five ready to

35
00:03:11,560 --> 00:03:17,769
test if the value of i is less than 10.
We can see that it is, so if we step over,

36
00:03:17,769 --> 00:03:23,769
it takes us to line 6. We're gonna step over
again to print the value of i. The value

37
00:03:23,769 --> 00:03:28,020
of i is now printed in the console. So
we're now on line seven, as you can see.

38
00:03:28,020 --> 00:03:31,760
So this is that strange way of increasing
the value of i that I just mentioned.

39
00:03:31,760 --> 00:03:36,600
Watch the value of i that's shown on
line four as I step over again.

40
00:03:36,600 --> 00:03:41,520
1 is being added to i. It now has the value of
1, 0 plus 1 is 1. So we're still less than

41
00:03:41,530 --> 00:03:46,720
10, so if we step over again, it takes us
back to line 6. Step over once more to

42
00:03:46,720 --> 00:03:51,310
print the value and we move to line 7.
Alright so we're now on line 7. That's

43
00:03:51,310 --> 00:03:55,150
going to add 1 to i again which should
give us the value 2. We should also go

44
00:03:55,150 --> 00:04:00,069
back to line 5 to loop again. Notice that
I'm not mindlessly clicking the step

45
00:04:00,069 --> 00:04:03,220
over button. When you're using the
debugger like this to understand your

46
00:04:03,220 --> 00:04:07,359
code, make sure you stop on each line and
work out what's going to happen. As I

47
00:04:07,360 --> 00:04:11,900
said earlier, we're learning to become
programmers, not mindless button clickers.

48
00:04:11,900 --> 00:04:16,839
Alright so let's see if we were right.
Step over, and i does have the

49
00:04:16,839 --> 00:04:20,680
value 2 and we have gone back to line 5.
Next time we go around the loop we

50
00:04:20,680 --> 00:04:24,550
should get the message i is now 2 printed
out, and i will be increased to 3 and

51
00:04:24,550 --> 00:04:27,180
after that happens we'll get back to
line 5.

52
00:04:27,180 --> 00:04:30,880
I'm going to use resume program over
here this time. If you prefer to step

53
00:04:30,880 --> 00:04:33,960
over a few more times then go ahead and
do that. You don't need to watch me

54
00:04:33,960 --> 00:04:37,600
clicking the button repeatedly. Pause the
video frequently if you do need to catch

55
00:04:37,610 --> 00:04:42,979
up, and if I click on machine program now, we
have got i is now 2 printed and we can

56
00:04:42,979 --> 00:04:49,669
see that I has been incremented to 3. Resume
program again, i is now 4. I'm going

57
00:04:49,669 --> 00:04:52,910
to click on the resume program 5 more
times until i has the value 9 and we're

58
00:04:52,910 --> 00:04:58,340
back on line 5. Now things are going to get
interesting, so I'm going to come over here and

59
00:04:58,340 --> 00:05:04,460
use step over once more to print
the value of i. So I'm going to step over

60
00:05:04,460 --> 00:05:11,270
here, i is now 9, and when I step over one
more time to execute line 7, we're back

61
00:05:11,270 --> 00:05:16,460
on line 5 with i having the value of 10.
10 isn't less than 10. That means the

62
00:05:16,460 --> 00:05:20,060
condition on line 5 will be False now
and we'll exit the loop. Because there's

63
00:05:20,060 --> 00:05:23,510
no more code in the program that means
the program should terminate. So let's

64
00:05:23,510 --> 00:05:25,970
see if we were right. I'm going to click
step over one more time to execute

65
00:05:25,970 --> 00:05:32,600
line 5, and you can see that has in fact
happened. The program has now terminated.

66
00:05:32,600 --> 00:05:35,210
And we were right - there's no more
output from our code and we get the

67
00:05:35,210 --> 00:05:40,039
message: process finished with exit code
zero produced by the IDE. Notice that our

68
00:05:40,039 --> 00:05:44,630
programs output appears in black and
messages from the IDE normally appear in

69
00:05:44,630 --> 00:05:49,430
blue. It sometimes uses red for errors
and other important information.

70
00:05:49,430 --> 00:05:53,690
Alright so that's a while loop. As we saw, a
while loop will continue to loop, as long

71
00:05:53,690 --> 00:05:58,280
as the condition evaluates to True. When
the condition evaluates to False, the

72
00:05:58,280 --> 00:06:03,050
loop terminates and execution continues
with the code after the loop. We don't

73
00:06:03,050 --> 00:06:06,590
have any more code, so the program
terminated as well. Alright I'll stop

74
00:06:06,590 --> 00:06:10,740
the video here and in the next one we'll
look at a few more things about while loops.

75
00:06:10,740 --> 00:06:13,760
See you in the next video.

