1 00:00:00,420 --> 00:00:01,200 ‫Welcome back. 2 00:00:01,230 --> 00:00:05,120 ‫In this video, we're going to have a look at break and continue. 3 00:00:05,130 --> 00:00:08,790 ‫And I told you that in this video would come a challenge. 4 00:00:08,790 --> 00:00:13,860 ‫But before we can go to the challenge, we need to have those two concepts. 5 00:00:13,860 --> 00:00:18,120 ‫We will need break and continue to do the following challenge. 6 00:00:18,120 --> 00:00:23,130 ‫So let's go ahead and create a little program which will do just that. 7 00:00:23,130 --> 00:00:32,670 ‫So let's create a for loop four and counter equals zero counter below ten counter plus plus. 8 00:00:32,670 --> 00:00:34,380 ‫So nothing too fancy yet. 9 00:00:34,560 --> 00:00:38,370 ‫And then let's simply write that on to the console. 10 00:00:39,060 --> 00:00:42,930 ‫So counter should be written on to the console. 11 00:00:42,930 --> 00:00:48,510 ‫So now what I want to have in this for loop is an if statement. 12 00:00:48,660 --> 00:00:58,230 ‫So if counter is equal to three, what I want to do is I want to type something onto the console or 13 00:00:58,230 --> 00:01:04,350 ‫write something onto the console right line at three we stop. 14 00:01:05,070 --> 00:01:07,620 ‫So that's the simple statement that it should do. 15 00:01:07,620 --> 00:01:13,290 ‫And now in order to stop, because usually this for loop would go from 0 to 9. 16 00:01:13,740 --> 00:01:16,980 ‫And now what I want to do is I want to stop it at three. 17 00:01:16,980 --> 00:01:20,430 ‫So what that will do is it will break here. 18 00:01:20,970 --> 00:01:25,770 ‫We have seen break before when we use switch statements or switch case. 19 00:01:25,770 --> 00:01:30,750 ‫And in this case we want to use break in a for loop what break will do. 20 00:01:30,750 --> 00:01:36,450 ‫It will run or get out of the current loop or the current context. 21 00:01:36,450 --> 00:01:38,670 ‫And in this case it's the for loop. 22 00:01:38,670 --> 00:01:42,630 ‫So it will simply finish the for loop at that point. 23 00:01:42,630 --> 00:01:47,850 ‫So whenever break is executed, we get out of the loop and then the code below. 24 00:01:47,850 --> 00:01:56,100 ‫In this case, line 24 would be executed and that line should simply be our read statement so that we 25 00:01:56,100 --> 00:01:58,290 ‫see what's happening on the console. 26 00:01:58,350 --> 00:01:58,740 ‫All right. 27 00:01:58,740 --> 00:02:03,480 ‫So let's run that and see what's happening. 28 00:02:04,170 --> 00:02:08,880 ‫And that we are we see zero one, two, three, at three we stop. 29 00:02:09,000 --> 00:02:10,530 ‫So that's exactly what happens. 30 00:02:10,530 --> 00:02:15,690 ‫So what the actual flow is, we check is zero below ten. 31 00:02:15,690 --> 00:02:16,470 ‫That's fine. 32 00:02:16,470 --> 00:02:22,710 ‫All right, let's write zero onto the console, then let's check if counter which is zero is equal to 33 00:02:22,710 --> 00:02:23,100 ‫three. 34 00:02:23,100 --> 00:02:24,420 ‫Well, that's not the case. 35 00:02:24,420 --> 00:02:28,320 ‫So this if statement is not true, let's go to the next iteration. 36 00:02:28,320 --> 00:02:30,780 ‫So now counter is at one. 37 00:02:30,780 --> 00:02:31,920 ‫We check again. 38 00:02:31,920 --> 00:02:33,180 ‫Okay, everything fine? 39 00:02:33,180 --> 00:02:35,370 ‫We just execute this line. 40 00:02:35,430 --> 00:02:40,320 ‫Next we check a two same thing and now counter is three. 41 00:02:40,320 --> 00:02:42,750 ‫So we have the iteration where counter is three. 42 00:02:42,750 --> 00:02:48,600 ‫So we write three onto the console and then we check is counter three and yes, it's true. 43 00:02:48,990 --> 00:02:51,600 ‫So this code will be executed. 44 00:02:51,600 --> 00:02:55,500 ‫So we write at three, we stop and next we break. 45 00:02:55,500 --> 00:03:02,460 ‫So now we get out of this loop and we don't execute the four, five, six, seven, eight, nine anymore. 46 00:03:03,120 --> 00:03:05,070 ‫So that's what you can do with break. 47 00:03:05,070 --> 00:03:08,100 ‫And I told you that we also will check out continue. 48 00:03:08,130 --> 00:03:10,980 ‫So let's have a quick look at what continue will do. 49 00:03:11,880 --> 00:03:19,710 ‫And I'm going to stop the execution here and let's go into continue. 50 00:03:20,280 --> 00:03:26,730 ‫What continue will do is it will simply skip the current entry. 51 00:03:26,730 --> 00:03:36,570 ‫So let me cut that under our if statement because what that will do now is it will only write afterwards. 52 00:03:36,570 --> 00:03:42,720 ‫So after this, if statement will make more sense once we run the code, we see here zero one, two, 53 00:03:42,720 --> 00:03:44,700 ‫three row there is no three. 54 00:03:44,730 --> 00:03:46,560 ‫It just says at three we stop. 55 00:03:46,560 --> 00:03:53,130 ‫Or what I could say is we skip the three, so let's enter that. 56 00:03:53,130 --> 00:03:57,480 ‫We skip number three 57 00:04:00,060 --> 00:04:04,530 ‫and that we are 012 and we skip number three. 58 00:04:04,920 --> 00:04:11,010 ‫So now what you could also check here is what if it's modulo two? 59 00:04:12,730 --> 00:04:18,580 ‫So we use modulo two is equal zero double equal, by the way. 60 00:04:20,800 --> 00:04:22,300 ‫And now let's run it again. 61 00:04:23,830 --> 00:04:29,000 ‫And we we get we skip number three, one, three and so forth. 62 00:04:29,020 --> 00:04:34,930 ‫So what we could actually write here is next one is an odd number. 63 00:04:35,770 --> 00:04:37,210 ‫So let's do that. 64 00:04:37,900 --> 00:04:41,080 ‫Now comes an odd number. 65 00:04:42,460 --> 00:04:43,780 ‫And let's run it again. 66 00:04:43,780 --> 00:04:45,970 ‫And we see now comes an odd number one. 67 00:04:45,970 --> 00:04:49,000 ‫Now comes an odd number three and so forth. 68 00:04:49,030 --> 00:04:55,770 ‫So each time that we don't have an odd number, which is the case here, we continue. 69 00:04:55,780 --> 00:05:01,870 ‫So we jump out of this current iteration of the for loop and jump to the next iteration of the for loop. 70 00:05:02,760 --> 00:05:03,390 ‫All right. 71 00:05:03,390 --> 00:05:10,530 ‫So it's simply stopping at this point for that iteration and it doesn't execute the code that follows. 72 00:05:10,530 --> 00:05:12,960 ‫So in our case, only that code follows. 73 00:05:12,960 --> 00:05:17,690 ‫So this console, that right line counter follows after the Continue statement. 74 00:05:17,700 --> 00:05:18,210 ‫Great. 75 00:05:18,210 --> 00:05:23,460 ‫So that's just a little information introduction into break and continue. 76 00:05:23,460 --> 00:05:26,640 ‫We're going to use that in further lectures. 77 00:05:26,640 --> 00:05:29,220 ‫So that's why I wanted to show it to you now. 78 00:05:29,340 --> 00:05:29,730 ‫Great. 79 00:05:29,730 --> 00:05:34,500 ‫So in the next video, you'll finally get the challenge that I promised and see you there.