1
00:00:05,360 --> 00:00:09,620
So in this video we're gonna talk about
the continue, break, and also else.

2
00:00:09,620 --> 00:00:11,460
Which I know we have talked
about a little bit in the past.

3
00:00:11,460 --> 00:00:13,460
Talk about these three key words.

4
00:00:13,460 --> 00:00:16,750
Now when you're actually coding, sometimes
you may need to interrupt the normal

5
00:00:16,750 --> 00:00:19,680
flow of a loop to either
jump out of it completely or

6
00:00:19,680 --> 00:00:22,260
to stop the current iteration and
move on to the next.

7
00:00:22,260 --> 00:00:24,510
Now once again in this example,
gonna actually show you how to do that,

8
00:00:24,510 --> 00:00:27,130
we're going to use a list even
though we haven't actually

9
00:00:27,130 --> 00:00:28,770
covered what lists are yet.

10
00:00:28,770 --> 00:00:32,200
So hopefully you can see that we're just
using them as a sequences of strings.

11
00:00:32,200 --> 00:00:35,210
But don't worry cuz we're gonna be
taking a good look at what lists are in

12
00:00:35,210 --> 00:00:35,930
a later video.

13
00:00:35,930 --> 00:00:37,710
Let's just type up a little
bit of code to get started.

14
00:00:37,710 --> 00:00:39,380
I'm gonna type, shopping.

15
00:00:39,380 --> 00:00:44,520
List equals: milk,

16
00:00:44,520 --> 00:00:51,610
pasta, eggs, spam, bread, rice.

17
00:00:51,610 --> 00:00:55,250
That's the list of strings effects,
as you can see there and

18
00:00:55,250 --> 00:00:56,540
we can do something like four item.

19
00:00:56,540 --> 00:01:00,080
You've seen this before
in shopping lists colon,

20
00:01:00,080 --> 00:01:04,769
print by plus item.

21
00:01:04,769 --> 00:01:08,600
And you've probably seen similar code to,
we have seen similar code to this if we

22
00:01:08,600 --> 00:01:13,380
actually run it we get going through each
item in the list and actually printing out

23
00:01:13,380 --> 00:01:17,280
the word Buy + whatever the item is in
the list over here on the right hand side.

24
00:01:17,280 --> 00:01:21,450
Now that's simple enough but in true
Monty Python fashion we don't like spam.

25
00:01:21,450 --> 00:01:24,100
So, we could check for
the current item not being spam and

26
00:01:24,100 --> 00:01:26,630
before actually printing the reminder.

27
00:01:26,630 --> 00:01:29,890
But, in the loop with a lot of processing
it can often be preferable to skip

28
00:01:29,890 --> 00:01:30,950
the item we don't want.

29
00:01:30,950 --> 00:01:32,470
So, this is what continuants do.

30
00:01:32,470 --> 00:01:35,220
So, what we can do is actually
put an If Test in there.

31
00:01:35,220 --> 00:01:38,990
So, we can come in here and
we can put if item.

32
00:01:38,990 --> 00:01:43,670
Is ==, two equal signs because we're
trying to compare to see whether or

33
00:01:43,670 --> 00:01:47,610
not in simple, it is equal or
not as opposed to an assignment operator.

34
00:01:47,610 --> 00:01:53,630
So if it = 'spam': you type continue,
getting our indent levels correct.

35
00:01:53,630 --> 00:01:55,400
In other words, we're just going
to leave the print in there.

36
00:01:55,400 --> 00:01:56,460
So you can see what should happen.

37
00:01:56,460 --> 00:01:59,650
Going through the list,
if it finds an item with the name of spam,

38
00:01:59,650 --> 00:02:00,960
it's gonna continue.

39
00:02:00,960 --> 00:02:02,650
And we'll see what that does shortly.

40
00:02:02,650 --> 00:02:03,680
But failing that,

41
00:02:03,680 --> 00:02:06,100
it's actually just gonna print out
the items as it was doing before.

