1 00:00:06,596 --> 00:00:09,090 - In the previous section, we saw how to write loops 2 00:00:09,090 --> 00:00:11,370 and now we're going to see how to use the break keyword 3 00:00:11,370 --> 00:00:12,360 and continue keyword 4 00:00:12,360 --> 00:00:16,500 to control execution through those loops. 5 00:00:16,500 --> 00:00:18,417 So if you've done other languages 6 00:00:18,417 --> 00:00:21,450 and you've come across break and continue in those languages 7 00:00:21,450 --> 00:00:24,360 then it's nearly exactly the same in Rust. 8 00:00:24,360 --> 00:00:26,850 The only difference is when you have a nested loop, 9 00:00:26,850 --> 00:00:28,860 how do you break out of a nested loop 10 00:00:28,860 --> 00:00:31,680 or how do you continue with a nested loop in Rust? 11 00:00:31,680 --> 00:00:33,960 So we'll have a look at that as well in this section. 12 00:00:33,960 --> 00:00:37,320 So first of all, this is how you can break out of a loop. 13 00:00:37,320 --> 00:00:38,850 Use the break keyword. 14 00:00:38,850 --> 00:00:40,860 So I've got some hypothetical loop 15 00:00:40,860 --> 00:00:43,290 and each time we run the loop 16 00:00:43,290 --> 00:00:44,820 it's gonna be doing some processing 17 00:00:44,820 --> 00:00:45,930 on the current element. 18 00:00:45,930 --> 00:00:47,820 Okay, so that's fine. 19 00:00:47,820 --> 00:00:51,840 But if we reach some condition, then break. 20 00:00:51,840 --> 00:00:53,640 As soon as some condition is met 21 00:00:53,640 --> 00:00:56,220 then the break keyword basically terminates the loop 22 00:00:56,220 --> 00:00:57,123 and we're done. 23 00:00:57,990 --> 00:01:00,600 So reasons for using the break keyword 24 00:01:00,600 --> 00:01:02,280 is where you wanna loop over a collection, 25 00:01:02,280 --> 00:01:05,970 maybe lines of text from a file, 26 00:01:05,970 --> 00:01:08,010 you're looking for a particular item, 27 00:01:08,010 --> 00:01:10,890 and once you found that item then stop looping 28 00:01:10,890 --> 00:01:13,800 because you found what you were looking for, okay? 29 00:01:13,800 --> 00:01:18,360 So if you found the item you were looking for, then break. 30 00:01:18,360 --> 00:01:22,023 No need to loop anymore. We found it, let's stop. 31 00:01:22,950 --> 00:01:26,010 Okay, right. What about the continue keyword? 32 00:01:26,010 --> 00:01:29,160 Same kind of syntax, except with the continue keyword, 33 00:01:29,160 --> 00:01:31,290 it doesn't abandon the whole loop completely 34 00:01:31,290 --> 00:01:33,810 it just abandons the current iteration 35 00:01:33,810 --> 00:01:35,460 and then it goes back up to the top again, 36 00:01:35,460 --> 00:01:37,440 ready for the next iteration, okay? 37 00:01:37,440 --> 00:01:39,540 So typical usage for that 38 00:01:39,540 --> 00:01:42,960 would be where you're looping over some items. 39 00:01:42,960 --> 00:01:46,050 So maybe you're reading lines of text from a file. 40 00:01:46,050 --> 00:01:47,730 And so each time around the loop 41 00:01:47,730 --> 00:01:49,860 you've got some processing that you're gonna do 42 00:01:49,860 --> 00:01:51,123 on the current element. 43 00:01:52,260 --> 00:01:55,770 But let's say that the next element that you read in 44 00:01:55,770 --> 00:01:58,290 from the file is invalid, okay? 45 00:01:58,290 --> 00:02:01,140 So in the if condition here, 46 00:02:01,140 --> 00:02:02,520 you're saying something like, 47 00:02:02,520 --> 00:02:05,580 if the current element is invalid 48 00:02:05,580 --> 00:02:08,580 then continue the loop back at the top, 49 00:02:08,580 --> 00:02:10,020 ready for the next iteration. 50 00:02:10,020 --> 00:02:13,290 So basically it abandons, it doesn't perform the body 51 00:02:13,290 --> 00:02:16,560 it just continues up the top, ready for the next element 52 00:02:16,560 --> 00:02:17,460 in the collection. 53 00:02:17,460 --> 00:02:21,360 So in a way, it's a pity that the continue element 54 00:02:21,360 --> 00:02:24,660 wasn't called skip, okay? 55 00:02:24,660 --> 00:02:26,340 Because that's kind of what it's doing. 56 00:02:26,340 --> 00:02:29,490 But of course, because of C, basically 57 00:02:29,490 --> 00:02:31,020 if you go back to 1972, 58 00:02:31,020 --> 00:02:33,930 the C programming language introduced the continue keyword 59 00:02:33,930 --> 00:02:36,000 and we've been stuck with it ever since. 60 00:02:36,000 --> 00:02:39,990 So continue the loop body back up the top again, 61 00:02:39,990 --> 00:02:41,850 ready for the next element. 62 00:02:41,850 --> 00:02:43,230 So quite straightforward really 63 00:02:43,230 --> 00:02:46,050 and familiar from other languages. 64 00:02:46,050 --> 00:02:47,550 Okay, one thing that is different 65 00:02:47,550 --> 00:02:48,870 is if you have a nested loop. 66 00:02:48,870 --> 00:02:51,330 So if you have a loop within a loop, 67 00:02:51,330 --> 00:02:54,420 by default the break and continue would just take you 68 00:02:54,420 --> 00:02:56,760 out of the nested loop, okay? 69 00:02:56,760 --> 00:02:58,230 And then you're still kind of, 70 00:02:58,230 --> 00:03:00,300 you would then go back into the outer loop. 71 00:03:00,300 --> 00:03:03,060 So if you've got this kind of arrangement, 72 00:03:03,060 --> 00:03:06,240 if you've got an outer loop and an inner loop, 73 00:03:06,240 --> 00:03:08,190 if you say break or continue in here, 74 00:03:08,190 --> 00:03:12,750 it'll just break or continue the current, the nested loop. 75 00:03:12,750 --> 00:03:14,700 If you want to break or continue 76 00:03:14,700 --> 00:03:17,520 out of the outer loop like that, 77 00:03:17,520 --> 00:03:20,700 then you have to take special measure. 78 00:03:20,700 --> 00:03:24,000 So what you have to do is you have to attach a label 79 00:03:24,000 --> 00:03:25,200 to the outer loop. 80 00:03:25,200 --> 00:03:28,020 You give it a name, it doesn't matter what you call it 81 00:03:28,020 --> 00:03:29,703 but you use this kind of syntax. 82 00:03:31,830 --> 00:03:35,250 Apostrophe and then some kind of name and a colon. 83 00:03:35,250 --> 00:03:39,690 Okay, so I could basically say 'outer: here. 84 00:03:39,690 --> 00:03:42,483 I label the outer loop a scope basically. 85 00:03:43,530 --> 00:03:46,230 And then when you say break or continue 86 00:03:46,230 --> 00:03:49,830 if you want to break out of the outer loop basically, 87 00:03:49,830 --> 00:03:53,070 like here, or if you wanna continue the outer loop, 88 00:03:53,070 --> 00:03:55,803 like here, then you have to use that label name. 89 00:03:57,240 --> 00:04:00,930 So if you wanted to break out of the outer loop, 90 00:04:00,930 --> 00:04:04,500 you'd say break and then apostrophe, 91 00:04:04,500 --> 00:04:06,870 and then whatever name you called it up here, okay? 92 00:04:06,870 --> 00:04:08,550 So whatever, it doesn't matter what you call it 93 00:04:08,550 --> 00:04:09,867 but it would be the same name here. 94 00:04:09,867 --> 00:04:11,700 And I would then basically break out 95 00:04:11,700 --> 00:04:15,123 of that outer loop like so. 96 00:04:16,290 --> 00:04:20,460 Or alternatively, if you want to continue the outer loop 97 00:04:20,460 --> 00:04:24,360 then you say continue with the label that you specified. 98 00:04:24,360 --> 00:04:28,590 And that would then continue, not continue the inner loop, 99 00:04:28,590 --> 00:04:31,230 it would instead continue the outer loop. 100 00:04:31,230 --> 00:04:35,010 It would basically iterate around the outer loop like so. 101 00:04:35,010 --> 00:04:38,700 So it's not often where you need to use nested loops 102 00:04:38,700 --> 00:04:42,660 with kind of labeled loop names 103 00:04:42,660 --> 00:04:44,373 but you can do it if you want to. 104 00:04:45,660 --> 00:04:48,000 So let's see an example of all of this. 105 00:04:48,000 --> 00:04:50,910 Same project, lesson03_flow_control 106 00:04:50,910 --> 00:04:52,890 and it's the final function we're gonna look at 107 00:04:52,890 --> 00:04:54,780 in this lesson, demo_break_continue. 108 00:04:54,780 --> 00:04:59,010 Let's have a look. So here's my code in VS Code. 109 00:04:59,010 --> 00:05:01,050 And in the main function, 110 00:05:01,050 --> 00:05:03,000 I'm going to uncomment the last function 111 00:05:03,000 --> 00:05:04,533 called demo_break_continue. 112 00:05:05,477 --> 00:05:07,927 And let's have a look at that function down here. 113 00:05:08,820 --> 00:05:11,610 Okay, so I've got several different bits of the example 114 00:05:11,610 --> 00:05:14,640 and as usual, I'll run the example first 115 00:05:14,640 --> 00:05:17,883 so we can actually have some output to look at. 116 00:05:18,900 --> 00:05:19,953 cargo run. 117 00:05:23,130 --> 00:05:26,460 Okay then, so let's have a look. 118 00:05:26,460 --> 00:05:30,270 So this is my demo of using break and continue. 119 00:05:30,270 --> 00:05:32,430 Here's my array. 120 00:05:32,430 --> 00:05:37,430 Oh, I'm looking to see, did the student get 100 in any exam? 121 00:05:39,030 --> 00:05:40,773 So loop through the elements. 122 00:05:41,670 --> 00:05:45,060 If the element was 100, then say we found 100. 123 00:05:45,060 --> 00:05:47,250 So break out of the loop completely. 124 00:05:47,250 --> 00:05:49,893 And the break statement takes me down here. 125 00:05:51,630 --> 00:05:53,933 Of course, if the value wasn't 100 126 00:05:53,933 --> 00:05:56,280 then we just print it, okay? 127 00:05:56,280 --> 00:06:00,210 So as we pass in 99 and 45 and 85, 128 00:06:00,210 --> 00:06:02,400 those would fail the if statement 129 00:06:02,400 --> 00:06:07,110 and they would just print the value 99, 45, 85. 130 00:06:07,110 --> 00:06:09,990 And then when we get to 100, hooray, 131 00:06:09,990 --> 00:06:11,970 that's what we're looking for. 132 00:06:11,970 --> 00:06:15,273 Break, stop looping, and we're done here. 133 00:06:17,070 --> 00:06:17,903 There we go. 134 00:06:17,903 --> 00:06:21,540 I never, ever got 100 in any exam ever in my life. 135 00:06:21,540 --> 00:06:24,150 I had 99 when I was about 14. 136 00:06:24,150 --> 00:06:25,990 We had an exam on technical drawing 137 00:06:26,940 --> 00:06:30,450 but my teacher took one mark off because I'd written my name 138 00:06:30,450 --> 00:06:32,730 in a slightly untidy handwriting. 139 00:06:32,730 --> 00:06:36,813 So my moment in the sun, snatched from my grasp. 140 00:06:38,280 --> 00:06:41,820 But I'm completely over it. 44 years later. 141 00:06:41,820 --> 00:06:43,530 What about this one then? 142 00:06:43,530 --> 00:06:47,610 If the exam mark was less than 50, that would be a failure, 143 00:06:47,610 --> 00:06:52,610 then basically skip that value and go back to the next one. 144 00:06:52,770 --> 00:06:55,230 So the first few exam marks, 145 00:06:55,230 --> 00:07:00,230 well, 99, I didn't skip 99, I printed 99, so there's 99. 146 00:07:04,020 --> 00:07:07,140 And then the next element was 45. 147 00:07:07,140 --> 00:07:09,750 So 45, basically skip that element. 148 00:07:09,750 --> 00:07:13,503 I found a value less than 50. So basically skip it. 149 00:07:14,430 --> 00:07:18,630 So the continue keyword basically stopped 150 00:07:18,630 --> 00:07:21,720 the current iteration, didn't go any further. 151 00:07:21,720 --> 00:07:24,840 It basically wound back up ready for the next element 152 00:07:24,840 --> 00:07:28,008 and the next few elements were okay. 153 00:07:28,008 --> 00:07:32,610 85, 100, and 82. 154 00:07:32,610 --> 00:07:34,230 Very good. 155 00:07:34,230 --> 00:07:37,620 And then here I've got an example of a nested loop. 156 00:07:37,620 --> 00:07:41,010 So it's a nested loop if you like, 157 00:07:41,010 --> 00:07:43,290 but it could have been a nested while loop 158 00:07:43,290 --> 00:07:44,730 or it could have been a nested for loop. 159 00:07:44,730 --> 00:07:48,240 And you can use this 'outer: mechanism for any kind of loop. 160 00:07:48,240 --> 00:07:50,130 I've just used an infinite loop here. 161 00:07:50,130 --> 00:07:52,533 So I have an infinite loop potentially. 162 00:07:53,400 --> 00:07:56,010 Entered the outer loop, it says here. 163 00:07:56,010 --> 00:07:58,090 So I have entered the outer loop 164 00:07:59,340 --> 00:08:02,913 and then here I have now entered the inner loop. 165 00:08:03,870 --> 00:08:05,820 Okay, so I'm now inside the inner loop. 166 00:08:07,410 --> 00:08:11,303 I break to the scope of the outer loop, okay? 167 00:08:11,303 --> 00:08:13,710 So it's not gonna go any further. 168 00:08:13,710 --> 00:08:16,950 It's gonna basically break out of this loop here. 169 00:08:16,950 --> 00:08:19,620 So that loop, the outer loop, whatever I call it. 170 00:08:19,620 --> 00:08:20,940 I didn't have to call it 'outer:, 171 00:08:20,940 --> 00:08:23,437 I could've called it 'wibble: and 'wibble:. 172 00:08:26,160 --> 00:08:28,380 That would've also been okay. 173 00:08:28,380 --> 00:08:31,200 It doesn't really matter what you call it. 174 00:08:31,200 --> 00:08:35,460 So break to the loop whose scope is defined by that. 175 00:08:35,460 --> 00:08:37,080 It'll break out of this loop here. 176 00:08:37,080 --> 00:08:39,033 In other words, it'll break out there. 177 00:08:40,260 --> 00:08:44,853 And then we get the message, exited the outer loop. The end. 178 00:08:45,720 --> 00:08:47,400 Exited the out the loop. The end. 179 00:08:47,400 --> 00:08:49,590 So that is the end of this lesson. 180 00:08:49,590 --> 00:08:52,710 In this lesson, we've seen how to write if statements, 181 00:08:52,710 --> 00:08:55,710 we've seen the match keyword, we've looked at loops, 182 00:08:55,710 --> 00:08:57,900 and we've looked at how to use break and continue, okay? 183 00:08:57,900 --> 00:09:01,260 So we're starting to put together algorithms 184 00:09:01,260 --> 00:09:02,640 with a little bit of interest now. 185 00:09:02,640 --> 00:09:04,790 Let's see what comes up in the next lesson.