WEBVTT 1 00:00:02.280 --> 00:00:06.540 So in this video we're gonna talk about the continue, break, and also else. 2 00:00:06.540 --> 00:00:08.380 Which I know we have talked about a little bit in the past. 3 00:00:08.380 --> 00:00:10.380 Talk about these three key words. 4 00:00:10.380 --> 00:00:13.670 Now when you're actually coding, sometimes you may need to interrupt the normal 5 00:00:13.670 --> 00:00:16.600 flow of a loop to either jump out of it completely or 6 00:00:16.600 --> 00:00:19.185 to stop the current iteration and move on to the next. 7 00:00:19.185 --> 00:00:21.430 Now once again in this example, gonna actually show you how to do that, 8 00:00:21.430 --> 00:00:24.050 we're going to use a list even though we haven't actually 9 00:00:24.050 --> 00:00:25.690 covered what lists are yet. 10 00:00:25.690 --> 00:00:29.120 So hopefully you can see that we're just using them as a sequences of strings. 11 00:00:29.120 --> 00:00:32.130 But don't worry cuz we're gonna be taking a good look at what lists are in 12 00:00:32.130 --> 00:00:32.850 a later video. 13 00:00:32.850 --> 00:00:34.630 Let's just type up a little bit of code to get started. 14 00:00:34.630 --> 00:00:36.303 I'm gonna type, shopping. 15 00:00:36.303 --> 00:00:41.449 List equals: milk, 16 00:00:41.449 --> 00:00:48.530 pasta, eggs, spam, bread, rice. 17 00:00:48.530 --> 00:00:52.170 That's the list of strings effects, as you can see there and 18 00:00:52.170 --> 00:00:53.460 we can do something like four item. 19 00:00:53.460 --> 00:00:57.006 You've seen this before in shopping lists colon, 20 00:00:57.006 --> 00:01:01.690 print by plus item. 21 00:01:01.690 --> 00:01:05.520 And you've probably seen similar code to, we have seen similar code to this if we 22 00:01:05.520 --> 00:01:10.300 actually run it we get going through each item in the list and actually printing out 23 00:01:10.300 --> 00:01:14.200 the word Buy + whatever the item is in the list over here on the right hand side. 24 00:01:14.200 --> 00:01:18.370 Now that's simple enough but in true Monty Python fashion we don't like spam. 25 00:01:18.370 --> 00:01:21.020 So, we could check for the current item not being spam and 26 00:01:21.020 --> 00:01:23.550 before actually printing the reminder. 27 00:01:23.550 --> 00:01:26.810 But, in the loop with a lot of processing it can often be preferable to skip 28 00:01:26.810 --> 00:01:27.870 the item we don't want. 29 00:01:27.870 --> 00:01:29.390 So, this is what continuants do. 30 00:01:29.390 --> 00:01:32.140 So, what we can do is actually put an If Test in there. 31 00:01:32.140 --> 00:01:35.912 So, we can come in here and we can put if item. 32 00:01:35.912 --> 00:01:40.590 Is ==, two equal signs because we're trying to compare to see whether or 33 00:01:40.590 --> 00:01:44.530 not in simple, it is equal or not as opposed to an assignment operator. 34 00:01:44.530 --> 00:01:50.550 So if it = 'spam': you type continue, getting our indent levels correct. 35 00:01:50.550 --> 00:01:52.320 In other words, we're just going to leave the print in there. 36 00:01:52.320 --> 00:01:53.380 So you can see what should happen. 37 00:01:53.380 --> 00:01:56.570 Going through the list, if it finds an item with the name of spam, 38 00:01:56.570 --> 00:01:57.880 it's gonna continue. 39 00:01:57.880 --> 00:01:59.570 And we'll see what that does shortly. 40 00:01:59.570 --> 00:02:00.600 But failing that, 41 00:02:00.600 --> 00:02:03.020 it's actually just gonna print out the items as it was doing before. 42 00:02:03.020 --> 00:02:06.680 So now if I actually run this, you can see that 43 00:02:06.680 --> 00:02:09.610 we've still got all the output we had before, but one thing's missing. 44 00:02:09.610 --> 00:02:12.950 You can see what happened was spam is now missing from the list. 45 00:02:12.950 --> 00:02:17.980 So what happens is continue actually stops processing any more lines in the block and 46 00:02:17.980 --> 00:02:21.290 forces the for loop to move on to the next value in the sequence. 47 00:02:21.290 --> 00:02:22.928 So in other words, what happened was I got to spam, 48 00:02:22.928 --> 00:02:25.690 realized that there was a match there so it looked at that line. 49 00:02:25.690 --> 00:02:28.290 When it found that it wasn't equal to spam, continue. 50 00:02:28.290 --> 00:02:32.880 Effectively bypassed this altogether, so I didn't actually process line six, and 51 00:02:32.880 --> 00:02:35.700 went back to line two and moved on to the next line. 52 00:02:35.700 --> 00:02:37.379 So you can see there we were at eggs. 53 00:02:37.379 --> 00:02:40.864 We were at eggs which is the item before spam, and we conveniently bypass that and 54 00:02:40.864 --> 00:02:42.053 went straight on to bread. 55 00:02:42.053 --> 00:02:46.352 So that's what continue does, it actually bypasses, or actually stops processing for 56 00:02:46.352 --> 00:02:48.645 that particular block of code and forces the for 57 00:02:48.645 --> 00:02:51.710 loop again to move on to the next value in the sequence. 58 00:02:51.710 --> 00:02:55.040 And again it's very important just to note which I'm going to harp on throughout this 59 00:02:55.040 --> 00:02:55.820 course. 60 00:02:55.820 --> 00:02:58.100 Be very careful with your indenting here as always. 61 00:02:58.100 --> 00:03:02.390 The code print is indented to be at the same level as If so 62 00:03:02.390 --> 00:03:04.530 it's not part of the block code control base. 63 00:03:04.530 --> 00:03:08.750 So again, if we move this over here, we gonna get a different result. 64 00:03:08.750 --> 00:03:10.890 In this case, we're getting no output at all, 65 00:03:10.890 --> 00:03:12.500 which is probably very confusing for you. 66 00:03:12.500 --> 00:03:15.770 So, make sure in the same level cause what we wanna do is you want to print 67 00:03:15.770 --> 00:03:17.480 basically each iteration of the loop and 68 00:03:17.480 --> 00:03:20.740 it's only if hits continue that we actually go back to that item. 69 00:03:20.740 --> 00:03:23.600 And just as at an aside we could have done something like this, we could have 70 00:03:23.600 --> 00:03:30.380 actually printed a message here to say, print I'm ignoring, plus item. 71 00:03:30.380 --> 00:03:34.290 And cuz it's at the same intent level, if we run that buy milk, buy pasta, 72 00:03:34.290 --> 00:03:37.230 buy eggs, I'm ignoring spam, buy bread, buy rice. 73 00:03:37.230 --> 00:03:41.580 So, you can see we could have actually added that indent level if we wanted to. 74 00:03:41.580 --> 00:03:44.570 Now, a similar statement to continue is the break. 75 00:03:44.570 --> 00:03:48.490 So it's possible to break out of a for loop completely using the break command. 76 00:03:48.490 --> 00:03:53.060 So if we actually replace continue with break, we're not gonna remind it to buy 77 00:03:53.060 --> 00:03:56.190 bread or rice because the loops gonna terminate when it gets to spam. 78 00:03:56.190 --> 00:03:58.010 So let's just see the difference there. 79 00:03:58.010 --> 00:04:01.151 So we just from this again without my extra print line, 80 00:04:01.151 --> 00:04:04.512 as you can see we've got the five items there with no spam. 81 00:04:04.512 --> 00:04:08.470 But if we change break, I'm sorry, change continue to break like so if you run it. 82 00:04:09.540 --> 00:04:11.000 You can see the result we've actually got there. 83 00:04:11.000 --> 00:04:12.740 So we've actually got nothing after eggs. 84 00:04:12.740 --> 00:04:15.830 So as soon as it hits spam, it was a match to that and the break, then, 85 00:04:15.830 --> 00:04:18.860 actually avoided, actually processing anything else altogether. 86 00:04:18.860 --> 00:04:21.450 So it's probably not what we wanna do here. 87 00:04:21.450 --> 00:04:25.070 But it's just a different way of doing it, whereas continue will actually miss just 88 00:04:25.070 --> 00:04:28.750 that counter item, break is essentially going to miss out on all the rest and 89 00:04:28.750 --> 00:04:32.590 actually exit out of the for loop, and not do any further processing at all. 90 00:04:32.590 --> 00:04:35.430 So in this particular case, it's probably not what we want to do here. 91 00:04:35.430 --> 00:04:38.660 Because if we're ordering a meal, we wanted something without spam, 92 00:04:38.660 --> 00:04:40.000 we could search the list for an item. 93 00:04:40.000 --> 00:04:41.730 That would be another way of doing it. 94 00:04:41.730 --> 00:04:43.910 And obviously we want to stop looking if we found it. 95 00:04:43.910 --> 00:04:45.542 So let's just go through and type in that example. 96 00:04:45.542 --> 00:04:48.610 I'm gonna leave that first example as it is, but I'll actually comment it out. 97 00:04:50.610 --> 00:04:51.670 So, let's do another scenario. 98 00:04:51.670 --> 00:04:58.149 So that's meal = ["egg", "Bacon", 99 00:04:58.149 --> 00:05:03.770 "spam", "sausages"] like so. 100 00:05:03.770 --> 00:05:05.660 We can put for item in meal. 101 00:05:05.660 --> 00:05:09.080 And you've seen that before processing going through a list. 102 00:05:09.080 --> 00:05:12.450 And if item == 'spam' :, two equal signs again. 103 00:05:12.450 --> 00:05:21.039 Then what we could do is we could put nasty food_item = item. 104 00:05:21.039 --> 00:05:23.108 And break. 105 00:05:23.108 --> 00:05:30.675 Then we can come back here and put if nasty_food_item print, 106 00:05:30.675 --> 00:05:35.830 Can't I have anything without spam in it? 107 00:05:36.830 --> 00:05:39.200 So if we actually run that. 108 00:05:39.200 --> 00:05:40.350 Can't have anything without spam in it and 109 00:05:40.350 --> 00:05:43.150 that's because we actually found spam in the list. 110 00:05:43.150 --> 00:05:46.020 So in that particular example we could allow the loop to run through 111 00:05:46.020 --> 00:05:47.840 all the items and it would still work. 112 00:05:47.840 --> 00:05:49.800 And we comment at the break line to confirm that. 113 00:05:49.800 --> 00:05:51.391 So if we actually commented that out. 114 00:05:54.015 --> 00:05:55.937 We do get exactly the same result, but in this case, 115 00:05:55.937 --> 00:05:58.425 it's not actually breaking out of the loop this time. 116 00:05:58.425 --> 00:05:59.770 Nasically it's a good idea to do that. 117 00:05:59.770 --> 00:06:03.490 So again if you had something with search with thousands of records here. 118 00:06:03.490 --> 00:06:07.370 So let's just say this entry for me was thousands of items, we could actually go 119 00:06:07.370 --> 00:06:10.920 through and finish it and go right through the entire loop without breaking. 120 00:06:10.920 --> 00:06:14.820 So in other words it would match the first item, then continue processing the rest. 121 00:06:14.820 --> 00:06:16.570 But in this case it's smart to use break. 122 00:06:16.570 --> 00:06:20.650 Because what we're doing is as soon as we find the first match for 123 00:06:20.650 --> 00:06:23.430 the word spam we're immediately exiting the for loop, and 124 00:06:23.430 --> 00:06:25.030 we're not doing any further processing. 125 00:06:25.030 --> 00:06:29.560 So, again, we're being smart with the utilization of hardware of our computer, 126 00:06:29.560 --> 00:06:32.310 and not processing any more than we actually need to. 127 00:06:32.310 --> 00:06:35.919 So, breaks for very useful in situations like that where we wanna terminate a loop 128 00:06:35.919 --> 00:06:38.430 earlier, when early when some condition is met. 129 00:06:38.430 --> 00:06:42.320 Now with that said, there's a serious actual problem with the program above. 130 00:06:42.320 --> 00:06:43.570 Now did you actually spot that? 131 00:06:43.570 --> 00:06:46.900 See if you can't spot that without actually running the program again. 132 00:06:46.900 --> 00:06:50.280 And the hint to look for is what would happen if there's no spam in the list. 133 00:06:50.280 --> 00:06:51.160 So did you spot it? 134 00:06:51.160 --> 00:06:53.630 What if you remove span from the list you're actually gonna get an error. 135 00:06:53.630 --> 00:06:54.270 So let's just do that, 136 00:06:54.270 --> 00:06:58.780 we'll call that "tomato" and run it we actually get an error. 137 00:06:58.780 --> 00:07:02.230 And you can see the error is nasty_food_item is not defined. 138 00:07:02.230 --> 00:07:05.180 And the error that it's crashing on is actually line 15 when trying 139 00:07:05.180 --> 00:07:06.460 to do this test here. 140 00:07:06.460 --> 00:07:09.630 And if we actually have a look we should be able to see line 15, 141 00:07:09.630 --> 00:07:10.970 it says line 15 over here. 142 00:07:10.970 --> 00:07:16.005 So it's crashing because we didn't find an item with the name spam. 143 00:07:16.005 --> 00:07:19.560 Nasty_food_item was never set to a value. 144 00:07:19.560 --> 00:07:21.380 So at first glance, if looking at it, 145 00:07:21.380 --> 00:07:25.460 you might think that you can rewrite this line and change something like this. 146 00:07:25.460 --> 00:07:28.310 See if nasty_food_item is equal to spam. 147 00:07:28.310 --> 00:07:29.690 But that might help. 148 00:07:29.690 --> 00:07:32.590 But it doesn't actually make any difference in the scheme of things, 149 00:07:32.590 --> 00:07:35.850 because nasty_food_item has never been defined 150 00:07:35.850 --> 00:07:39.480 because ultimately spam was never found in the list here. 151 00:07:39.480 --> 00:07:42.410 Now if you actually look carefully in Intelli J it's a little bit harder 152 00:07:42.410 --> 00:07:44.890 to see on my screen, but it should be a little bit easier for you. 153 00:07:44.890 --> 00:07:48.800 At the far right of line 15, and actually hovering over, 154 00:07:48.800 --> 00:07:50.620 if you hover over it you can see a warning. 155 00:07:50.620 --> 00:07:53.220 And if you actually just hover over there and have a look, 156 00:07:53.220 --> 00:07:55.580 name nasty_food_item cannot be defined. 157 00:07:55.580 --> 00:07:59.390 So unless you get a green tick at the top right hand corner out of your current 158 00:07:59.390 --> 00:08:03.160 editing window in IntelliJ it's likely there's a problem in your code somewhere. 159 00:08:03.160 --> 00:08:06.990 And it's actually a good idea to investigate every one of those warnings to 160 00:08:06.990 --> 00:08:07.830 see what they are. 161 00:08:07.830 --> 00:08:11.294 And ideally, you should be able to resolve pretty well all of them before you 162 00:08:11.294 --> 00:08:14.653 actually run your code because they're there as a warning to tell you that 163 00:08:14.653 --> 00:08:16.894 probably what you've done is you've created. 164 00:08:16.894 --> 00:08:20.367 So you made a mistake in your source code somewhere and it may not be enough of 165 00:08:20.367 --> 00:08:23.583 a mistake to have caused an error to actually highlighting an error. 166 00:08:23.583 --> 00:08:27.175 It may not crash your program but it is actually IntelliJ's way to come back to 167 00:08:27.175 --> 00:08:29.730 you to say that this is something you need to relook at. 168 00:08:29.730 --> 00:08:32.320 So how do we go ahead and actually cure this problem? 169 00:08:32.320 --> 00:08:36.640 So, the cure here is to make sure that we initialize our variables to a value 170 00:08:36.640 --> 00:08:37.795 before we start the loop. 171 00:08:37.795 --> 00:08:41.280 Cuz what you can see is in here nasty_food_item is at 172 00:08:41.280 --> 00:08:44.820 only ever initialized for the first time when we're actually in that loop, and 173 00:08:44.820 --> 00:08:47.570 in this case if we actually find the item is spam. 174 00:08:47.570 --> 00:08:51.690 So what we actually should have done is actually initialized this up here. 175 00:08:51.690 --> 00:08:56.080 So just under meal, we could put nasty_food_item, and 176 00:08:56.080 --> 00:09:00.040 put two quotes there to initialize it for the first time. 177 00:09:00.040 --> 00:09:03.090 And then we should find that this is actually gonna work for us. 178 00:09:03.090 --> 00:09:05.385 And the reason it's gonna work is because no matter what, 179 00:09:05.385 --> 00:09:10.080 nasty_food__item has now been initialized on line ten. 180 00:09:10.080 --> 00:09:11.253 So, if we run this and 181 00:09:11.253 --> 00:09:15.625 we get no output and the reason we get no output at the moment is because remember I 182 00:09:15.625 --> 00:09:19.460 changed spam to tomato but notice there's no errors anymore. 183 00:09:19.460 --> 00:09:23.260 And if we put our little cursor over here we've got four weak warnings but 184 00:09:23.260 --> 00:09:24.520 no major warning here. 185 00:09:24.520 --> 00:09:27.290 So that's a sign that everything's okay. 186 00:09:27.290 --> 00:09:30.420 And you can see some of the warnings are quite minor, like a saying, so 187 00:09:30.420 --> 00:09:34.100 hover over this one on line 7a, no uline at the end of file. 188 00:09:34.100 --> 00:09:35.480 Missing watt space after comma. 189 00:09:35.480 --> 00:09:38.720 So what it's actually saying there is it really wants us to actually put a space 190 00:09:38.720 --> 00:09:39.520 there for those. 191 00:09:41.100 --> 00:09:42.770 And notice how that disappeared when I actually did that. 192 00:09:42.770 --> 00:09:46.410 So it does actually make sense to actually read these warnings and 193 00:09:46.410 --> 00:09:48.170 see if you can actually resolve them. 194 00:09:48.170 --> 00:09:51.320 So now what that probably wants us is to press enter like that. 195 00:09:51.320 --> 00:09:53.585 And notice how added one extra blank blank line at the end and 196 00:09:53.585 --> 00:09:54.985 that actually made it happy. 197 00:09:54.985 --> 00:09:58.225 So again, pay attention to the warnings that IntelligJ's popping up for 198 00:09:58.225 --> 00:09:59.665 you, cuz they're normally there for a reason. 199 00:09:59.665 --> 00:10:00.805 They're there to guide you and 200 00:10:00.805 --> 00:10:04.245 ultimately to make you a better programmer by helping you find common 201 00:10:04.245 --> 00:10:08.315 errors that are actually, programmers actually put into their code by mistake. 202 00:10:08.315 --> 00:10:10.955 Okay, so we looked at break and continue and 203 00:10:10.955 --> 00:10:14.410 we've actually seen else when we looked at testing conditions using if. 204 00:10:14.410 --> 00:10:20.260 But we can also use else at the end of loops and probably the way that it's 205 00:10:20.260 --> 00:10:24.650 been defined, it's an unfortunate word to use else for what it does with loops. 206 00:10:24.650 --> 00:10:27.830 Because its use doesn't really imply otherwise in any sense. 207 00:10:27.830 --> 00:10:28.830 What it does, 208 00:10:28.830 --> 00:10:33.260 it causes a block to be executed if the loop was allowed to continue to the end. 209 00:10:33.260 --> 00:10:35.520 In other words, if the loop was not broken out of. 210 00:10:35.520 --> 00:10:38.350 We can demonstrate that again and just before I do that, just to confirm that 211 00:10:38.350 --> 00:10:43.220 this code was going to work, I'll change that back to spam and we'll run it again. 212 00:10:43.220 --> 00:10:45.370 Can't have anything without spam in it so I can say that did work. 213 00:10:45.370 --> 00:10:48.360 I want you to also pay attention to the little green arrow in 214 00:10:48.360 --> 00:10:49.670 the top right-hand corner, now. 215 00:10:49.670 --> 00:10:53.410 We actually fixed all those errors up and there's now no problems found. 216 00:10:53.410 --> 00:10:57.640 So, in an ideal world, what you'll wanna do is every time you're creating a program 217 00:10:57.640 --> 00:11:00.780 you should be able to see that little green tick there to indicate that 218 00:11:00.780 --> 00:11:05.020 as far as IntelliJ's concerned, a lot of the basic errors that crop up 219 00:11:05.020 --> 00:11:06.898 have been addressed and you've actually fixed those. 220 00:11:06.898 --> 00:11:10.900 Still not guaranteed the program won't crash, but chances are you've identified 221 00:11:10.900 --> 00:11:14.610 a lot of those common faults that we fixed up in this video. 222 00:11:14.610 --> 00:11:17.970 So, let's go ahead and actually look to see how the else works. 223 00:11:17.970 --> 00:11:20.740 And as I said, else works for the for loop in this case. 224 00:11:20.740 --> 00:11:22.821 So, we can type in else, we can actually put it here. 225 00:11:25.083 --> 00:11:29.413 Print all. 226 00:11:29.413 --> 00:11:35.685 I'll have a plate of that then please. 227 00:11:37.968 --> 00:11:41.586 We can leave everything else as it was, and if we actually run it, But 228 00:11:41.586 --> 00:11:44.842 one problem actually still remains when we do that is that we don't end up ordering 229 00:11:44.842 --> 00:11:46.367 the meal if it doesn't contain spam. 230 00:11:46.367 --> 00:11:49.631 So we know the meal is spam-free if the loop is not broken out of which is exactly 231 00:11:49.631 --> 00:11:52.070 the situation that the else was designed for. 232 00:11:52.070 --> 00:11:54.149 So now if we change spam to say beans. 233 00:11:56.420 --> 00:11:57.930 Like so, and we actually run it again. 234 00:11:58.960 --> 00:12:02.090 I'll have a plate of that then please, and I'll actually fix my typo. 235 00:12:03.110 --> 00:12:05.650 So, I can say the else is only going to work 236 00:12:05.650 --> 00:12:07.570 if the if was never actually executed. 237 00:12:07.570 --> 00:12:09.890 In other words, if we actually never broke out of the code. 238 00:12:09.890 --> 00:12:13.000 If we leave spam in, the loop does break, and the else block is ignored. 239 00:12:13.000 --> 00:12:14.160 So you can see what's happening there. 240 00:12:14.160 --> 00:12:16.200 So, again if I go back, make that spam again. 241 00:12:17.670 --> 00:12:21.960 The else block isn't executed because we did actually break out of the code. 242 00:12:21.960 --> 00:12:23.840 So that's what the else is there for, and as I said, 243 00:12:23.840 --> 00:12:26.270 it's probably not the best name. 244 00:12:26.270 --> 00:12:29.180 Probably a bit of an unfortunate word to use for 245 00:12:29.180 --> 00:12:32.580 that because its use doesn't really imply otherwise in any sense. 246 00:12:32.580 --> 00:12:35.490 So again what it does, it causes the block to be executed 247 00:12:35.490 --> 00:12:38.870 if the loop was allowed to continue to the end, in other words, there was no break. 248 00:12:38.870 --> 00:12:42.750 And again, as always be very careful with your indentations, 249 00:12:42.750 --> 00:12:47.570 particularly tricky here is that else can follow both an if and a for statement so 250 00:12:47.570 --> 00:12:49.330 you can quite easily put an else there. 251 00:12:50.730 --> 00:12:53.430 And actually got a completely different result because the else is now 252 00:12:53.430 --> 00:12:56.730 attached to the if, as opposed to attaching it to the for. 253 00:12:56.730 --> 00:13:01.960 So really do make sure you spend some time early on getting your indentation 254 00:13:01.960 --> 00:13:06.460 levels correct or you'll actually have all sorts of weird problems happening and 255 00:13:06.460 --> 00:13:07.670 you'll be wondering why. 256 00:13:07.670 --> 00:13:10.680 And very common there when you're starting out with parsing you have these sorts of 257 00:13:10.680 --> 00:13:14.270 errors, which are basically to do with indentation levels. 258 00:13:14.270 --> 00:13:16.649 And incidentally if we leave that beans in there, 259 00:13:16.649 --> 00:13:20.740 the else could be a good actual place to actually initialize our nasty_food_item. 260 00:13:20.740 --> 00:13:23.520 So I could have actually initialized it at that point. 261 00:13:25.380 --> 00:13:27.250 And that will now work and the reason for 262 00:13:27.250 --> 00:13:30.820 that is the else here was only going to be called if we haven't broken out of it. 263 00:13:30.820 --> 00:13:34.760 And a break here is actually only breaking because we're actually looking for 264 00:13:34.760 --> 00:13:35.450 the word spam. 265 00:13:35.450 --> 00:13:38.950 So if we run this, that's another way of actually doing it as well. 266 00:13:38.950 --> 00:13:41.730 So that basically ensures that the variables, that are fine for 267 00:13:41.730 --> 00:13:45.290 the loop if the loop terminates normally without a break. 268 00:13:45.290 --> 00:13:47.890 Now whichever is better is probably personal preference. 269 00:13:47.890 --> 00:13:50.760 My background's been weaned on languages like Pascal where everything has 270 00:13:50.760 --> 00:13:52.270 to be declared before it's used. 271 00:13:52.270 --> 00:13:52.790 So for 272 00:13:52.790 --> 00:13:56.492 that reason I would normally recommend you actually put the code up here. 273 00:13:56.492 --> 00:13:59.570 So you'd initialize it at the top so it's clearer. 274 00:13:59.570 --> 00:14:02.650 And I think a lot of programs will probably look for that as well. 275 00:14:02.650 --> 00:14:06.145 But again it is a feature of Python if you want to you can actually define it in that 276 00:14:06.145 --> 00:14:08.460 else method as well. 277 00:14:08.460 --> 00:14:11.090 There will certainly be plenty of people who would argue with this and 278 00:14:11.090 --> 00:14:14.030 say that it should actually be done in the else. 279 00:14:14.030 --> 00:14:16.670 One thing I would say, that once you've actually decided on which way you're gonna 280 00:14:16.670 --> 00:14:19.988 do things, be consistent and do it the same way every time. 281 00:14:19.988 --> 00:14:22.820 That's not only gonna help you when you revisit your current months later. 282 00:14:22.820 --> 00:14:26.300 But it's also gonna help other people who read and maintain the code you've written. 283 00:14:26.300 --> 00:14:29.055 Another thing I wanna point out that the actual variable name, 284 00:14:29.055 --> 00:14:34.450 nasty_food_item isn't consistent with the names that we've used in the past. 285 00:14:34.450 --> 00:14:38.157 We've actually named our variables with what's called camel case. 286 00:14:38.157 --> 00:14:42.639 And that's something like nasty_food_item is not like that, 287 00:14:42.639 --> 00:14:47.420 we would have done something like this, nastyFoodItem. 288 00:14:47.420 --> 00:14:49.250 So starting with the lower case for the first word and 289 00:14:49.250 --> 00:14:53.400 then each subsequent word we capitalize it and that's called camel case. 290 00:14:53.400 --> 00:14:54.310 We could have done it that way but 291 00:14:54.310 --> 00:14:56.245 i wanted to show you that there's different conventions for 292 00:14:56.245 --> 00:14:57.895 variable names in Python. 293 00:14:57.895 --> 00:15:01.995 Now languages like Java and C have very clear preferences that most people adopt. 294 00:15:01.995 --> 00:15:05.725 But Python doesn't really have a firm convention to follow in this respect, so 295 00:15:05.725 --> 00:15:07.265 there's no sort of one way of doing it. 296 00:15:07.265 --> 00:15:11.065 There's no sort of common scenario that says you should write your code this way. 297 00:15:11.065 --> 00:15:15.145 So again whichever way you prefer to do it, whether you want to write a creative 298 00:15:15.145 --> 00:15:18.335 variable with words like separated by underscores or 299 00:15:18.335 --> 00:15:22.560 you want to use camel case which is the word first word being lower case then each 300 00:15:22.560 --> 00:15:27.840 word after that capitalized with a letter to help separating the actual words. 301 00:15:27.840 --> 00:15:30.876 The thing here would be to actually be consistent again and you've come from 302 00:15:30.876 --> 00:15:34.320 languages like Java and C it's probably a good idea just to follow the same 303 00:15:34.320 --> 00:15:38.180 conventions just to make sure that if you are going back to those languages, 304 00:15:38.180 --> 00:15:40.560 you know that should bring, at least, your habits back to them. 305 00:15:40.560 --> 00:15:44.095 But again, completely personal preference, whichever way you prefer it, 306 00:15:44.095 --> 00:15:45.210 it's up to you. 307 00:15:45.210 --> 00:15:48.040 Again, I can't stress enough that make sure you're actually consistent. 308 00:15:48.040 --> 00:15:50.613 So we're gonna be consistent within individual video topics, 309 00:15:50.613 --> 00:15:53.990 but we're gonna actually use both styles just so you get used to them both. 310 00:15:53.990 --> 00:15:57.190 Just another reminder also before I end this video is that Python 311 00:15:57.190 --> 00:15:58.900 variables are case sensitive. 312 00:15:58.900 --> 00:16:04.490 So in other words, nastyFoodItem 313 00:16:04.490 --> 00:16:09.500 is actually different to nasty_food_item on the first time with the lower case n. 314 00:16:09.500 --> 00:16:10.770 Two entirely different variables. 315 00:16:10.770 --> 00:16:13.860 All right, so I'm just gonna delete that code there and 316 00:16:13.860 --> 00:16:15.820 we're going to end the video here now. 317 00:16:15.820 --> 00:16:20.330 In the next video we're actually going to look at Augmented Assignment, and 318 00:16:20.330 --> 00:16:22.840 I know that sounds like a mouthful, but you'll be finding out what that is and 319 00:16:22.840 --> 00:16:24.300 how to use it in the next video.