42
00:02:06,100 --> 00:02:09,759
So now if I actually run this,
you can see that

43
00:02:09,759 --> 00:02:12,690
we've still got all the output we
had before, but one thing's missing.

44
00:02:12,690 --> 00:02:16,030
You can see what happened was spam
is now missing from the list.

45
00:02:16,030 --> 00:02:21,060
So what happens is continue actually stops
processing any more lines in the block and

46
00:02:21,060 --> 00:02:24,370
forces the for loop to move on to
the next value in the sequence.

47
00:02:24,370 --> 00:02:26,000
So in other words,
what happened was I got to spam,

48
00:02:26,000 --> 00:02:28,770
realized that there was a match there so
it looked at that line.

49
00:02:28,770 --> 00:02:31,370
When it found that it wasn't
equal to spam, continue.

50
00:02:31,370 --> 00:02:35,960
Effectively bypassed this altogether, so
I didn't actually process line six, and

51
00:02:35,960 --> 00:02:38,780
went back to line two and
moved on to the next line.

52
00:02:38,780 --> 00:02:40,450
So you can see there we were at eggs.

53
00:02:40,450 --> 00:02:43,940
We were at eggs which is the item before
spam, and we conveniently bypass that and

54
00:02:43,940 --> 00:02:45,130
went straight on to bread.

55
00:02:45,130 --> 00:02:49,430
So that's what continue does, it actually
bypasses, or actually stops processing for

56
00:02:49,430 --> 00:02:51,720
that particular block of code and
forces the for

57
00:02:51,720 --> 00:02:54,790
loop again to move on to
the next value in the sequence.

58
00:02:54,790 --> 00:02:58,120
And again it's very important just to note
which I'm going to harp on throughout this

59
00:02:58,120 --> 00:02:58,900
course.

60
00:02:58,900 --> 00:03:01,180
Be very careful with your
indenting here as always.

61
00:03:01,180 --> 00:03:05,470
The code print is indented to
be at the same level as If so

62
00:03:05,470 --> 00:03:07,610
it's not part of the block
code control base.

63
00:03:07,610 --> 00:03:11,830
So again, if we move this over here,
we gonna get a different result.

64
00:03:11,830 --> 00:03:13,970
In this case,
we're getting no output at all,

65
00:03:13,970 --> 00:03:15,580
which is probably very confusing for you.

66
00:03:15,580 --> 00:03:18,850
So, make sure in the same level cause
what we wanna do is you want to print

67
00:03:18,850 --> 00:03:20,560
basically each iteration of the loop and

68
00:03:20,560 --> 00:03:23,820
it's only if hits continue that
we actually go back to that item.

69
00:03:23,820 --> 00:03:26,680
And just as at an aside we could have
done something like this, we could have

70
00:03:26,680 --> 00:03:33,460
actually printed a message here to say,
print I'm ignoring, plus item.

71
00:03:33,460 --> 00:03:37,370
And cuz it's at the same intent level,
if we run that buy milk, buy pasta,

72
00:03:37,370 --> 00:03:40,310
buy eggs, I'm ignoring spam,
buy bread, buy rice.

73
00:03:40,310 --> 00:03:44,660
So, you can see we could have actually
added that indent level if we wanted to.

74
00:03:44,660 --> 00:03:47,650
Now, a similar statement
to continue is the break.

75
00:03:47,650 --> 00:03:51,570
So it's possible to break out of a for
loop completely using the break command.

76
00:03:51,570 --> 00:03:56,140
So if we actually replace continue with
break, we're not gonna remind it to buy

77
00:03:56,140 --> 00:03:59,270
bread or rice because the loops gonna
terminate when it gets to spam.

78
00:03:59,270 --> 00:04:01,090
So let's just see the difference there.

79
00:04:01,090 --> 00:04:04,230
So we just from this again
without my extra print line,

80
00:04:04,230 --> 00:04:07,590
as you can see we've got the five
items there with no spam.

