1
00:00:00,000 --> 00:00:03,990
Sometimes when you are writing a loop you need a way to shortcut the loop, or to

2
00:00:04,000 --> 00:00:07,990
break out of the loop, and Python has controls for this.

3
00:00:08,000 --> 00:00:14,990
Let's go ahead and make a working copy of loopcontrol.py. I'll call this

4
00:00:15,000 --> 00:00:18,990
loopcontrol-working.py and go ahead and open that working copy.

5
00:00:19,000 --> 00:00:22,990
Here, we have a simple loop that prints out each character of a string.

6
00:00:23,000 --> 00:00:25,990
It looks like it's just printing the string, but it's actually doing it one

7
00:00:26,000 --> 00:00:27,990
character at a time in the loop.

8
00:00:28,000 --> 00:00:30,990
I'll go ahead and run this, and you'll see that's what it looks like.

9
00:00:31,000 --> 00:00:37,990
Let's say that we wanted to skip all of the letter Ss. We could simply say if c =='s':

10
00:00:38,000 --> 00:00:39,990
and continue.

11
00:00:40,000 --> 00:00:42,990
What continue does is it shortcuts the loop.

12
00:00:43,000 --> 00:00:46,990
It just goes back to the beginning and gets to the next iteration without doing

13
00:00:47,000 --> 00:00:48,990
whatever comes after it.

14
00:00:49,000 --> 00:00:52,990
If we save this and run, we see that we skip all the Ss.

15
00:00:53,000 --> 00:00:55,990
On the other hand, maybe we want to end at the first s. Maybe we were

16
00:00:56,000 --> 00:01:01,990
just looking for the first s. So, we can use break, and break jumps out of loop entirely.

17
00:01:02,000 --> 00:01:02,990
Let me save this and run it.

18
00:01:03,000 --> 00:01:04,990
That terminates the loop.

19
00:01:05,000 --> 00:01:09,990
It just brings the first 3 characters, and when it gets that first s, it stops entirely.

20
00:01:10,000 --> 00:01:11,990
So, that's break and continue.

21
00:01:12,000 --> 00:01:15,990
Finally, there is one more control in Python.

22
00:01:16,000 --> 00:01:20,990
It's called else, and it works just like else does in an if statement.

23
00:01:21,000 --> 00:01:27,990
If I say else here, and print ('else') and get rid of this break,

24
00:01:28,000 --> 00:01:32,990
what this does is this gets run whenever this condition becomes completely false.

25
00:01:33,000 --> 00:01:37,990
In other words, when the iterator is out of stuff to iterate, then else gets used.

26
00:01:38,000 --> 00:01:42,990
So, if I save this and run it, you see that it prints the entire string and

27
00:01:43,000 --> 00:01:43,990
then it prints else.

28
00:01:44,000 --> 00:01:49,990
And else also works in a while loop - actually all of these controls also work in while loop.

29
00:01:50,000 --> 00:01:52,990
I am just going to show you this one. I am going to do this as a while loop

30
00:01:53,000 --> 00:01:59,990
here, we'll say i= 0 and while (i<len(s)).

31
00:02:00,000 --> 00:02:05,990
We don't have this c here any more. We can say s so i, and then we'll need to increment.

32
00:02:06,000 --> 00:02:08,990
All right, here we go.

33
00:02:09,000 --> 00:02:13,990
So, let's save this and run it, and we see that does exactly the same thing.

34
00:02:14,000 --> 00:02:16,990
So, when this condition becomes false, in other words when we've

35
00:02:17,000 --> 00:02:21,990
incremented past the end of the string, then else gets used, and it prints

36
00:02:22,000 --> 00:02:22,990
the else down there.

37
00:02:23,000 --> 00:02:29,990
So, that's break and continue and else, and these are controls that will break

38
00:02:30,000 --> 00:02:30,990
entirely out of loop.

39
00:02:31,000 --> 00:02:35,990
will shortcut a loop with continue, or else is used when the condition becomes

40
00:02:36,000 --> 00:02:39,990
completely false, or if it's never true in the first place.

41
00:02:40,000 --> 00:02:50,000
So, that's break, continue and else.

