1 00:00:00,000 --> 00:00:07,700 [No Audio] 2 00:00:07,701 --> 00:00:09,933 In this tutorial, we will be covering two 3 00:00:09,934 --> 00:00:12,700 important concepts in connection with loops, 4 00:00:13,033 --> 00:00:16,033 they are called break and continue statements. 5 00:00:16,500 --> 00:00:18,533 Again you will find similar and 6 00:00:18,566 --> 00:00:21,000 equivalent concepts in almost all the 7 00:00:21,001 --> 00:00:23,366 programming languages. We will cover the 8 00:00:23,367 --> 00:00:26,133 break statement first, so let's get started. 9 00:00:27,100 --> 00:00:29,433 First I will write some suitable comments. 10 00:00:29,445 --> 00:00:33,366 [No Audio] 11 00:00:33,367 --> 00:00:36,966 Okay, the break statement is inside the loops 12 00:00:36,967 --> 00:00:40,466 for stopping the loop execution. When a break 13 00:00:40,467 --> 00:00:43,466 statement is encountered inside a loop, the 14 00:00:43,467 --> 00:00:46,200 loop is immediately terminated, and the 15 00:00:46,201 --> 00:00:49,233 program control resumes at the next statement 16 00:00:49,234 --> 00:00:52,200 following the loop body. Let us explain this 17 00:00:52,201 --> 00:00:55,933 by having a simple example. Suppose, I want 18 00:00:55,934 --> 00:00:58,333 to write a program, which given a certain 19 00:00:58,334 --> 00:01:01,466 number typically greater than or equal to 100, 20 00:01:02,266 --> 00:01:05,500 will compute the smallest number lesser than 21 00:01:05,501 --> 00:01:08,200 the given number, which is divisible by 13. 22 00:01:08,700 --> 00:01:11,166 Of course, there are many ways in which this 23 00:01:11,167 --> 00:01:14,466 program may be coded, but I will code it with 24 00:01:14,467 --> 00:01:17,600 the help of simple loop. Again, this is just 25 00:01:17,601 --> 00:01:19,800 for the sake of illustration, and the solution 26 00:01:19,801 --> 00:01:22,700 may not be the optimal solution, but the 27 00:01:22,701 --> 00:01:25,333 intent here is to learn the usage of break. 28 00:01:25,666 --> 00:01:28,166 So I will first declare a variable, which 29 00:01:28,167 --> 00:01:30,300 contains the given number. So, I will write 30 00:01:30,566 --> 00:01:35,400 let mut, and then var, and I will make it 31 00:01:35,401 --> 00:01:39,166 equal to let's say a value of 100. Next, I 32 00:01:39,167 --> 00:01:42,000 will write a simple loop by writing, loop 33 00:01:42,001 --> 00:01:45,066 followed by two curly brackets. Inside the 34 00:01:45,067 --> 00:01:48,266 loop, I will first deincrement the value of 35 00:01:48,267 --> 00:01:52,066 the variable var by one, because we are 36 00:01:52,067 --> 00:01:54,466 looking for a number which is strictly less 37 00:01:54,467 --> 00:01:57,400 than the current number, which is divisible by 13. 38 00:01:57,401 --> 00:02:00,566 So, I will write a var = var - 1. 39 00:02:01,300 --> 00:02:03,500 Next, I will check if the number 40 00:02:03,501 --> 00:02:06,200 is divisible by 13 or not. So, I will write 41 00:02:06,201 --> 00:02:09,466 var and then % sign, which is a mod operator, 42 00:02:09,765 --> 00:02:12,300 and then 13 == 0. 43 00:02:13,800 --> 00:02:17,366 Now, if the if statement is true, it will 44 00:02:17,367 --> 00:02:20,600 mean, that we need to exit from the loop, 45 00:02:20,601 --> 00:02:23,633 because we have found our desired number. 46 00:02:23,866 --> 00:02:26,066 To exit from the loop, we will use a break 47 00:02:26,067 --> 00:02:28,533 statement, inside the body of the if statement 48 00:02:28,534 --> 00:02:31,666 by writing a break. In case the condition is 49 00:02:31,667 --> 00:02:34,233 not true, we will continue with the next 50 00:02:34,234 --> 00:02:37,833 iteration, where again, the variable var will 51 00:02:37,834 --> 00:02:40,366 be reduced by a value of one, and the 52 00:02:40,367 --> 00:02:44,166 condition will be checked again. Outside the 53 00:02:44,167 --> 00:02:46,233 body of the loop, I will display the number 54 00:02:46,234 --> 00:02:49,400 which I may found inside the loop by writing 55 00:02:49,666 --> 00:02:51,700 println, followed by exclamation mark, and 56 00:02:51,701 --> 00:02:53,300 then within the parentheses, I will put two 57 00:02:53,301 --> 00:02:56,533 quotes where I will write, The highest number 58 00:02:56,866 --> 00:03:01,500 lesser than the given number divisible by 13 59 00:03:01,501 --> 00:03:04,133 is, curly brackets, var. 60 00:03:05,100 --> 00:03:07,800 The break statement inside the if statement will 61 00:03:07,801 --> 00:03:11,100 terminate the loop, once it is encountered, 62 00:03:11,133 --> 00:03:14,033 and it will shift the control to the first 63 00:03:14,066 --> 00:03:16,333 statement outside the body of the loop, 64 00:03:16,334 --> 00:03:20,700 which will be the print statement. So let us cargo run this. 65 00:03:21,333 --> 00:03:23,833 Yes, the correct answer, which is 91, 66 00:03:23,834 --> 00:03:26,533 is being displayed to us. Again please note 67 00:03:26,534 --> 00:03:28,533 that the break will move the control to the 68 00:03:28,534 --> 00:03:31,600 first statement after the body of the loop. 69 00:03:32,566 --> 00:03:35,533 Let us now learn the second topic of today's lecture. 70 00:03:36,200 --> 00:03:38,300 I will add suitable comments first, 71 00:03:38,301 --> 00:03:40,966 and will comment out the code that we have 72 00:03:40,967 --> 00:03:42,166 written so far. 73 00:03:42,167 --> 00:03:47,400 [No Audio] 74 00:03:47,401 --> 00:03:49,900 The continue statement skips the current 75 00:03:49,901 --> 00:03:52,500 iteration of the loop and continues with the 76 00:03:52,501 --> 00:03:55,566 next iteration. When the program encounters 77 00:03:55,567 --> 00:03:58,566 the continue, the continue statement, all 78 00:03:58,567 --> 00:04:00,733 the statements beneath the continue 79 00:04:00,734 --> 00:04:02,900 statement will be skipped, and the next 80 00:04:02,901 --> 00:04:05,233 iteration of the loop will be executed. 81 00:04:05,600 --> 00:04:08,566 Let us do a simple example to illustrate the 82 00:04:08,567 --> 00:04:11,666 use of continue. I will write a simple 83 00:04:11,667 --> 00:04:14,100 program in which I want to compute the first 84 00:04:14,166 --> 00:04:17,132 three numbers with the property, that they are 85 00:04:17,133 --> 00:04:20,399 divisible by both 3 and 5. The program 86 00:04:20,400 --> 00:04:22,700 will start with the number 0, and will 87 00:04:22,701 --> 00:04:25,733 keep on incrementing it by a value of one, and 88 00:04:25,734 --> 00:04:28,600 keep on checking for the property. We know 89 00:04:28,866 --> 00:04:31,713 that the first such number will be the number 15, 90 00:04:31,714 --> 00:04:34,033 and the second number is 30, and the third 91 00:04:34,034 --> 00:04:37,200 number is 45. When the first three numbers 92 00:04:37,201 --> 00:04:40,833 are found with the proposed property, the 93 00:04:40,834 --> 00:04:43,300 program should stop processing further 94 00:04:43,301 --> 00:04:46,333 numbers. Let us start implementing this program. 95 00:04:46,733 --> 00:04:48,866 I will start by declaring a variable 96 00:04:48,867 --> 00:04:51,400 var with a value of zero, so I will write 97 00:04:51,401 --> 00:04:54,100 let mut var: i32 = 0. 98 00:04:54,833 --> 00:04:57,966 This variable will be incremented sequentially and 99 00:04:57,967 --> 00:05:00,466 will be checked for the required property. 100 00:05:01,133 --> 00:05:03,200 Next, I will declare another variable, which 101 00:05:03,201 --> 00:05:05,133 will keep on counting the numbers with the 102 00:05:05,134 --> 00:05:09,100 property. So, I will write let mut count : i32 = 0. 103 00:05:10,466 --> 00:05:11,800 Next, I will declare an 104 00:05:11,801 --> 00:05:15,433 infinite loop by writing, loop followed by curly brackets. 105 00:05:16,666 --> 00:05:18,100 During each iteration of the 106 00:05:18,101 --> 00:05:20,166 loop, I will first increment the variable 107 00:05:20,167 --> 00:05:24,500 var by one, so I will write var = var +1 108 00:05:25,471 --> 00:05:26,938 Next, I will check if the 109 00:05:26,939 --> 00:05:29,266 current number under investigation has the 110 00:05:29,267 --> 00:05:32,533 property or not, so I will write var % 111 00:05:32,534 --> 00:05:37,000 sign, followed by a value of 5 == 0. 112 00:05:37,533 --> 00:05:38,700 And then since I am 113 00:05:38,701 --> 00:05:40,800 looking for another condition also, so I 114 00:05:40,801 --> 00:05:44,100 will write &&, and then 115 00:05:44,101 --> 00:05:47,666 followed by var % 3 == 0. 116 00:05:48,533 --> 00:05:50,766 Inside the if body, I will add a 117 00:05:50,767 --> 00:05:52,600 print statement by writing println 118 00:05:52,601 --> 00:05:54,133 followed by exclamation mark, and then 119 00:05:54,134 --> 00:05:55,900 within the parentheses, I will put two quotes 120 00:05:55,901 --> 00:05:59,133 where I will write the number which is 121 00:05:59,134 --> 00:06:04,166 divisible by both 3 and 5 equals to, curly brackets, 122 00:06:04,933 --> 00:06:07,700 and then followed by /, var. 123 00:06:09,133 --> 00:06:10,333 This statement will be 124 00:06:10,334 --> 00:06:12,633 printed each time I found the number with the 125 00:06:12,634 --> 00:06:17,033 property. Next, inside the same if body, I will 126 00:06:17,066 --> 00:06:19,300 add one to the value of count, by writing 127 00:06:19,301 --> 00:06:23,033 count = count +1. Next, inside 128 00:06:23,034 --> 00:06:26,300 the if statement, I will check if the value 129 00:06:26,301 --> 00:06:28,133 of the count is equal to 3 or not, 130 00:06:28,566 --> 00:06:30,815 because when it reaches the value of 3, 131 00:06:30,816 --> 00:06:34,033 I want to quit the program, because I intend to 132 00:06:34,034 --> 00:06:36,333 find the first 3 numbers with the property. 133 00:06:36,334 --> 00:06:39,400 So, I will write, if count == 3, 134 00:06:39,666 --> 00:06:41,066 and then within the curly 135 00:06:41,067 --> 00:06:44,700 brackets, I will write break. Next, 136 00:06:44,701 --> 00:06:47,766 outside the body of the outer if statement, I 137 00:06:47,767 --> 00:06:50,133 will add a simple print statement which will 138 00:06:50,134 --> 00:06:52,366 print the current number under investigation 139 00:06:52,367 --> 00:06:54,266 by writing println 140 00:06:54,300 --> 00:06:56,800 exclamation mark. And then within the 141 00:06:56,801 --> 00:06:59,266 parentheses, I will put two quotes, and inside 142 00:06:59,267 --> 00:07:02,666 the two quotes, I will write curly brackets, var. 143 00:07:03,733 --> 00:07:06,466 This means that, during each 144 00:07:06,666 --> 00:07:08,966 iteration of the program, I will first 145 00:07:08,967 --> 00:07:11,633 increment a variable var, and then we'll 146 00:07:11,634 --> 00:07:14,366 check the condition. And if the condition is 147 00:07:14,367 --> 00:07:17,100 satisfied, I will print this message followed 148 00:07:17,101 --> 00:07:20,366 by this print, and then at this condition. 149 00:07:21,000 --> 00:07:23,666 Let us cargo run this program, and then we 150 00:07:23,667 --> 00:07:26,266 will refine its output by making use of the 151 00:07:26,300 --> 00:07:30,866 continue statement. Okay, you may note that we 152 00:07:30,867 --> 00:07:33,000 have the number that is being printed. And 153 00:07:33,001 --> 00:07:35,033 when the condition is being met, we have the 154 00:07:35,034 --> 00:07:38,000 special output afterwards, the program again 155 00:07:38,001 --> 00:07:40,066 starts to print the numbers starting from the 156 00:07:40,067 --> 00:07:44,033 value of 15. This looks a bit bad, because we 157 00:07:44,066 --> 00:07:46,500 already have a special print statement for 158 00:07:46,501 --> 00:07:49,700 the value of 15. Now, we want the program to 159 00:07:49,701 --> 00:07:52,533 skip printing the value of 15, and in the same 160 00:07:52,534 --> 00:07:56,433 way, after the value of 30. Let us see how we 161 00:07:56,434 --> 00:07:59,433 can skip printing the values of 15 and 30. 162 00:08:00,566 --> 00:08:04,366 The if statement we see that, when the 163 00:08:04,367 --> 00:08:07,300 count == 3, the loop is being terminated. 164 00:08:07,301 --> 00:08:09,000 However, when the count is 165 00:08:09,001 --> 00:08:11,533 not equal to three, then the loop continues 166 00:08:11,534 --> 00:08:14,933 with the remaining statements beneath the if statement. 167 00:08:15,200 --> 00:08:17,600 That is, it keep on executing the 168 00:08:17,601 --> 00:08:20,666 statement of the print statement. If we write 169 00:08:20,700 --> 00:08:23,533 after the if statement, else, and then within 170 00:08:23,534 --> 00:08:26,033 the curly brackets, continue, the program will 171 00:08:26,034 --> 00:08:28,533 not continue with the remaining of the 172 00:08:28,534 --> 00:08:31,466 statements in the loop, and will not print the number. 173 00:08:32,332 --> 00:08:34,500 This means that, when the outer if 174 00:08:34,501 --> 00:08:38,100 statement is true, the inner if will get a chance. 175 00:08:38,265 --> 00:08:41,500 If in that case, the inner if is true, 176 00:08:41,501 --> 00:08:44,666 the loop will terminate. However, in case 177 00:08:44,667 --> 00:08:47,466 when it is not true, the else part will be 178 00:08:47,467 --> 00:08:50,000 executed, which will skip the execution of the 179 00:08:50,001 --> 00:08:52,800 current iteration, thereby, avoiding the print 180 00:08:52,801 --> 00:08:56,066 statement beneath it to get executed, and will 181 00:08:56,067 --> 00:08:58,266 shift the control to the next iteration. 182 00:08:58,633 --> 00:09:01,766 Let us cargo run to check, if the desired output 183 00:09:01,767 --> 00:09:03,833 is being achieved by making use of the 184 00:09:03,834 --> 00:09:08,333 continue statement. Yes, we may note that, the 185 00:09:08,334 --> 00:09:11,442 value of 15 and 30 are not part of the 186 00:09:11,443 --> 00:09:15,042 printed values anymore. In some sense, 187 00:09:15,500 --> 00:09:18,033 the continue has some similarities with the break. 188 00:09:18,133 --> 00:09:21,000 However, instead of forcing termination 189 00:09:21,001 --> 00:09:23,900 of the entire loop, it forces the next 190 00:09:23,901 --> 00:09:25,666 iteration of the loop to take place, 191 00:09:25,866 --> 00:09:29,166 skipping any code coming after the continue in the 192 00:09:29,167 --> 00:09:33,000 same iteration. There is a special use of the break, 193 00:09:33,133 --> 00:09:36,333 when used in association with loops. 194 00:09:36,400 --> 00:09:39,900 It can also be used to return a value from 195 00:09:39,901 --> 00:09:43,600 that loop. This is only valid with simple loops, 196 00:09:43,666 --> 00:09:45,900 and not with any other type of loops, 197 00:09:45,901 --> 00:09:48,000 such as for loops or while loops. For 198 00:09:48,001 --> 00:09:51,166 instance, if in the same program, I want to 199 00:09:51,167 --> 00:09:54,600 return the third value with the property, that 200 00:09:54,601 --> 00:09:57,966 it is divisible by both 3 and 5. 201 00:09:57,967 --> 00:10:01,000 So I will write something like, something like, 202 00:10:01,001 --> 00:10:06,266 let req_number = loop at the top. 203 00:10:07,100 --> 00:10:09,166 Next, when I use the break, 204 00:10:09,167 --> 00:10:12,366 which basically exits from the loop, I will 205 00:10:12,367 --> 00:10:14,433 mention the variable name, which I want to 206 00:10:14,434 --> 00:10:16,866 assign to the req_number, 207 00:10:16,933 --> 00:10:20,100 which in this case will be the variable of var. 208 00:10:20,101 --> 00:10:22,266 So I will write break followed by the 209 00:10:22,267 --> 00:10:25,866 variable name var. Outside the body of the 210 00:10:25,867 --> 00:10:29,300 loop, I will add semicolon. Let us add a 211 00:10:29,301 --> 00:10:31,466 print statement outside the body of the loop 212 00:10:31,467 --> 00:10:33,800 for displaying the value of the variable 213 00:10:34,033 --> 00:10:37,133 req_number, by writing println 214 00:10:37,134 --> 00:10:39,900 followed by a exclamation mark, followed 215 00:10:39,901 --> 00:10:42,600 by parentheses, and then within the two quotes 216 00:10:42,601 --> 00:10:46,733 I will write the required third highest 217 00:10:46,833 --> 00:10:49,966 number divisible by both 3 and 5 is, 218 00:10:50,000 --> 00:10:53,466 curly brackets, req_number. 219 00:10:54,266 --> 00:10:56,933 Let me cargo run this, and we may note that 220 00:10:56,934 --> 00:10:59,100 the variable value has been correctly 221 00:10:59,101 --> 00:11:01,900 displayed. Please note that, this is a special 222 00:11:01,901 --> 00:11:04,566 use of the break, which can only be used when 223 00:11:04,567 --> 00:11:08,100 the break is used in connection with simple loops. 224 00:11:08,666 --> 00:11:11,733 Also note, that in the same way, is used 225 00:11:11,734 --> 00:11:14,400 with simple loops. The two constructs of 226 00:11:14,401 --> 00:11:17,000 break and continue may be used in combination 227 00:11:17,001 --> 00:11:19,333 with the for loops and while loops. The 228 00:11:19,334 --> 00:11:21,700 essential working is the same as explained, 229 00:11:21,701 --> 00:11:25,300 and the behavior will not change. Okay, that 230 00:11:25,301 --> 00:11:27,700 brings us to the end of this tutorial. I hope 231 00:11:27,701 --> 00:11:30,833 you would have enjoyed this. That also brings 232 00:11:30,834 --> 00:11:32,733 us to the end of this section, and now in the 233 00:11:32,734 --> 00:11:34,733 new section we will be coming up with more 234 00:11:34,734 --> 00:11:36,800 interesting concepts. So do come back to cover 235 00:11:36,801 --> 00:11:40,166 those. And until next tutorial, happy Rust programming. 236 00:11:40,167 --> 00:11:44,966 [No Audio]