81
00:04:07,590 --> 00:04:11,550
But if we change break, I'm sorry, change
continue to break like so if you run it.

82
00:04:12,620 --> 00:04:14,080
You can see the result
we've actually got there.

83
00:04:14,080 --> 00:04:15,820
So we've actually got nothing after eggs.

84
00:04:15,820 --> 00:04:18,910
So as soon as it hits spam, it was
a match to that and the break, then,

85
00:04:18,910 --> 00:04:21,940
actually avoided, actually
processing anything else altogether.

86
00:04:21,940 --> 00:04:24,530
So it's probably not
what we wanna do here.

87
00:04:24,530 --> 00:04:28,150
But it's just a different way of doing it,
whereas continue will actually miss just

88
00:04:28,150 --> 00:04:31,830
that counter item, break is essentially
going to miss out on all the rest and

89
00:04:31,830 --> 00:04:35,670
actually exit out of the for loop, and
not do any further processing at all.

90
00:04:35,670 --> 00:04:38,510
So in this particular case,
it's probably not what we want to do here.

91
00:04:38,510 --> 00:04:41,740
Because if we're ordering a meal,
we wanted something without spam,

92
00:04:41,740 --> 00:04:43,080
we could search the list for an item.

93
00:04:43,080 --> 00:04:44,810
That would be another way of doing it.

94
00:04:44,810 --> 00:04:46,990
And obviously we want to
stop looking if we found it.

95
00:04:46,990 --> 00:04:48,620
So let's just go through and
type in that example.

96
00:04:48,620 --> 00:04:51,690
I'm gonna leave that first example as it
is, but I'll actually comment it out.

97
00:04:53,690 --> 00:04:54,750
So, let's do another scenario.

98
00:04:54,750 --> 00:05:01,220
So that's meal = ["egg", "Bacon",

99
00:05:01,220 --> 00:05:06,850
"spam", "sausages"] like so.

100
00:05:06,850 --> 00:05:08,740
We can put for item in meal.

101
00:05:08,740 --> 00:05:12,160
And you've seen that before
processing going through a list.

102
00:05:12,160 --> 00:05:15,530
And if item == 'spam' :,
two equal signs again.

103
00:05:15,530 --> 00:05:24,110
Then what we could do is we could
put nasty food_item = item.

104
00:05:24,110 --> 00:05:26,180
And break.

105
00:05:26,180 --> 00:05:33,750
Then we can come back here and
put if nasty_food_item print,

106
00:05:33,750 --> 00:05:38,910
Can't I have anything without spam in it?

107
00:05:39,910 --> 00:05:42,280
So if we actually run that.

108
00:05:42,280 --> 00:05:43,430
Can't have anything without spam in it and

109
00:05:43,430 --> 00:05:46,230
that's because we actually
found spam in the list.

110
00:05:46,230 --> 00:05:49,100
So in that particular example we
could allow the loop to run through

111
00:05:49,100 --> 00:05:50,920
all the items and it would still work.

112
00:05:50,920 --> 00:05:52,880
And we comment at the break
line to confirm that.

113
00:05:52,880 --> 00:05:54,470
So if we actually commented that out.

114
00:05:57,090 --> 00:05:59,010
We do get exactly the same result,
but in this case,

115
00:05:59,010 --> 00:06:01,500
it's not actually breaking
out of the loop this time.

116
00:06:01,500 --> 00:06:02,850
Nasically it's a good idea to do that.

117
00:06:02,850 --> 00:06:06,570
So again if you had something with
search with thousands of records here.

118
00:06:06,570 --> 00:06:10,450
So let's just say this entry for me was
thousands of items, we could actually go

119
00:06:10,450 --> 00:06:14,000
through and finish it and go right
through the entire loop without breaking.

120
00:06:14,000 --> 00:06:17,900
So in other words it would match the first
item, then continue processing the rest.

121
00:06:17,900 --> 00:06:19,650
But in this case it's smart to use break.

