1 00:00:00,990 --> 00:00:06,640 Hello will come again in this list and we are going to talk about jumping your statements. 2 00:00:06,710 --> 00:00:12,320 That is essentially breaking out of a for two or essentially any kind of loop. 3 00:00:12,510 --> 00:00:20,470 So imagine if you have a form that runs between 0 to 100 and then the moment reaches 75 you don't want 4 00:00:20,490 --> 00:00:26,910 to continue anymore or if it reaches a certain number you don't want something to happen in that particular 5 00:00:26,970 --> 00:00:27,720 number. 6 00:00:27,720 --> 00:00:30,220 So how can we break out of a 4:2. 7 00:00:30,390 --> 00:00:39,570 So this isn't about that the school I head into ex-cult I'm going to start a new project called John 8 00:00:39,750 --> 00:00:43,710 statesman's mind jumps statement. 9 00:00:43,780 --> 00:00:45,190 Here is what I'm about to do. 10 00:00:45,270 --> 00:00:52,410 I'm going to say I have a for loop that goes between our certain numbers. 11 00:00:52,410 --> 00:00:56,830 So here is my fault for ins. 12 00:00:56,830 --> 00:01:06,480 Co. I'm going to counter this time count or below 10 counter-force increments. 13 00:01:06,810 --> 00:01:11,280 So for 10 times zero to 10 I'm actually going to do this. 14 00:01:11,280 --> 00:01:18,200 I'm going to say and Aslaug counter is a percentage I counter. 15 00:01:18,420 --> 00:01:23,960 So I'm going to print out there as you can imagine when I run this is going to printed 10 times. 16 00:01:23,970 --> 00:01:30,280 Now here's something I'm going to do I'm going to say if the counter is six. 17 00:01:30,280 --> 00:01:34,270 So if you reached Number Six Here's what they want to happen. 18 00:01:34,270 --> 00:01:35,750 I want to. 19 00:01:36,070 --> 00:01:38,820 So let's see what this does for us. 20 00:01:38,910 --> 00:01:42,040 So I'm going to run the code and the code runs. 21 00:01:42,080 --> 00:01:44,800 You can see the compiler down here. 22 00:01:45,090 --> 00:01:47,870 So it says counter is 0 1 2 3 4 5. 23 00:01:47,880 --> 00:01:55,130 But the moment it is 6 it breaks out of the for loop and essentially comes here with this line. 24 00:01:55,140 --> 00:02:02,110 So let me put something here and Aslaug and asked this. 25 00:02:03,030 --> 00:02:05,400 And of the form. 26 00:02:05,890 --> 00:02:06,450 Right. 27 00:02:06,590 --> 00:02:12,660 So if you rerun this now you will see the error counts between 0 to 5 and then says well this is the 28 00:02:12,660 --> 00:02:13,850 end of the formula. 29 00:02:14,100 --> 00:02:21,900 Now that's a lot of the thing let's say in my FOR loop if my counter is going to reach has a six I want 30 00:02:21,900 --> 00:02:23,460 to continue. 31 00:02:24,030 --> 00:02:28,870 Let's see what continuo does continue is essentially going to skip this iteration. 32 00:02:28,890 --> 00:02:31,050 It says Number Six. 33 00:02:31,080 --> 00:02:32,440 Skip this part. 34 00:02:32,690 --> 00:02:41,630 Let's see what you get and as you can imagine we now have 0 1 2 3 4 5 but we don't have 6 because when 35 00:02:41,650 --> 00:02:46,480 time 36 we saying continue to skip this quarter the next one. 36 00:02:46,540 --> 00:02:53,010 So he's going to 71 81 on the 91 and then the whole 42 is essentially finished. 37 00:02:53,040 --> 00:02:55,970 Now could this be useful for us. 38 00:02:55,970 --> 00:02:59,650 Let me take you back to one of the lessons we had earlier. 39 00:02:59,840 --> 00:03:05,550 Earlier on we had this array array of the cities that we copy all of that. 40 00:03:05,720 --> 00:03:06,650 You don't need to do this. 41 00:03:06,650 --> 00:03:09,340 This is just for demonstration purposes. 42 00:03:09,390 --> 00:03:15,320 When I actually comment out both of these guys and I'm going to put this in here so we have an array 43 00:03:15,380 --> 00:03:18,130 with a large amount of what you're texting. 44 00:03:18,140 --> 00:03:20,160 There's so many different items in it. 45 00:03:20,340 --> 00:03:26,640 I want to find out the very very first time are the term Shanghai shows up. 46 00:03:26,720 --> 00:03:41,640 So I'm going to go ahead and say for innocence Thring cities in cities array if City is equal to a string. 47 00:03:41,750 --> 00:03:44,270 And this thing I'm looking for was Shanghai. 48 00:03:44,420 --> 00:03:47,990 So if you found Shanghai then we wanted to do something. 49 00:03:47,990 --> 00:03:54,120 For instance we want to say and that's the long of the foundation. 50 00:03:54,380 --> 00:03:55,610 All right simple as that. 51 00:03:55,610 --> 00:04:02,360 So we go to all the cities in this area and if we find Shanghai right we found Shanghai and there's 52 00:04:02,360 --> 00:04:08,750 really nothing wrong about that except for something let me print another thing at the bottom of the 53 00:04:08,750 --> 00:04:09,860 for loop in here. 54 00:04:09,860 --> 00:04:20,080 I'm going to say and Aslaug this city is and I'm going to say the object that you find in the city. 55 00:04:20,180 --> 00:04:22,430 So let's run this. 56 00:04:23,360 --> 00:04:31,490 So as you can see it says in our cities Vancouver Vancouver blah blah blah blah because we found Shanghai 57 00:04:31,940 --> 00:04:32,600 again. 58 00:04:32,620 --> 00:04:33,860 This city is Shanghai. 59 00:04:33,850 --> 00:04:40,650 The city is all part of the city they pay wash over who work cheap scoring going through all of them. 60 00:04:40,750 --> 00:04:45,130 What if the moment I found Shanghai I didn't want to continue anymore. 61 00:04:45,140 --> 00:04:47,940 But if I didn't want to go to everybody else. 62 00:04:48,020 --> 00:04:55,470 So in that case I'm going to say if the city was trying calling say we found Shanghai and then say break 63 00:04:55,850 --> 00:04:57,850 I don't want to do this anymore. 64 00:04:58,100 --> 00:05:05,190 So that's one of the it says it happens fewer times it says Vancouver Vancouver. 65 00:05:05,190 --> 00:05:06,780 All of these different places. 66 00:05:06,930 --> 00:05:11,660 And it says we found Shanghai and then it breaks entirely out of this flow. 67 00:05:11,820 --> 00:05:18,030 So we don't have that anymore and that's quite useful because you know it cuts down on the compilation 68 00:05:18,030 --> 00:05:23,670 time and the computation time and also make sure that your code has a better structure but that logic 69 00:05:23,940 --> 00:05:28,080 if you're looking for something in the moment you found that you don't care whether anything else. 70 00:05:28,230 --> 00:05:28,680 That's it. 71 00:05:28,680 --> 00:05:33,090 We don't have to do any of the other calculations we have to just jump outside of it. 72 00:05:33,360 --> 00:05:39,640 Now in saying that we could also use break or continue in this that for loops as well. 73 00:05:39,660 --> 00:05:41,260 So let me show you how. 74 00:05:41,850 --> 00:05:47,340 Let's say I have a group of four dopes that are nested inside each other. 75 00:05:47,350 --> 00:05:51,710 So for instance H is zero. 76 00:05:51,720 --> 00:06:03,490 That is for a horizontal H is below 5 and H increments itself and then within that four ends work the 77 00:06:03,500 --> 00:06:10,170 call equals zero call below 5 V plus plus. 78 00:06:10,350 --> 00:06:12,030 And then we want to do something in here. 79 00:06:12,030 --> 00:06:17,730 So we want to have five essentially five columns of five rows. 80 00:06:17,730 --> 00:06:20,350 So it's going to be a matrix of 5.5. 81 00:06:20,370 --> 00:06:22,720 We'll be making a lot of these in the future. 82 00:06:22,890 --> 00:06:24,540 So for now I want to do this. 83 00:06:24,570 --> 00:06:37,830 I want to say if we reached the tree and the H are four if we reach this particular location of break 84 00:06:37,830 --> 00:06:39,970 out entirely. 85 00:06:40,370 --> 00:06:40,930 Right. 86 00:06:40,980 --> 00:06:45,200 So if we reached this place we want to break out of here entirely. 87 00:06:45,210 --> 00:06:52,060 So if V was 3 and H was for we we want to finish this for low income to here which says. 88 00:06:52,220 --> 00:06:57,240 And that's like all all is completely finished. 89 00:06:57,240 --> 00:06:58,330 So what should we do. 90 00:06:59,870 --> 00:07:03,670 So here's what I'm I'm going to say in every nation. 91 00:07:03,710 --> 00:07:05,110 Let's print something. 92 00:07:05,300 --> 00:07:19,280 And as long as we are at and whether we have to we are at our age which is that things we that is another 93 00:07:19,310 --> 00:07:19,970 thing. 94 00:07:20,210 --> 00:07:24,800 So what are those ideas the age we we thought that is where we are. 95 00:07:24,800 --> 00:07:25,880 So then we print this. 96 00:07:25,940 --> 00:07:27,550 We built this application. 97 00:07:27,770 --> 00:07:35,150 It says we are at age 0 0 0 1 and then we go all the way to before and then just keeps going all the 98 00:07:35,150 --> 00:07:39,840 way to age 14 and before and then it says the loop is completely finished. 99 00:07:39,890 --> 00:07:52,220 But then in here I'm going to say if what are we looking for if it is tree and the edge is for if I'm 100 00:07:52,220 --> 00:07:58,320 not wrong the stage is for in that case what they want to do is if they want to break. 101 00:07:58,550 --> 00:07:59,630 So let's see what 102 00:08:04,860 --> 00:08:05,660 so what. 103 00:08:05,670 --> 00:08:09,660 The gun is here is Vegard H for v3. 104 00:08:09,660 --> 00:08:12,610 What we wanted and the loop has completely finished. 105 00:08:12,840 --> 00:08:14,610 This may seem it is working. 106 00:08:14,640 --> 00:08:15,750 It actually is not. 107 00:08:15,750 --> 00:08:17,030 Let me show you why. 108 00:08:17,190 --> 00:08:23,160 Let's say I wanted to find age to age 2 and Vietri. 109 00:08:23,250 --> 00:08:24,840 So I'm going to change that too. 110 00:08:25,130 --> 00:08:28,570 So then I'd add one more in a slug and a slide. 111 00:08:29,020 --> 00:08:34,180 When I say internal loop is finished. 112 00:08:34,230 --> 00:08:39,610 So as you can imagine every time that this flop is finished we run this. 113 00:08:39,630 --> 00:08:45,820 Then we go back to the outer for a loop and increment that and then we redo this whole thing. 114 00:08:46,020 --> 00:08:48,160 So let's see what we get. 115 00:08:49,530 --> 00:09:02,900 It says in here where we find it essentially it says our age zero before etch to reiterate this is very 116 00:09:02,910 --> 00:09:06,140 wanted things to be Vietri age too. 117 00:09:06,390 --> 00:09:08,360 And then the internal loop finishes. 118 00:09:08,490 --> 00:09:11,210 But then it goes to H-3 and zero. 119 00:09:11,220 --> 00:09:13,820 So it really starts to outperform. 120 00:09:13,980 --> 00:09:15,320 But that's not what we wanted. 121 00:09:15,320 --> 00:09:18,480 We wanted to completely break out and come in here. 122 00:09:18,690 --> 00:09:19,850 So what could we do. 123 00:09:19,910 --> 00:09:28,130 Here is something that's a very common practice and going introduce a new valuable called Boullion are 124 00:09:28,260 --> 00:09:31,230 let's say completed. 125 00:09:31,380 --> 00:09:34,250 And they completed by the forth is false. 126 00:09:34,500 --> 00:09:42,380 So if we reach this situation if we reach where everything is the way that we want them to be here's 127 00:09:42,390 --> 00:09:43,240 what we're going to do. 128 00:09:43,380 --> 00:09:50,490 We're gonna say in that case a sort of a break come play Ted becomes true. 129 00:09:50,490 --> 00:09:55,140 So now that complete that is true we're going to break out of this for a lot. 130 00:09:55,170 --> 00:09:57,720 So let me get myself another line here. 131 00:09:57,780 --> 00:10:01,920 I'm going to say brag or rather direct with proper spelling. 132 00:10:01,920 --> 00:10:03,120 So break. 133 00:10:03,450 --> 00:10:12,060 And then at the beginning of each of the outer four loops I'm going to go ahead and say if completed 134 00:10:12,450 --> 00:10:16,150 is completed is true then branch. 135 00:10:16,500 --> 00:10:22,830 So the moment we begin this complete is not true so it is not going to break so it's going to go to 136 00:10:22,830 --> 00:10:24,860 this line is going to calculate that. 137 00:10:25,230 --> 00:10:32,200 But then the moment the complete it becomes true from there on every time we go to a new citation that 138 00:10:32,420 --> 00:10:34,920 iteration is not going to happen. 139 00:10:35,010 --> 00:10:44,720 So let's say this in practice then run this and the moment they say H2 and H3 internal loop and. 140 00:10:44,770 --> 00:10:48,440 And also does the for loop is completely finished. 141 00:10:48,450 --> 00:10:54,810 So as you can see it's a combination of some of the tools that are there such as a break or continue. 142 00:10:54,990 --> 00:11:01,350 Some of the you know the logic that we could use ourselves we say well the moment I reach a certain 143 00:11:01,350 --> 00:11:07,530 this and that's when I want things to happen and based on that we decide about you know what's going 144 00:11:07,530 --> 00:11:09,000 to happen next. 145 00:11:09,000 --> 00:11:19,440 So two quick recap in here are use things such as continue or break to either keep and I ration of loop 146 00:11:19,800 --> 00:11:25,030 or to skip the entire four loop based on a certain condition. 147 00:11:25,050 --> 00:11:28,380 So you continue to say skip this one go to the next one. 148 00:11:28,480 --> 00:11:34,920 You use the break to completely break out of a of a loop or isolation. 149 00:11:34,950 --> 00:11:36,950 So I think that's about that. 150 00:11:36,990 --> 00:11:39,480 Let's keep it up to here and I see you in the next lesson.