1 00:00:00,900 --> 00:00:01,530 Welcome back. 2 00:00:01,770 --> 00:00:05,130 And this video I would like to introduce to break and continue keywords. 3 00:00:05,370 --> 00:00:10,830 Those two keywords are super useful when it comes to for loops or loops in general because they allow 4 00:00:10,830 --> 00:00:15,730 us to get out of the current iteration or get out of the loop entirely. 5 00:00:15,750 --> 00:00:17,340 If a certain condition is met. 6 00:00:17,790 --> 00:00:19,580 So here I have this full loop. 7 00:00:19,590 --> 00:00:26,520 Let's just use a print statement here where I'm just going to print I so this will print all the way 8 00:00:26,700 --> 00:00:27,600 up to 19. 9 00:00:27,600 --> 00:00:32,009 So from one to 19, let's look at it and we could see one to 19. 10 00:00:32,610 --> 00:00:33,760 So that's cool and all. 11 00:00:33,780 --> 00:00:36,960 So now let's look at what we can do with a break keyboard. 12 00:00:37,170 --> 00:00:44,100 So what I'm going to say is let's divide it by two and then check if that is going to be five. 13 00:00:44,220 --> 00:00:50,130 So basically, it's 10 that will be the case and that case of breakout. 14 00:00:50,610 --> 00:00:56,980 So just break, which means that finished his follow up or don't iterate through the follow up anymore. 15 00:00:57,480 --> 00:01:00,780 Basically continuing the code down here. 16 00:01:00,810 --> 00:01:04,050 So print done with the loop. 17 00:01:04,860 --> 00:01:06,400 OK, let's run it again. 18 00:01:06,420 --> 00:01:09,420 You could see it's running all the way to 10 and then it breaks out. 19 00:01:09,600 --> 00:01:13,920 So let's run it again and then you will see that it says done with a loop. 20 00:01:14,700 --> 00:01:15,660 OK, so that's cool. 21 00:01:16,080 --> 00:01:23,100 So basically, what happens here is once brake is executed, it gets out of this entire loop. 22 00:01:23,700 --> 00:01:27,680 So this follow up is done and it continues to code down here. 23 00:01:27,690 --> 00:01:28,920 So after the loop. 24 00:01:30,320 --> 00:01:37,490 Now, this is very useful if you get this data from an external source or if this is dependent on the 25 00:01:37,490 --> 00:01:43,760 user input, for example, or something like that in our case, it's rather useful, you could say, 26 00:01:43,760 --> 00:01:49,370 because well, why not just say until 11:00 here that would do the same thing and you wouldn't have 27 00:01:49,370 --> 00:01:50,210 to work with breaks. 28 00:01:50,420 --> 00:01:51,800 But this is not the point here. 29 00:01:52,070 --> 00:01:59,570 The point is really that you can use the break word in order to jump out of a for loop or a when expression 30 00:01:59,810 --> 00:02:02,330 when a certain condition is met. 31 00:02:02,840 --> 00:02:05,930 And that would usually be a condition that you have no control over. 32 00:02:06,170 --> 00:02:11,060 That is either random or it has to do with data that you get or something else. 33 00:02:11,720 --> 00:02:13,520 OK, so that's the break word. 34 00:02:13,760 --> 00:02:15,780 Now let's look at the continu keyword. 35 00:02:16,160 --> 00:02:20,630 And therefore, I will need to push this print statement further down. 36 00:02:21,110 --> 00:02:23,420 And now let's use continue. 37 00:02:23,750 --> 00:02:28,250 Like, so OK, let's run it again and see what's going to happen. 38 00:02:28,850 --> 00:02:30,950 So you see one two nine is there. 39 00:02:31,070 --> 00:02:32,270 There's No 10, there is No. 40 00:02:32,270 --> 00:02:35,540 11, and then it goes from 12 to 19 and it's done with a loop. 41 00:02:35,960 --> 00:02:37,130 So what happened here? 42 00:02:37,430 --> 00:02:42,100 The thing is, 10 divided by two is going to be five, right? 43 00:02:42,350 --> 00:02:43,910 So 10 divided by two is five. 44 00:02:44,120 --> 00:02:45,800 So this if statement is true. 45 00:02:46,100 --> 00:02:47,750 So it goes into this continue. 46 00:02:48,290 --> 00:02:54,230 What continue then does is it says, OK, stop with the current iteration and go to the next iteration. 47 00:02:54,590 --> 00:02:59,570 So skip the code that is inside of this for loop and then go to the next step. 48 00:02:59,990 --> 00:03:04,100 So it goes from one to two to three, and this has never met, right, this condition. 49 00:03:04,580 --> 00:03:11,480 But then at the point when it's 10, this condition is met and it just says, OK, skip the current 50 00:03:11,480 --> 00:03:13,640 iteration, go to the 11th step. 51 00:03:14,150 --> 00:03:17,690 OK, then it's checking 11 divided by two is equal equal five. 52 00:03:17,690 --> 00:03:22,820 And it says true because 11 divided by two is five point five. 53 00:03:23,060 --> 00:03:28,760 But because we're working with integers here, it doesn't save the point five, so it just saves five 54 00:03:28,760 --> 00:03:29,300 in there. 55 00:03:29,690 --> 00:03:31,940 So it is five equals five. 56 00:03:31,940 --> 00:03:33,470 So it's met as well. 57 00:03:33,470 --> 00:03:35,240 When I is equal. 58 00:03:35,240 --> 00:03:37,610 11 So then it just says Continue. 59 00:03:37,620 --> 00:03:39,380 So skip the current iteration. 60 00:03:39,590 --> 00:03:43,610 Don't execute this code anymore and go back to the next iteration. 61 00:03:44,270 --> 00:03:46,280 OK, so that's basically what's happening here. 62 00:03:46,310 --> 00:03:50,570 It's super interesting, but yeah, that's really here. 63 00:03:50,600 --> 00:03:54,050 So 10 divided by two is five. 64 00:03:55,320 --> 00:03:55,860 And then. 65 00:03:57,080 --> 00:04:09,980 Eleven divided by two is 5.5, which is five in terms of an int because integers cannot stored is zero 66 00:04:09,980 --> 00:04:11,480 point five thingI, right? 67 00:04:11,480 --> 00:04:14,030 So just cuts off whatever comes after the DOT. 68 00:04:14,240 --> 00:04:18,890 It cuts it off and that's why the 11 is not appearing here either. 69 00:04:19,370 --> 00:04:26,270 So it just continued through it, basically skipping this part where the value is actually being printed 70 00:04:26,270 --> 00:04:27,500 onto the console. 71 00:04:28,340 --> 00:04:28,700 All right. 72 00:04:28,700 --> 00:04:32,180 And that's pretty much it for the brick and continue keywords.