122
00:06:19,650 --> 00:06:23,730
Because what we're doing is as soon
as we find the first match for

123
00:06:23,730 --> 00:06:26,510
the word spam we're immediately
exiting the for loop, and

124
00:06:26,510 --> 00:06:28,110
we're not doing any further processing.

125
00:06:28,110 --> 00:06:32,640
So, again, we're being smart with the
utilization of hardware of our computer,

126
00:06:32,640 --> 00:06:35,390
and not processing any more
than we actually need to.

127
00:06:35,390 --> 00:06:38,990
So, breaks for very useful in situations
like that where we wanna terminate a loop

128
00:06:38,990 --> 00:06:41,510
earlier, when early when
some condition is met.

129
00:06:41,510 --> 00:06:45,400
Now with that said, there's a serious
actual problem with the program above.

130
00:06:45,400 --> 00:06:46,650
Now did you actually spot that?

131
00:06:46,650 --> 00:06:49,980
See if you can't spot that without
actually running the program again.

132
00:06:49,980 --> 00:06:53,360
And the hint to look for is what would
happen if there's no spam in the list.

133
00:06:53,360 --> 00:06:54,240
So did you spot it?

134
00:06:54,240 --> 00:06:56,710
What if you remove span from the list
you're actually gonna get an error.

135
00:06:56,710 --> 00:06:57,350
So let's just do that,

136
00:06:57,350 --> 00:07:01,860
we'll call that "tomato" and
run it we actually get an error.

137
00:07:01,860 --> 00:07:05,310
And you can see the error is
nasty_food_item is not defined.

138
00:07:05,310 --> 00:07:08,260
And the error that it's crashing
on is actually line 15 when trying

139
00:07:08,260 --> 00:07:09,540
to do this test here.

140
00:07:09,540 --> 00:07:12,710
And if we actually have a look we
should be able to see line 15,

141
00:07:12,710 --> 00:07:14,050
it says line 15 over here.

142
00:07:14,050 --> 00:07:19,080
So it's crashing because we didn't
find an item with the name spam.

143
00:07:19,080 --> 00:07:22,640
Nasty_food_item was never set to a value.

144
00:07:22,640 --> 00:07:24,460
So at first glance, if looking at it,

145
00:07:24,460 --> 00:07:28,540
you might think that you can rewrite this
line and change something like this.

146
00:07:28,540 --> 00:07:31,390
See if nasty_food_item is equal to spam.

147
00:07:31,390 --> 00:07:32,770
But that might help.

148
00:07:32,770 --> 00:07:35,670
But it doesn't actually make any
difference in the scheme of things,

149
00:07:35,670 --> 00:07:38,930
because nasty_food_item
has never been defined

150
00:07:38,930 --> 00:07:42,560
because ultimately spam was
never found in the list here.

151
00:07:42,560 --> 00:07:45,490
Now if you actually look carefully in
Intelli J it's a little bit harder

152
00:07:45,490 --> 00:07:47,970
to see on my screen, but
it should be a little bit easier for you.

153
00:07:47,970 --> 00:07:51,880
At the far right of line 15,
and actually hovering over,

154
00:07:51,880 --> 00:07:53,700
if you hover over it
you can see a warning.

155
00:07:53,700 --> 00:07:56,300
And if you actually just hover
over there and have a look,

156
00:07:56,300 --> 00:07:58,660
name nasty_food_item cannot be defined.

157
00:07:58,660 --> 00:08:02,470
So unless you get a green tick at the top
right hand corner out of your current

158
00:08:02,470 --> 00:08:06,240
editing window in IntelliJ it's likely
there's a problem in your code somewhere.

159
00:08:06,240 --> 00:08:10,070
And it's actually a good idea to
investigate every one of those warnings to

160
00:08:10,070 --> 00:08:10,910
see what they are.

161
00:08:10,910 --> 00:08:14,370
And ideally, you should be able to resolve
pretty well all of them before you

