1
00:00:04,780 --> 00:00:09,440
Sometimes you may need to interrupt the
normal flow of a loop, to either jump out

2
00:00:09,440 --> 00:00:13,760
of it completely, or stop the current
iteration and move on to the next one.

3
00:00:13,760 --> 00:00:17,420
In the next two videos, we're going to look
at the continue and break statements

4
00:00:17,420 --> 00:00:22,310
that we can use with loops. We need some
examples to show how these statements

5
00:00:22,310 --> 00:00:26,480
can be useful, so I'm going to start by
introducing lists. We will be looking at

6
00:00:26,480 --> 00:00:29,810
lists in more detail in the next section
of this course, so this is a very quick

7
00:00:29,810 --> 00:00:34,850
introduction to what a list is. So list
in Python is an ordered sequence of

8
00:00:34,850 --> 00:00:41,360
values enclosed in square brackets. For
example, we might have a shopping list.

9
00:00:41,360 --> 00:00:52,320
So what I'm going to do is create a new
Python file here called shopping.py

10
00:00:52,320 --> 00:00:57,110
and let's start by defining a list.
So I'm going to type shopping underscore

11
00:00:57,110 --> 00:01:03,140
list is equal to and square braces, as I
mentioned, or brackets as the case, as you

12
00:01:03,140 --> 00:01:08,030
may call them, and I'm going to enter
some items that we might buy when we go

13
00:01:08,030 --> 00:01:11,180
out shopping. I'm going to put, and
because they're strings I'm going to put

14
00:01:11,180 --> 00:01:16,140
them in double quotes, so milk is the
first one, separating each one, each item,

15
00:01:16,140 --> 00:01:22,180
by comma pasta comma and obviously, as
you can see, double quotes around all of

16
00:01:22,189 --> 00:01:28,759
them comma because they're strings,
spam comma bread comma and the last one

17
00:01:28,759 --> 00:01:32,210
rice, and then that hasn't got a comma
because it's the last one and then we've

18
00:01:32,210 --> 00:01:37,220
got our square brackets as well. So that
defines our list containing six items.

19
00:01:37,220 --> 00:01:41,600
Now you saw that those items were strings
but we could use numbers instead. In fact,

20
00:01:41,600 --> 00:01:45,289
you can store any type of object in a
list in Python, but we'll stick with

21
00:01:45,289 --> 00:01:49,460
strings at the moment. So make sure you
do use square brackets when creating the

22
00:01:49,460 --> 00:01:53,929
list, as I've done. So we've bound the
variable shopping_list to our

23
00:01:53,929 --> 00:01:58,219
list and we can now use that in a for
loop to print out all the items. So let's

24
00:01:58,220 --> 00:02:04,580
add some code for that: for item in
shopping_list colon.

25
00:02:04,580 --> 00:02:12,400
Then line 4, print parentheses
double quotes buy space plus item

26
00:02:12,900 --> 00:02:20,840
and if we run this now,

27
00:02:20,840 --> 00:02:24,320
and we now get a reminder to
buy each item in the list.

28
00:02:24,320 --> 00:02:28,620
So that's simple enough. It's a basic for
loop that iterates over the list and

29
00:02:28,629 --> 00:02:32,709
prints out a message containing each
item. But what if we don't like spam?

30
00:02:32,709 --> 00:02:36,700
Well we've seen one way to deal with
that. We could check for the current item

31
00:02:36,700 --> 00:02:41,220
not being spam,d before printing the
reminder. So let's try some code there.

32
00:02:41,220 --> 00:02:50,200
So we could add it above the print: if item is
not equal to, spam in double quotes colon and

33
00:02:50,200 --> 00:02:55,030
then we need to indent the print. So
we're only printing if the item isn't

34
00:02:55,030 --> 00:02:58,540
spam. And looking at the list down the
bottom, it currently does have spam. If we

35
00:02:58,540 --> 00:03:04,880
run the program, you can see that spam
has now been excluded from the output.

36
00:03:04,880 --> 00:03:07,840
So that's one way to exclude
spam from the output, but there's

37
00:03:07,840 --> 00:03:11,740
another way in loops and that's to use
continue. So let's see how that works.

38
00:03:11,740 --> 00:03:14,530
What I'm going to do is comment out this
loop, just for now, so you've still got

39
00:03:14,530 --> 00:03:21,040
the code if you're downloading it, and
let's try another approach. Line 7, I'm

40
00:03:21,040 --> 00:03:28,090
going to type for item in shopping under
score list : if item is equal to, so two

41
00:03:28,090 --> 00:03:36,009
equal signs spam in double quotes :
continue, otherwise I'm going to go down

42
00:03:36,009 --> 00:03:42,430
to line 11 there, print parentheses
rather, double quotes buy space in those

43
00:03:42,430 --> 00:03:47,049
double quotes plus item. Then we've got
our right parenthesis. Now remember to be

44
00:03:47,049 --> 00:03:49,600
very careful with your indenting here, as
always.

45
00:03:49,600 --> 00:03:54,759
The call to print is indented to be at
the same level as if, so it's not part of

46
00:03:54,760 --> 00:03:59,360
the block controlled by if. So when I run
this, looking at the output we had last

