1 00:00:00,009 --> 00:00:04,400 Let's now talk about the break statement because it's a very, 2 00:00:04,409 --> 00:00:07,119 very powerful statement and we're going to be working with it in 3 00:00:07,449 --> 00:00:09,810 this course. But before 4 00:00:10,170 --> 00:00:11,149 I do that, 5 00:00:12,010 --> 00:00:16,270 I just want to show you a different way of actually printing out 6 00:00:16,889 --> 00:00:20,500 the names in this particular example. 7 00:00:20,799 --> 00:00:25,770 Right now we're saying good morning and then plus and then XX, of course, 8 00:00:25,780 --> 00:00:27,590 being the variable 9 00:00:27,829 --> 00:00:31,229 representing each name in our list. 10 00:00:32,039 --> 00:00:36,558 Another way of writing this would be to do something like this. First of all, 11 00:00:36,699 --> 00:00:39,499 I'm going to remove the uh plus X 12 00:00:40,209 --> 00:00:40,750 OK? 13 00:00:41,490 --> 00:00:44,830 But now I'm gonna come right here at the very beginning, 14 00:00:45,310 --> 00:00:47,549 I'm going to type in F 15 00:00:48,520 --> 00:00:49,220 OK? 16 00:00:49,639 --> 00:00:51,900 And now right here, 17 00:00:52,419 --> 00:00:57,549 I'm going to add my curly braces, not the usual braces, but the curly ones. 18 00:00:57,900 --> 00:00:58,939 And now here 19 00:00:59,209 --> 00:01:01,090 I will simply type in X 20 00:01:01,750 --> 00:01:02,419 OK. 21 00:01:02,729 --> 00:01:07,260 I can run the program and you can see right now we have the exact same results. 22 00:01:07,480 --> 00:01:12,730 This right here is a different and also a more efficient way 23 00:01:12,980 --> 00:01:16,250 of actually printing out 24 00:01:16,370 --> 00:01:21,440 the values of the variables in a list or an array 25 00:01:21,709 --> 00:01:22,730 in a for 26 00:01:22,889 --> 00:01:24,930 or while loop. 27 00:01:25,349 --> 00:01:26,019 All right. 28 00:01:26,300 --> 00:01:29,489 And we're going to be working like this moving forwards. 29 00:01:29,809 --> 00:01:30,529 Now, 30 00:01:31,470 --> 00:01:35,000 the break statement, what exactly is the brake statement 31 00:01:35,330 --> 00:01:40,879 it's used when we want to terminate a loop early. 32 00:01:41,629 --> 00:01:45,730 For example, in this folder that we've seen right now, what it does is that it simply, 33 00:01:45,739 --> 00:01:47,169 it's simply gonna go through 34 00:01:47,910 --> 00:01:51,209 all the names in our list and then print out 35 00:01:51,510 --> 00:01:54,279 uh good morning and then the name of the person, right? 36 00:01:54,529 --> 00:01:56,440 But what if for one reason or the other, 37 00:01:56,449 --> 00:02:02,230 we wanted to terminate the loop as soon as James has been reached, 38 00:02:02,239 --> 00:02:06,250 we don't want to print out a good morning Violetta because we don't like Violetta, 39 00:02:06,379 --> 00:02:06,870 right? 40 00:02:07,040 --> 00:02:11,889 This is where we could use the break statement 41 00:02:12,210 --> 00:02:14,009 to terminate the loop early 42 00:02:14,559 --> 00:02:18,960 and to demonstrate this, I'm gonna write a completely different program. OK? So 43 00:02:19,089 --> 00:02:21,970 let's do this. All right, I'm gonna type in, let's say numbers again. 44 00:02:21,979 --> 00:02:25,539 I'm gonna create a new list right here. Numbers equals 45 00:02:26,139 --> 00:02:26,839 and 46 00:02:27,149 --> 00:02:28,740 I'm gonna put in my 47 00:02:29,589 --> 00:02:30,770 brackets in here 48 00:02:31,080 --> 00:02:38,410 and let's go with a series of numbers. I'm gonna go with 13. Let's go with uh 78, 10 49 00:02:39,389 --> 00:02:42,809 and let's go with 11 and let's add one more 50 00:02:43,139 --> 00:02:44,820 even number 14. 51 00:02:45,259 --> 00:02:45,809 OK. 52 00:02:46,199 --> 00:02:48,169 So we have a list of numbers in here. 53 00:02:48,580 --> 00:02:51,889 And what we want to do right now is we want to create a loop 54 00:02:52,270 --> 00:02:54,339 that will run through all the numbers. 55 00:02:54,350 --> 00:02:59,580 However, once the first even number has been reached, 56 00:02:59,720 --> 00:03:04,690 we want to terminate the loop, meaning that once the number, which is eight, 57 00:03:05,029 --> 00:03:08,529 we're going to end the loop. So it should run four times 58 00:03:08,770 --> 00:03:12,669 the first loop for one, the second loop for three, third loop for seven, 59 00:03:12,679 --> 00:03:14,460 fourth loop for eight. 60 00:03:14,589 --> 00:03:15,860 So think about this, right, 61 00:03:16,029 --> 00:03:20,309 we're gonna create the for loop first and then we're going to introduce the if 62 00:03:20,490 --> 00:03:23,509 statement. So the if statement will check. OK. 63 00:03:23,520 --> 00:03:30,419 Is this number actually even, or is it odd if it's even then break the loop? 64 00:03:30,429 --> 00:03:33,509 Because we found the first even number elses, 65 00:03:34,000 --> 00:03:36,690 if it's not an even number, if it's an odd number, 66 00:03:37,020 --> 00:03:38,419 continue 67 00:03:38,619 --> 00:03:40,940 with the loop. So 68 00:03:41,190 --> 00:03:43,139 how are we going to do this 69 00:03:43,949 --> 00:03:45,399 gonna tap and enter 70 00:03:45,899 --> 00:03:46,160 the 71 00:03:46,479 --> 00:03:47,149 follow up for, 72 00:03:47,880 --> 00:03:48,899 let's say num 73 00:03:49,139 --> 00:03:52,910 OK, no, would represent the, the numbers. 74 00:03:53,119 --> 00:03:56,479 And by the way, I'm not sure if I've mentioned this before, but I think it's, 75 00:03:56,490 --> 00:03:57,320 it's time I do. 76 00:03:57,330 --> 00:03:57,699 So 77 00:03:58,059 --> 00:03:59,830 see, I've only typed in for number. 78 00:03:59,839 --> 00:04:04,169 You can see right now on my screen, Python is already suggesting in numbers 79 00:04:04,289 --> 00:04:06,389 because Python is actually quite smart, right? 80 00:04:06,750 --> 00:04:07,259 So in 81 00:04:07,399 --> 00:04:08,669 this kind of situation right now, 82 00:04:08,679 --> 00:04:11,830 if you're happy with the suggestion that the program has given you, 83 00:04:11,949 --> 00:04:12,330 you can see, 84 00:04:12,630 --> 00:04:13,979 press the tab 85 00:04:14,490 --> 00:04:15,990 a key on your keyboard 86 00:04:16,170 --> 00:04:16,738 and 87 00:04:16,980 --> 00:04:20,640 you will get the exact same result, right? So I've typed, I 88 00:04:22,079 --> 00:04:23,670 do forgive me. I can't talk, 89 00:04:24,019 --> 00:04:25,630 I've typed the tab number 90 00:04:26,440 --> 00:04:31,519 on the tab key on my keyboard. And now I have for num in numbers, I'm gonna add my colon. 91 00:04:31,529 --> 00:04:32,609 Ok. Now 92 00:04:32,929 --> 00:04:36,730 I'm going to add the if statement. And would you look at that? 93 00:04:37,809 --> 00:04:41,989 I promise you. I've actually not typed this program before, 94 00:04:42,100 --> 00:04:43,940 but I think Python is so intelligent. 95 00:04:43,950 --> 00:04:46,459 It already has an idea of what I might be looking for. 96 00:04:46,720 --> 00:04:48,369 Yes, I want if none 97 00:04:49,040 --> 00:04:54,190 and then percentage two equals zero. So what exactly is this, what is going on here? 98 00:04:54,470 --> 00:04:59,390 What this means is that we're going to check if the number that's been pulled out, 99 00:04:59,570 --> 00:05:04,029 if it's divisible by two and the remainder is equal to zero, 100 00:05:04,040 --> 00:05:07,029 then we know for sure that it is an even number. 101 00:05:07,369 --> 00:05:09,399 If you divide seven by two, 102 00:05:09,510 --> 00:05:15,470 you will get 3.5, 3.5 there's always going to be that remainder may have been half. 103 00:05:15,480 --> 00:05:20,179 But when you divide eight by two, the answer is straight up four. 104 00:05:20,410 --> 00:05:24,799 It's not 4.1 it's not 4.2 it's not 4.5, it is four. 105 00:05:24,809 --> 00:05:30,049 So this is how we can check if the number is actually even. 106 00:05:30,170 --> 00:05:30,790 So 107 00:05:31,079 --> 00:05:33,899 if the number is, in fact, even what do we do, 108 00:05:34,149 --> 00:05:36,070 let's say print, 109 00:05:36,730 --> 00:05:37,510 OK. 110 00:05:38,250 --> 00:05:41,690 And now I'm going to add the F string. 111 00:05:42,600 --> 00:05:45,709 OK? And now the actual string itself 112 00:05:45,890 --> 00:05:49,459 found the first even 113 00:05:49,940 --> 00:05:51,070 number. 114 00:05:51,980 --> 00:05:57,850 OK, let's add my comma. And now I want to display the actual number, curly braces. 115 00:05:57,890 --> 00:06:00,839 And what is it gonna be? It is going to be numb. 116 00:06:01,940 --> 00:06:03,160 OK. 117 00:06:03,500 --> 00:06:04,480 Awesome. 118 00:06:04,910 --> 00:06:05,440 So 119 00:06:06,130 --> 00:06:07,320 we've added what 120 00:06:07,470 --> 00:06:10,140 the program should do if the number is, in fact, 121 00:06:10,779 --> 00:06:16,959 an even number Prince says, OK, we found the first number and now break the loop, 122 00:06:16,980 --> 00:06:19,350 we don't need the loop to want anymore. 123 00:06:19,359 --> 00:06:22,920 Break it. It's over. We found the first even number we're getting out of here. 124 00:06:23,779 --> 00:06:24,859 Else. 125 00:06:26,339 --> 00:06:29,619 Make sure your tabs. Remember the tabulation is extremely important. 126 00:06:29,730 --> 00:06:34,679 If else must be on the same tab right there, the same, the same indentation, right? 127 00:06:34,950 --> 00:06:35,299 So 128 00:06:37,029 --> 00:06:39,929 simply print out, let's just say print, 129 00:06:40,760 --> 00:06:42,010 um, 130 00:06:42,350 --> 00:06:44,940 let me add my brackets and then my coats 131 00:06:45,630 --> 00:06:48,130 still looking 132 00:06:49,070 --> 00:06:51,140 for that even 133 00:06:51,489 --> 00:06:52,239 number. 134 00:06:53,299 --> 00:06:53,940 And 135 00:06:54,589 --> 00:06:55,170 there it is 136 00:06:56,089 --> 00:06:56,679 OK. 137 00:06:57,089 --> 00:06:58,470 Let's go ahead right now, 138 00:06:58,970 --> 00:07:01,910 run the program and there it is 139 00:07:02,390 --> 00:07:04,179 still looking for that even number, 140 00:07:04,190 --> 00:07:06,589 still looking for that even numbers looking for that even number. 141 00:07:06,600 --> 00:07:11,649 Oh, look, we found the first even number and the number is eight. 142 00:07:12,140 --> 00:07:16,869 This is the use of the break statement. 143 00:07:17,309 --> 00:07:24,709 Now let's use it in a while loop now to do this, I'm gonna come all the way down here 144 00:07:25,239 --> 00:07:31,980 and let's create a very simple program that will ask the user to type in something. 145 00:07:32,339 --> 00:07:38,980 And as long as or type in a particular letter and as long as the letter isn't equal to Q, 146 00:07:39,339 --> 00:07:41,019 then just keep asking 147 00:07:41,230 --> 00:07:44,100 the user for a new letter. 148 00:07:44,359 --> 00:07:44,899 OK? 149 00:07:45,179 --> 00:07:45,640 So 150 00:07:45,850 --> 00:07:47,950 that's basically the condition that needs to be satisfied. 151 00:07:47,959 --> 00:07:54,190 So while the user is typing in AB CD, ef as long as it's not Q, 152 00:07:54,290 --> 00:07:57,920 keep asking the user to input a new letter. So 153 00:07:58,350 --> 00:08:00,920 I'm going to say while true 154 00:08:02,630 --> 00:08:06,130 what this does right now, this is a very simple function 155 00:08:06,390 --> 00:08:08,130 or loop in Python 156 00:08:08,290 --> 00:08:13,450 that automatically has created a condition that is always true. 157 00:08:13,709 --> 00:08:17,910 Unless something else happens. Remember that with the wild loops, 158 00:08:18,040 --> 00:08:19,890 there needs to be a particular condition 159 00:08:20,049 --> 00:08:25,089 that must be continually satisfied in order for the loop to actually run 160 00:08:25,190 --> 00:08:30,130 and then it must be terminated once a very specific condition has been met. So 161 00:08:30,399 --> 00:08:32,500 we're going to assume right now that 162 00:08:33,289 --> 00:08:35,030 the condition is always true. 163 00:08:35,479 --> 00:08:39,030 And now I'm going to type in user input, 164 00:08:39,330 --> 00:08:41,539 this is going to be the variable 165 00:08:41,789 --> 00:08:43,109 that will represent 166 00:08:43,330 --> 00:08:44,469 the letter 167 00:08:44,669 --> 00:08:46,869 that the user will actually type in. So 168 00:08:47,059 --> 00:08:49,750 I'm gonna say user input equals and now 169 00:08:50,270 --> 00:08:51,210 input, 170 00:08:51,400 --> 00:08:55,390 remember the function input, which will now ask the user to type in something. So 171 00:08:56,059 --> 00:08:57,080 I'm gonna say 172 00:08:57,270 --> 00:08:58,119 uh 173 00:08:58,859 --> 00:08:59,760 enter 174 00:09:00,489 --> 00:09:01,400 a letter 175 00:09:03,010 --> 00:09:04,369 and then in brackets, 176 00:09:04,510 --> 00:09:06,190 I'm gonna say all 177 00:09:06,580 --> 00:09:09,030 type a Q, 178 00:09:10,520 --> 00:09:11,299 OK? 179 00:09:11,909 --> 00:09:16,099 Type Q to exit the program. 180 00:09:16,849 --> 00:09:22,349 OK? So we're giving the users here a choice. OK? You can either just type in a letter 181 00:09:22,450 --> 00:09:25,510 or if you want to exit the program, just type in Q. 182 00:09:25,830 --> 00:09:26,469 OK? 183 00:09:26,630 --> 00:09:27,150 So 184 00:09:27,880 --> 00:09:31,299 I'm going to press enter. So now let's type in 185 00:09:31,619 --> 00:09:32,940 the condition 186 00:09:33,309 --> 00:09:34,559 that most that 187 00:09:34,690 --> 00:09:38,419 we need to check whether or not the input was actually QM 188 00:09:39,020 --> 00:09:41,059 gonna come in right now, add 189 00:09:41,559 --> 00:09:43,020 the if statement. 190 00:09:43,340 --> 00:09:47,479 So we're gonna check if the user input is equal to Q, 191 00:09:47,919 --> 00:09:50,419 what should we do? Well, 192 00:09:50,859 --> 00:09:53,299 I'm going to say, print, 193 00:09:55,340 --> 00:09:56,960 uh, in brackets 194 00:09:58,969 --> 00:10:00,419 and I'm gonna say 195 00:10:01,590 --> 00:10:02,619 you have, 196 00:10:02,909 --> 00:10:03,659 you 197 00:10:04,020 --> 00:10:06,219 have chosen to exit 198 00:10:06,570 --> 00:10:07,419 the program, 199 00:10:09,090 --> 00:10:10,830 uh, just exit the program, 200 00:10:12,440 --> 00:10:13,580 press enter 201 00:10:13,890 --> 00:10:14,780 and there it is. 202 00:10:15,260 --> 00:10:18,469 And now since they've chosen to, oh, let me add the closing 203 00:10:18,799 --> 00:10:24,619 code here. Since they've chosen to exit the program, we are now going to break 204 00:10:24,900 --> 00:10:25,619 the loop. 205 00:10:27,130 --> 00:10:29,609 Otherwise I'm gonna come in here right now 206 00:10:31,880 --> 00:10:33,570 and then say print 207 00:10:37,530 --> 00:10:38,109 and thou 208 00:10:39,809 --> 00:10:40,760 in quotes 209 00:10:41,309 --> 00:10:42,179 you 210 00:10:42,900 --> 00:10:43,979 typed. 211 00:10:45,260 --> 00:10:48,320 And now remember in the curly brackets, 212 00:10:48,799 --> 00:10:52,359 I'm going to say user underscore input 213 00:10:53,210 --> 00:10:53,869 and 214 00:10:53,989 --> 00:10:55,729 there it is, 215 00:10:56,780 --> 00:10:57,159 I 216 00:10:58,440 --> 00:10:59,619 gonna go ahead right now, 217 00:10:59,719 --> 00:11:01,099 run the program. 218 00:11:02,380 --> 00:11:04,979 OK? So now it's saying enter a letter, 219 00:11:05,099 --> 00:11:06,539 I'm gonna say Y 220 00:11:06,809 --> 00:11:10,359 press enter, OK? Now it says, OK, you typed Y 221 00:11:10,559 --> 00:11:12,710 enter another letter. I'm gonna say a 222 00:11:13,520 --> 00:11:16,219 once again it's running until I now type in Q 223 00:11:16,630 --> 00:11:17,489 and now 224 00:11:17,760 --> 00:11:20,250 it has terminated the program. 225 00:11:20,650 --> 00:11:23,719 And this right here is us combining the 226 00:11:24,469 --> 00:11:25,690 our while statement, 227 00:11:25,830 --> 00:11:28,489 the wild loop brother with the break statement. 228 00:11:28,570 --> 00:11:32,809 So let me run through this over again. I know you might have a question. 229 00:11:33,090 --> 00:11:36,510 Why didn't we type in the LS? 230 00:11:36,650 --> 00:11:39,869 Because we have the if statement? OK. So where is the LS? Well, 231 00:11:40,010 --> 00:11:42,010 we could actually add the LS. 232 00:11:42,250 --> 00:11:43,609 It's going to be right here 233 00:11:44,059 --> 00:11:45,690 and I'll just say LS 234 00:11:46,369 --> 00:11:47,140 and then 235 00:11:47,380 --> 00:11:47,650 the Tate 236 00:11:48,109 --> 00:11:48,729 print, 237 00:11:49,520 --> 00:11:53,219 you typed this. So I can go ahead right now, run the exact same program again. 238 00:11:53,700 --> 00:11:55,440 OK. Come in here right now, type in G, 239 00:11:55,900 --> 00:11:56,859 it works well, 240 00:11:57,099 --> 00:12:03,469 type in U, it works well typ in Q enter and now it works the exact same way. 241 00:12:03,479 --> 00:12:06,219 So the one that you need to understand here is that 242 00:12:06,229 --> 00:12:10,830 the reason why the LS statement is actually optional here is because 243 00:12:11,140 --> 00:12:12,590 we're working with the wild 244 00:12:12,700 --> 00:12:13,179 true 245 00:12:13,619 --> 00:12:15,520 uh loop, right, the condition. 246 00:12:15,530 --> 00:12:20,960 So it's going to automatically assume that the condition will always be true. 247 00:12:21,309 --> 00:12:27,119 And now we're using the break statement here to intentionally control 248 00:12:27,330 --> 00:12:30,200 when the loop would actually end. 249 00:12:30,609 --> 00:12:34,500 So there's really no need for the else statement here. 250 00:12:34,690 --> 00:12:37,000 The loop will always run 251 00:12:37,520 --> 00:12:40,659 because we have said while true 252 00:12:40,900 --> 00:12:42,700 and will only stop 253 00:12:42,940 --> 00:12:44,979 via the break statement 254 00:12:45,130 --> 00:12:48,020 once the user has typed in the letter Q. 255 00:12:48,030 --> 00:12:51,900 So really the else statement here is not necessary, 256 00:12:51,909 --> 00:12:54,700 you can still type it in the program will still work perfectly fine. 257 00:12:54,960 --> 00:12:58,539 But whenever you're working with the while true and then the break statements, 258 00:12:58,669 --> 00:13:02,619 you don't need to add the LS condition uh any more. 259 00:13:02,929 --> 00:13:07,750 So we've done quite a lot in this particular video. Hopefully 260 00:13:07,919 --> 00:13:11,869 you've been able to follow and hopefully you now understand how the break statement 261 00:13:12,130 --> 00:13:13,489 is used. 262 00:13:13,619 --> 00:13:16,750 Thank you so much for watching the video. I will see you in the next class.