162
00:08:14,370 --> 00:08:17,730
actually run your code because they're
there as a warning to tell you that

163
00:08:17,730 --> 00:08:19,970
probably what you've
done is you've created.

164
00:08:19,970 --> 00:08:23,440
So you made a mistake in your source code
somewhere and it may not be enough of

165
00:08:23,440 --> 00:08:26,660
a mistake to have caused an error
to actually highlighting an error.

166
00:08:26,660 --> 00:08:30,250
It may not crash your program but it is
actually IntelliJ's way to come back to

167
00:08:30,250 --> 00:08:32,809
you to say that this is
something you need to relook at.

168
00:08:32,809 --> 00:08:35,400
So how do we go ahead and
actually cure this problem?

169
00:08:35,400 --> 00:08:39,720
So, the cure here is to make sure that
we initialize our variables to a value

170
00:08:39,720 --> 00:08:40,870
before we start the loop.

171
00:08:40,870 --> 00:08:44,360
Cuz what you can see is in
here nasty_food_item is at

172
00:08:44,360 --> 00:08:47,900
only ever initialized for the first time
when we're actually in that loop, and

173
00:08:47,900 --> 00:08:50,650
in this case if we actually
find the item is spam.

174
00:08:50,650 --> 00:08:54,770
So what we actually should have done
is actually initialized this up here.

175
00:08:54,770 --> 00:08:59,160
So just under meal,
we could put nasty_food_item, and

176
00:08:59,160 --> 00:09:03,120
put two quotes there to initialize it for
the first time.

177
00:09:03,120 --> 00:09:06,170
And then we should find that this
is actually gonna work for us.

178
00:09:06,170 --> 00:09:08,460
And the reason it's gonna work
is because no matter what,

179
00:09:08,460 --> 00:09:13,160
nasty_food__item has now been
initialized on line ten.

180
00:09:13,160 --> 00:09:14,330
So, if we run this and

181
00:09:14,330 --> 00:09:18,700
we get no output and the reason we get no
output at the moment is because remember I

182
00:09:18,700 --> 00:09:22,540
changed spam to tomato but
notice there's no errors anymore.

183
00:09:22,540 --> 00:09:26,340
And if we put our little cursor over
here we've got four weak warnings but

184
00:09:26,340 --> 00:09:27,600
no major warning here.

185
00:09:27,600 --> 00:09:30,370
So that's a sign that everything's okay.

186
00:09:30,370 --> 00:09:33,500
And you can see some of the warnings
are quite minor, like a saying, so

187
00:09:33,500 --> 00:09:37,180
hover over this one on line 7a,
no uline at the end of file.

188
00:09:37,180 --> 00:09:38,560
Missing watt space after comma.

189
00:09:38,560 --> 00:09:41,800
So what it's actually saying there is it
really wants us to actually put a space

190
00:09:41,800 --> 00:09:42,600
there for those.

191
00:09:44,180 --> 00:09:45,850
And notice how that disappeared
when I actually did that.

192
00:09:45,850 --> 00:09:49,490
So it does actually make sense to
actually read these warnings and

193
00:09:49,490 --> 00:09:51,250
see if you can actually resolve them.

194
00:09:51,250 --> 00:09:54,400
So now what that probably wants
us is to press enter like that.

195
00:09:54,400 --> 00:09:56,660
And notice how added one extra
blank blank line at the end and

196
00:09:56,660 --> 00:09:58,060
that actually made it happy.

197
00:09:58,060 --> 00:10:01,300
So again, pay attention to the warnings
that IntelligJ's popping up for

198
00:10:01,300 --> 00:10:02,740
you, cuz they're normally there for
a reason.

199
00:10:02,740 --> 00:10:03,880
They're there to guide you and

200
00:10:03,880 --> 00:10:07,320
ultimately to make you a better
programmer by helping you find common

201
00:10:07,320 --> 00:10:11,390
errors that are actually, programmers
actually put into their code by mistake.