47
00:03:59,360 --> 00:04:02,379
time down the bottom of the screen,
running it now, you can see we've got the

48
00:04:02,379 --> 00:04:07,000
identical output. So spam is still not
included. Basically, all the items except

49
00:04:07,000 --> 00:04:10,780
spam have been printed. Alright, so
let's have a closer look now at what's

50
00:04:10,780 --> 00:04:14,709
happening here. So we're going to set a
breakpoint on line 8 here, that's the if

51
00:04:14,709 --> 00:04:23,200
checking to see whether the item is spam,
and let's run the program now in the debugger.

52
00:04:23,940 --> 00:04:27,080
Then we're going to kick straight over
to the console tab, if you're using

53
00:04:27,080 --> 00:04:30,100
IntelliJ or PyCharm, then obviously I'm
using IntelliJ, as you can see, because we

54
00:04:30,100 --> 00:04:34,340
can see the values of our variables
printed in the Edit window at the top.

55
00:04:34,340 --> 00:04:37,860
Alright so we've entered the for loop and
you can see the item is currently milk.

56
00:04:37,860 --> 00:04:43,940
That doesn't equal spam so therefore
line 9 won't be executed, so we step over,

57
00:04:43,940 --> 00:04:49,180
and you can see we step straight over to
line 11. So if I step over again now,

58
00:04:49,180 --> 00:04:53,720
you can see buy milk has been printed out to
the console and we're back now again at

59
00:04:53,720 --> 00:04:58,130
line 7.
So step over again, and this time you can

60
00:04:58,130 --> 00:05:02,479
see the item has changed to pasta. Now
that also doesn't equal spam, so I'm

61
00:05:02,480 --> 00:05:08,640
going to step over twice more to get
back to line 7, and you can see buy pasta

62
00:05:08,640 --> 00:05:13,160
has appeared in the output pane as well.
The next item will be eggs, so I'm going

63
00:05:13,160 --> 00:05:18,940
to step over three more times; one, you
can see item's now eggs, and two more times,

64
00:05:18,940 --> 00:05:22,100
and buy eggs has appeared in the console
window and we're back up to line seven

65
00:05:22,100 --> 00:05:26,389
again. So next we're going to get to spam,
so I'm going to step over it and we

66
00:05:26,389 --> 00:05:32,030
should get to line eight. You can see
item is spam. This time the condition will

67
00:05:32,030 --> 00:05:37,660
be True and line 9 will execute.
Let's step over to check that.

68
00:05:37,660 --> 00:05:43,060
Alright so we're now ready to execute the continue
statement on line 9. continue causes

69
00:05:43,060 --> 00:05:46,060
all the remaining code in
the block to be skipped,

70
00:05:46,060 --> 00:05:49,820
so execution will jump straight back to
the for statement. So let's step over and

71
00:05:49,820 --> 00:05:54,710
confirm that, and you can see what
happened there. We jumped right back to

72
00:05:54,710 --> 00:05:58,760
line 7. So that's what continue does.
When it appears in a loop it causes

73
00:05:58,760 --> 00:06:01,370
everything else in the
loop to be ignored.

74
00:06:01,370 --> 00:06:07,680
We've avoided doing any processing when
the item is spam, so if we step over again,

75
00:06:07,680 --> 00:06:13,000
you can see item is now bread. I'll
continue stepping through now, to let the

76
00:06:13,000 --> 00:06:18,720
program finish, and you can see the
program has now completed. Alright.

77
00:06:18,720 --> 00:06:22,940
So that's what continue does. You probably
won't use it very often. It's there if

78
00:06:22,940 --> 00:06:26,210
you need it and you now know what it
does, if you come across it in code that

79
00:06:26,210 --> 00:06:29,780
you have to work with.
I'll finish this video by comparing our

80
00:06:29,780 --> 00:06:32,560
two bits of code.

81
00:06:33,060 --> 00:06:37,919
The code that's currently commented out
on lines 3 through 5, produces exactly the same

82
00:06:37,919 --> 00:06:43,320
result as the continue code on lines 7
through 11. If you've decided that you

83
00:06:43,320 --> 00:06:47,669
never have to use continue, then you're
correct. In fact, a lot of languages don't

84
00:06:47,669 --> 00:06:51,990
have the equivalent of a continue
keyword. Where it can be useful, though, is

85
00:06:51,990 --> 00:06:56,730
in making code easier to read. So check
out the responses to this stack overflow

86
00:06:56,730 --> 00:07:00,900
question that I'm about to put on the
screen. Link, as always, is in the

87
00:07:00,900 --> 00:07:06,210
resources section of the video, and
someone's asking the question there, as

88
00:07:06,210 --> 00:07:11,160
you can see, some example, for some
example uses of continue, and there's

89
00:07:11,160 --> 00:07:16,200
some good examples there you can read
through, of using continue. Basically, some

90
00:07:16,200 --> 00:07:21,800
other opinions from people there, about
continue. Certainly a useful read.

91
00:07:21,800 --> 00:07:25,540
Alright, so coming up next we're going to
start talking about the break statement.

92
00:07:25,540 --> 00:07:28,620
See you in the next video.

