WEBVTT 1 00:00:00.570 --> 00:00:02.480 Hi guys, and welcome back. 2 00:00:02.480 --> 00:00:04.460 In this video, we're going to learn about 3 00:00:04.460 --> 00:00:07.720 Break and Continue - two more Python keywords 4 00:00:07.720 --> 00:00:10.100 that do some very interesting things when you put them 5 00:00:10.100 --> 00:00:12.340 inside a loop. 6 00:00:12.340 --> 00:00:15.380 Imagine you've got a list of car productions. 7 00:00:15.380 --> 00:00:18.510 So, most of them are ok, but every now and then 8 00:00:18.510 --> 00:00:20.540 you get a faulty one. 9 00:00:20.540 --> 00:00:24.590 So, what we want to do is, we want to show the user 10 00:00:24.590 --> 00:00:29.590 that we are ok with most cars, but when we encounter 11 00:00:29.710 --> 00:00:34.130 a faulty one, we want to stop the production line entirely 12 00:00:34.130 --> 00:00:36.680 so that we have time to investigate. 13 00:00:36.680 --> 00:00:38.470 What we will do is we will, first of all, 14 00:00:38.470 --> 00:00:40.373 iterate over this list. 15 00:00:42.380 --> 00:00:47.380 Now we can print something like "This car is {status}." 16 00:00:48.640 --> 00:00:52.087 So this will just print each status - 17 00:00:52.087 --> 00:00:53.803 "This car is ok, this car is ok, this car is okay, 18 00:00:54.934 --> 00:00:57.430 This car is faulty, this car is ok, this car is ok." 19 00:00:57.430 --> 00:00:58.540 Now, we've got a problem, 20 00:00:58.540 --> 00:01:01.210 which is that, in our production line, or in, 21 00:01:01.210 --> 00:01:03.440 in our imaginary production line, 22 00:01:03.440 --> 00:01:05.730 when we encounter a faulty car, 23 00:01:05.730 --> 00:01:07.810 we cannot continue processing cars. 24 00:01:07.810 --> 00:01:11.380 So we have to stop the loop when that happens. 25 00:01:11.380 --> 00:01:15.960 Well, that's where the break keyword comes into play. 26 00:01:15.960 --> 00:01:18.463 Inside our for loop, we will have an if statement, 27 00:01:20.380 --> 00:01:24.010 and that will check if the status is faulty. 28 00:01:24.010 --> 00:01:26.820 Now, do note something very important, 29 00:01:26.820 --> 00:01:31.510 you've got a lot of spaces now because you need four spaces 30 00:01:31.510 --> 00:01:34.770 to tell Python that this line here is inside the for loop, 31 00:01:34.770 --> 00:01:37.540 and then you need an extra four spaces to tell it that it's 32 00:01:37.540 --> 00:01:39.270 inside the if statement. 33 00:01:39.270 --> 00:01:42.790 So you've got eight spaces in total in this line here 34 00:01:42.790 --> 00:01:47.350 because it's both inside the for loop and the if statement. 35 00:01:47.350 --> 00:01:50.810 So if we encounter a car whose status is faulty, 36 00:01:50.810 --> 00:01:53.627 we're going to print "Stopping the production line!" 37 00:01:56.000 --> 00:01:58.800 And then, we're going to break. 38 00:01:58.800 --> 00:02:01.970 What this means is "stop the loop", 39 00:02:01.970 --> 00:02:06.640 so because we are inside a loop here, break will stop it. 40 00:02:06.640 --> 00:02:08.480 No more iterations of the loop. 41 00:02:08.480 --> 00:02:12.520 We will simply continue from after the loop onwards. 42 00:02:12.520 --> 00:02:14.090 Let's run this. 43 00:02:14.090 --> 00:02:16.670 And you'll see that we get "This car is ok, This car is ok, 44 00:02:16.670 --> 00:02:19.680 This car is ok", and then "Stopping the production line." 45 00:02:19.680 --> 00:02:21.790 So break is useful for when you want to 46 00:02:21.790 --> 00:02:23.533 terminate a loop completely. 47 00:02:24.990 --> 00:02:28.840 However, let's say that instead of completely stopping 48 00:02:28.840 --> 00:02:31.710 the production line, we just want to skip this car. 49 00:02:31.710 --> 00:02:35.590 For example, because every time that a car is ok, 50 00:02:35.590 --> 00:02:39.450 we ship it. "Shipping new car to customer!" 51 00:02:39.450 --> 00:02:43.580 Every time a car is ok, we ship the new car to the customer. 52 00:02:43.580 --> 00:02:46.570 So, if we run this right now, you'll see that 53 00:02:46.570 --> 00:02:49.397 we get "This car is ok. Shipping new car to customer!" 54 00:02:49.397 --> 00:02:51.830 "This car is ok. Shipping new car to customer!" 55 00:02:51.830 --> 00:02:53.900 And again, and finally we stop the production line 56 00:02:53.900 --> 00:02:55.580 because we found a faulty one. 57 00:02:55.580 --> 00:02:57.770 But again, what we want to do now is, 58 00:02:57.770 --> 00:02:59.180 if we find a faulty one, 59 00:02:59.180 --> 00:03:02.350 we just want to skip it and ship the next one. 60 00:03:02.350 --> 00:03:05.750 So, we can do that with a continue keyword. 61 00:03:05.750 --> 00:03:07.270 Instead of stopping the production line, 62 00:03:07.270 --> 00:03:12.027 we will type "Found faulty car, skipping...", 63 00:03:13.270 --> 00:03:16.490 and then, instead of break, we will do continue. 64 00:03:16.490 --> 00:03:19.600 And what this does is, it just jumps back 65 00:03:19.600 --> 00:03:22.850 to the start of the loop without printing anything else, 66 00:03:22.850 --> 00:03:25.500 or without running any lines of code inside this loop. 67 00:03:25.500 --> 00:03:28.110 So when you encounter a continue keyword, 68 00:03:28.110 --> 00:03:31.620 it just goes into the next one from the top. 69 00:03:31.620 --> 00:03:33.900 So by pressing run again, you'll see that, 70 00:03:33.900 --> 00:03:37.377 when we encounter the faulty car, we do not print 71 00:03:37.377 --> 00:03:40.510 "This car is ok. Shipping new car to customer!" 72 00:03:40.510 --> 00:03:45.510 In total, we have five shipments because we have five oks. 73 00:03:48.350 --> 00:03:50.370 All right, so those are the two keywords, 74 00:03:50.370 --> 00:03:52.770 and, again, when you first start learning about 75 00:03:52.770 --> 00:03:56.020 a new piece of syntax like break and continue, 76 00:03:56.020 --> 00:03:58.570 you may not immediately understand what it's for, 77 00:03:58.570 --> 00:03:59.930 why it's useful. 78 00:03:59.930 --> 00:04:02.010 But we're going to learn through examples 79 00:04:02.010 --> 00:04:03.400 as we progress through the course. 80 00:04:03.400 --> 00:04:05.770 So don't worry about it, thanks for joining me, 81 00:04:05.770 --> 00:04:07.520 line:15% and I'll see you in the next video.