202
00:10:11,390 --> 00:10:14,030
Okay, so we looked at break and
continue and

203
00:10:14,030 --> 00:10:17,490
we've actually seen else when we
looked at testing conditions using if.

204
00:10:17,490 --> 00:10:23,340
But we can also use else at the end of
loops and probably the way that it's

205
00:10:23,340 --> 00:10:27,730
been defined, it's an unfortunate word
to use else for what it does with loops.

206
00:10:27,730 --> 00:10:30,910
Because its use doesn't really
imply otherwise in any sense.

207
00:10:30,910 --> 00:10:31,910
What it does,

208
00:10:31,910 --> 00:10:36,340
it causes a block to be executed if the
loop was allowed to continue to the end.

209
00:10:36,340 --> 00:10:38,600
In other words,
if the loop was not broken out of.

210
00:10:38,600 --> 00:10:41,430
We can demonstrate that again and just
before I do that, just to confirm that

211
00:10:41,430 --> 00:10:46,300
this code was going to work, I'll change
that back to spam and we'll run it again.

212
00:10:46,300 --> 00:10:48,450
Can't have anything without spam in it so
I can say that did work.

213
00:10:48,450 --> 00:10:51,440
I want you to also pay attention
to the little green arrow in

214
00:10:51,440 --> 00:10:52,750
the top right-hand corner, now.

215
00:10:52,750 --> 00:10:56,490
We actually fixed all those errors up and
there's now no problems found.

216
00:10:56,490 --> 00:11:00,720
So, in an ideal world, what you'll wanna
do is every time you're creating a program

217
00:11:00,720 --> 00:11:03,860
you should be able to see that little
green tick there to indicate that

218
00:11:03,860 --> 00:11:08,100
as far as IntelliJ's concerned,
a lot of the basic errors that crop up

219
00:11:08,100 --> 00:11:09,970
have been addressed and
you've actually fixed those.

220
00:11:09,970 --> 00:11:13,980
Still not guaranteed the program won't
crash, but chances are you've identified

221
00:11:13,980 --> 00:11:17,690
a lot of those common faults
that we fixed up in this video.

222
00:11:17,690 --> 00:11:21,050
So, let's go ahead and
actually look to see how the else works.

223
00:11:21,050 --> 00:11:23,820
And as I said, else works for
the for loop in this case.

224
00:11:23,820 --> 00:11:25,900
So, we can type in else,
we can actually put it here.

225
00:11:28,160 --> 00:11:32,490
Print all.

226
00:11:32,490 --> 00:11:38,760
I'll have a plate of that then please.

227
00:11:41,040 --> 00:11:44,660
We can leave everything else as it was,
and if we actually run it, But

228
00:11:44,660 --> 00:11:47,920
one problem actually still remains when we
do that is that we don't end up ordering

229
00:11:47,920 --> 00:11:49,440
the meal if it doesn't contain spam.

230
00:11:49,440 --> 00:11:52,710
So we know the meal is spam-free if the
loop is not broken out of which is exactly

231
00:11:52,710 --> 00:11:55,150
the situation that
the else was designed for.

232
00:11:55,150 --> 00:11:57,220
So now if we change spam to say beans.

233
00:11:59,500 --> 00:12:01,010
Like so, and we actually run it again.

234
00:12:02,040 --> 00:12:05,170
I'll have a plate of that then please,
and I'll actually fix my typo.

235
00:12:06,190 --> 00:12:08,730
So, I can say the else
is only going to work

236
00:12:08,730 --> 00:12:10,650
if the if was never actually executed.

237
00:12:10,650 --> 00:12:12,970
In other words, if we actually
never broke out of the code.

238
00:12:12,970 --> 00:12:16,080
If we leave spam in, the loop does break,
and the else block is ignored.

239
00:12:16,080 --> 00:12:17,240
So you can see what's happening there.

240
00:12:17,240 --> 00:12:19,280
So, again if I go back,
make that spam again.

241
00:12:20,750 --> 00:12:25,040
The else block isn't executed because
we did actually break out of the code.

242
00:12:25,040 --> 00:12:26,920
So that's what the else is there for,
and as I said,

243
00:12:26,920 --> 00:12:29,350
it's probably not the best name.

244
00:12:29,350 --> 00:12:32,260
Probably a bit of
an unfortunate word to use for

245
00:12:32,260 --> 00:12:35,660
that because its use doesn't really
imply otherwise in any sense.

246
00:12:35,660 --> 00:12:38,570
So again what it does,
it causes the block to be executed

247
00:12:38,570 --> 00:12:41,950
if the loop was allowed to continue to the
end, in other words, there was no break.

248
00:12:41,950 --> 00:12:45,830
And again, as always be very
careful with your indentations,

249
00:12:45,830 --> 00:12:50,650
particularly tricky here is that else can
follow both an if and a for statement so

250
00:12:50,650 --> 00:12:52,410
you can quite easily put an else there.

251
00:12:53,810 --> 00:12:56,510
And actually got a completely different
result because the else is now

252
00:12:56,510 --> 00:12:59,810
attached to the if,
as opposed to attaching it to the for.

253
00:12:59,810 --> 00:13:05,040
So really do make sure you spend some
time early on getting your indentation

254
00:13:05,040 --> 00:13:09,540
levels correct or you'll actually have
all sorts of weird problems happening and

255
00:13:09,540 --> 00:13:10,750
you'll be wondering why.

256
00:13:10,750 --> 00:13:13,760
And very common there when you're starting
out with parsing you have these sorts of

257
00:13:13,760 --> 00:13:17,350
errors, which are basically to
do with indentation levels.

258
00:13:17,350 --> 00:13:19,720
And incidentally if we
leave that beans in there,

259
00:13:19,720 --> 00:13:23,820
the else could be a good actual place to
actually initialize our nasty_food_item.

260
00:13:23,820 --> 00:13:26,600
So I could have actually
initialized it at that point.

261
00:13:28,460 --> 00:13:30,330
And that will now work and the reason for

262
00:13:30,330 --> 00:13:33,900
that is the else here was only going to
be called if we haven't broken out of it.

263
00:13:33,900 --> 00:13:37,840
And a break here is actually only breaking
because we're actually looking for

264
00:13:37,840 --> 00:13:38,530
the word spam.

265
00:13:38,530 --> 00:13:42,030
So if we run this, that's another
way of actually doing it as well.

266
00:13:42,030 --> 00:13:44,810
So that basically ensures that
the variables, that are fine for

267
00:13:44,810 --> 00:13:48,370
the loop if the loop terminates
normally without a break.

268
00:13:48,370 --> 00:13:50,970
Now whichever is better is
probably personal preference.

269
00:13:50,970 --> 00:13:53,840
My background's been weaned on languages
like Pascal where everything has

270
00:13:53,840 --> 00:13:55,350
to be declared before it's used.

271
00:13:55,350 --> 00:13:55,870
So for

272
00:13:55,870 --> 00:13:59,570
that reason I would normally recommend
you actually put the code up here.

273
00:13:59,570 --> 00:14:02,650
So you'd initialize it at the top so
it's clearer.

274
00:14:02,650 --> 00:14:05,730
And I think a lot of programs will
probably look for that as well.

275
00:14:05,730 --> 00:14:09,220
But again it is a feature of Python if you
want to you can actually define it in that

276
00:14:09,220 --> 00:14:11,540
else method as well.

277
00:14:11,540 --> 00:14:14,170
There will certainly be plenty of
people who would argue with this and

278
00:14:14,170 --> 00:14:17,110
say that it should actually
be done in the else.

279
00:14:17,110 --> 00:14:19,750
One thing I would say, that once you've
actually decided on which way you're gonna

280
00:14:19,750 --> 00:14:23,060
do things, be consistent and
do it the same way every time.

281
00:14:23,060 --> 00:14:25,900
That's not only gonna help you when
you revisit your current months later.

282
00:14:25,900 --> 00:14:29,380
But it's also gonna help other people who
read and maintain the code you've written.

283
00:14:29,380 --> 00:14:32,130
Another thing I wanna point out
that the actual variable name,

284
00:14:32,130 --> 00:14:37,530
nasty_food_item isn't consistent with
the names that we've used in the past.

285
00:14:37,530 --> 00:14:41,230
We've actually named our variables
with what's called camel case.

286
00:14:41,230 --> 00:14:45,710
And that's something like
nasty_food_item is not like that,

287
00:14:45,710 --> 00:14:50,500
we would have done something like this,
nastyFoodItem.

288
00:14:50,500 --> 00:14:52,330
So starting with the lower case for
the first word and

289
00:14:52,330 --> 00:14:56,480
then each subsequent word we capitalize
it and that's called camel case.

290
00:14:56,480 --> 00:14:57,390
We could have done it that way but

291
00:14:57,390 --> 00:14:59,320
i wanted to show you that there's
different conventions for

292
00:14:59,320 --> 00:15:00,970
variable names in Python.

293
00:15:00,970 --> 00:15:05,070
Now languages like Java and C have very
clear preferences that most people adopt.

294
00:15:05,070 --> 00:15:08,800
But Python doesn't really have a firm
convention to follow in this respect, so

295
00:15:08,800 --> 00:15:10,340
there's no sort of one way of doing it.

296
00:15:10,340 --> 00:15:14,140
There's no sort of common scenario that
says you should write your code this way.

297
00:15:14,140 --> 00:15:18,220
So again whichever way you prefer to do
it, whether you want to write a creative

298
00:15:18,220 --> 00:15:21,410
variable with words like
separated by underscores or

299
00:15:21,410 --> 00:15:25,640
you want to use camel case which is the
word first word being lower case then each

300
00:15:25,640 --> 00:15:30,920
word after that capitalized with a letter
to help separating the actual words.

301
00:15:30,920 --> 00:15:33,950
The thing here would be to actually be
consistent again and you've come from

302
00:15:33,950 --> 00:15:37,400
languages like Java and C it's probably
a good idea just to follow the same

303
00:15:37,400 --> 00:15:41,260
conventions just to make sure that if
you are going back to those languages,

304
00:15:41,260 --> 00:15:43,640
you know that should bring,
at least, your habits back to them.

305
00:15:43,640 --> 00:15:47,170
But again, completely personal preference,
whichever way you prefer it,

306
00:15:47,170 --> 00:15:48,290
it's up to you.

307
00:15:48,290 --> 00:15:51,120
Again, I can't stress enough that
make sure you're actually consistent.

308
00:15:51,120 --> 00:15:53,690
So we're gonna be consistent
within individual video topics,

309
00:15:53,690 --> 00:15:57,070
but we're gonna actually use both styles
just so you get used to them both.

310
00:15:57,070 --> 00:16:00,270
Just another reminder also before
I end this video is that Python

311
00:16:00,270 --> 00:16:01,980
variables are case sensitive.

312
00:16:01,980 --> 00:16:07,570
So in other words, nastyFoodItem

313
00:16:07,570 --> 00:16:12,580
is actually different to nasty_food_item
on the first time with the lower case n.

314
00:16:12,580 --> 00:16:13,850
Two entirely different variables.

315
00:16:13,850 --> 00:16:16,940
All right, so
I'm just gonna delete that code there and

316
00:16:16,940 --> 00:16:18,900
we're going to end the video here now.

317
00:16:18,900 --> 00:16:23,410
In the next video we're actually going
to look at Augmented Assignment, and

318
00:16:23,410 --> 00:16:25,920
I know that sounds like a mouthful, but
you'll be finding out what that is and

319
00:16:25,920 --> 00:16:27,380
how to use it in the next video.

