1 00:00:12,430 --> 00:00:13,540 Hello, everybody. 2 00:00:13,580 --> 00:00:17,020 Sorry if I scared you, just checking your alertness. 3 00:00:18,010 --> 00:00:20,710 All right, now we know our way in loops. 4 00:00:20,710 --> 00:00:27,070 We can all say that, right, because we have been over lots of examples in force for loops and wire 5 00:00:27,080 --> 00:00:30,700 loops and seen examples and seen homework and done the homework. 6 00:00:30,970 --> 00:00:32,370 And we've been through a lot. 7 00:00:32,380 --> 00:00:38,290 And this path, we've been through a lot, really, it wasn't easy, but now it's time to be professionals 8 00:00:38,290 --> 00:00:44,380 and to be professionals, you know, to to you get to know the hacks of coding and to know some cool 9 00:00:44,380 --> 00:00:46,180 tricks in programming. 10 00:00:46,210 --> 00:00:46,530 All right. 11 00:00:46,930 --> 00:00:50,490 So in Python, we have sort of flexibility of the code. 12 00:00:50,500 --> 00:00:52,840 That's why we call Python, because it's flexible. 13 00:00:53,140 --> 00:00:57,070 And let's see here a few tricks that we can apply upon while loops. 14 00:00:57,790 --> 00:00:58,210 All right. 15 00:00:58,240 --> 00:01:02,440 So stopping a while loop with a break, huh? 16 00:01:02,890 --> 00:01:03,360 All right. 17 00:01:03,520 --> 00:01:09,700 So up until now, we've seen that we can stop while loops only by having a false statement inside the 18 00:01:09,700 --> 00:01:10,300 condition. 19 00:01:10,540 --> 00:01:10,880 All right. 20 00:01:10,990 --> 00:01:13,390 This is the only way that we've seen in previous videos. 21 00:01:13,960 --> 00:01:19,090 We can stop it by having a false condition inside the wire loop. 22 00:01:19,900 --> 00:01:22,210 But this that we already know. 23 00:01:23,080 --> 00:01:30,310 But what if there is an additional way to stop a while loop without mentioning and without addressing 24 00:01:30,310 --> 00:01:32,500 the condition inside the whale? 25 00:01:32,800 --> 00:01:36,220 So there is a way to stop a while, even though the condition is true. 26 00:01:38,040 --> 00:01:39,690 We can use our friend the break. 27 00:01:39,780 --> 00:01:45,150 All right, we have encountered the break, if I recall, in collections, and we can also apply here 28 00:01:45,900 --> 00:01:47,340 inside while loops. 29 00:01:47,490 --> 00:01:48,990 And how can we apply that? 30 00:01:48,990 --> 00:01:50,560 We'll see it in a few seconds. 31 00:01:50,580 --> 00:01:50,930 All right. 32 00:01:51,360 --> 00:01:56,190 So we have an example, which is based on the previous example, because I don't want to make things 33 00:01:56,670 --> 00:01:57,860 harder than they are. 34 00:01:58,140 --> 00:02:01,200 So it's the same example from the previous two videos. 35 00:02:01,200 --> 00:02:08,400 And let's see how we can apply the break here and how can we stop the while loop, even though even 36 00:02:08,400 --> 00:02:11,200 though the condition here is keeps being true. 37 00:02:11,490 --> 00:02:12,000 All right. 38 00:02:12,570 --> 00:02:15,540 So let's see, how can we do it? 39 00:02:15,690 --> 00:02:22,190 So basically here in the code below, we again have the number equals zero, which is the counter. 40 00:02:22,980 --> 00:02:26,400 We had the condition while number smaller than 10. 41 00:02:26,610 --> 00:02:32,310 This is the while when its condition and here inside the while loop inside the bulk of code. 42 00:02:32,310 --> 00:02:36,980 Here in the dots, we added we added an additional item. 43 00:02:36,990 --> 00:02:42,330 We have an if and the if statement says if no equals two, then break. 44 00:02:43,530 --> 00:02:46,980 So break will stop the loop once number equals two. 45 00:02:47,640 --> 00:02:49,140 So let's go over the flow. 46 00:02:49,190 --> 00:02:51,050 We're starting the code we're having here. 47 00:02:51,070 --> 00:02:52,470 Number equals zero. 48 00:02:52,710 --> 00:02:55,520 We go inside the wire loop, it's zero. 49 00:02:56,280 --> 00:03:01,150 We get to the if the if is ignored because number is zero at the moment. 50 00:03:01,170 --> 00:03:04,170 So we just print it and grow the number by one. 51 00:03:04,920 --> 00:03:05,310 All right. 52 00:03:05,400 --> 00:03:11,290 From this point we go again to the top and now we have no equals one again going downwards. 53 00:03:11,670 --> 00:03:14,370 This statement is still still ignored. 54 00:03:14,640 --> 00:03:20,070 And again, we're growing the number by one and now we have no equals two. 55 00:03:20,250 --> 00:03:22,200 So two is smaller than ten. 56 00:03:22,200 --> 00:03:22,930 Still true. 57 00:03:23,100 --> 00:03:32,310 So we go inside the wire loop going inside and then we get to the if and now we have if no number equals 58 00:03:32,310 --> 00:03:32,730 two. 59 00:03:33,150 --> 00:03:35,520 So two equals equals two. 60 00:03:35,670 --> 00:03:36,810 This statement is true. 61 00:03:37,020 --> 00:03:40,860 And then we go inside and right here we have the break key word. 62 00:03:41,040 --> 00:03:47,370 And basically what happens now is that the code exits the wire loop completely and goes further down 63 00:03:47,370 --> 00:03:47,850 the code. 64 00:03:48,600 --> 00:03:55,050 So at this point, the wire loop is executed by the code and we ignore it. 65 00:03:55,260 --> 00:04:02,730 We do not do the iterations again and again up until we get to ten, which is in two, which is exit 66 00:04:02,940 --> 00:04:05,720 the wire loop completely because we have a break here. 67 00:04:06,570 --> 00:04:07,730 This is the whole point. 68 00:04:07,740 --> 00:04:15,200 So the break basically can overcome the the condition that we have here in the wild condition. 69 00:04:15,390 --> 00:04:18,270 The break is stronger than the condition on top. 70 00:04:18,450 --> 00:04:20,610 And this is how can we exit? 71 00:04:21,630 --> 00:04:23,310 This is how we can stop a while. 72 00:04:23,310 --> 00:04:24,180 Loop with a break. 73 00:04:24,840 --> 00:04:25,320 All right. 74 00:04:27,090 --> 00:04:30,690 From here, I think we should see an additional example. 75 00:04:30,690 --> 00:04:33,390 Again, a friend from the best seems break. 76 00:04:33,690 --> 00:04:36,060 We have a friend from the base here, which is else. 77 00:04:36,360 --> 00:04:41,760 OK, so now let's see, how can we use while with else? 78 00:04:42,330 --> 00:04:44,580 And it's a pretty logical thing to do. 79 00:04:45,110 --> 00:04:45,690 All right. 80 00:04:45,930 --> 00:04:51,500 It's a pretty logical thing to do because let me just go to the slide from the previous lecture. 81 00:04:51,870 --> 00:04:54,640 So this is the slide from previous lecture. 82 00:04:54,690 --> 00:04:59,670 So here what we're doing, by the way, is the same Codrea, the example. 83 00:04:59,670 --> 00:05:07,030 This one is only with if and break, but it's the same numbers, same everything here. 84 00:05:07,100 --> 00:05:14,220 OK, so let's have a quick look over here and the previous lectures, previous lectures, example here. 85 00:05:14,430 --> 00:05:24,900 And we can see here that the the number grows by one again and again, right up until the number goes 86 00:05:24,900 --> 00:05:27,030 straight up to be what? 87 00:05:27,630 --> 00:05:28,450 To be nine. 88 00:05:28,500 --> 00:05:28,980 All right. 89 00:05:29,190 --> 00:05:35,370 And after the number is nine, we also print it and then we go again. 90 00:05:35,460 --> 00:05:36,380 And I was false. 91 00:05:36,840 --> 00:05:39,310 Now, ten smaller than ten is a false. 92 00:05:39,570 --> 00:05:45,210 So what basically happens the code, because it's the while loop completely and runs whatever comes 93 00:05:45,210 --> 00:05:46,430 after the while. 94 00:05:47,490 --> 00:05:53,580 But what if I wanted to tell the code, don't just go outside the wire loop, do what I tell you to 95 00:05:53,580 --> 00:05:53,760 do. 96 00:05:53,760 --> 00:05:56,520 I want you to do to run something specifically. 97 00:05:56,700 --> 00:06:01,510 I want you to maybe tell me that we outside the wire loop. 98 00:06:02,310 --> 00:06:02,510 Right. 99 00:06:03,540 --> 00:06:07,400 So let's see an example here for what we just discussed. 100 00:06:07,410 --> 00:06:07,780 All right. 101 00:06:08,620 --> 00:06:15,750 So, by the way, my thing in teaching is explaining the same thing in different different types of 102 00:06:15,750 --> 00:06:20,070 views, two or three times up until all the students get the hang of it. 103 00:06:20,070 --> 00:06:25,620 Even the the ones that have some topics, they are strong in in some topics they have difficulties in. 104 00:06:25,830 --> 00:06:28,580 So I'm explaining the same thing in different perspective. 105 00:06:28,590 --> 00:06:34,560 So if you get the hang of it, just straightforward lecture and those of you that have a bit of trouble. 106 00:06:34,740 --> 00:06:36,870 So I'll go over what I just said again. 107 00:06:37,360 --> 00:06:44,440 And let's see the physical example of what we just discussed, so in the past, we used if and else 108 00:06:44,470 --> 00:06:51,700 and it worked perfectly together, but we can use the alst together with wire loop as well as we just 109 00:06:51,700 --> 00:06:52,360 talked about. 110 00:06:52,850 --> 00:06:56,230 We will tell the code instead of exiting the while loop. 111 00:06:56,230 --> 00:07:00,380 Once the condition is false, just go straight ahead to the else. 112 00:07:00,790 --> 00:07:03,910 Same as we had in its statements once. 113 00:07:03,910 --> 00:07:06,450 If a condition is not met. 114 00:07:06,850 --> 00:07:09,430 So go to the elf's, go to the default exit point. 115 00:07:09,580 --> 00:07:14,590 So here we also have a default exit point, but this time it's of that while loop. 116 00:07:15,610 --> 00:07:18,010 So let's see the this is the example. 117 00:07:18,010 --> 00:07:23,920 It's right here what I've just marked and let's see some two bullets regarding what it's going to do. 118 00:07:23,980 --> 00:07:24,280 All right. 119 00:07:24,520 --> 00:07:28,110 So add plus one to the number while it's smaller than five. 120 00:07:28,420 --> 00:07:29,980 So this is the ball right here. 121 00:07:30,760 --> 00:07:33,520 We have a no no equals one. 122 00:07:33,670 --> 00:07:37,510 And inside the wire, which spread the number and add plus one to it. 123 00:07:38,380 --> 00:07:39,700 OK, this is the first part. 124 00:07:40,120 --> 00:07:46,540 The second part is once the condition is false, go to the else and multiply the number by hundred, 125 00:07:47,170 --> 00:07:48,280 then print it. 126 00:07:48,550 --> 00:07:57,550 OK, so we going for one here, first iteration, then we have to second iteration three, four. 127 00:07:57,820 --> 00:08:00,490 And then once we have the number equals five here. 128 00:08:01,090 --> 00:08:03,070 Five is not smaller than five. 129 00:08:03,080 --> 00:08:04,390 This statement is false. 130 00:08:04,780 --> 00:08:10,100 Don't just exit the wire loop, go straight to the LS and and execute this gold right here. 131 00:08:10,620 --> 00:08:20,320 OK, so once we go to the house number is multiplied by 100 and number is no longer less than five. 132 00:08:21,280 --> 00:08:21,810 All right. 133 00:08:21,820 --> 00:08:23,550 And the output will be is following. 134 00:08:23,650 --> 00:08:31,960 We have one to three and once the number is is bigger than five, then we're going to execute this line 135 00:08:31,960 --> 00:08:33,430 right here, which is this print. 136 00:08:33,820 --> 00:08:34,210 Yeah. 137 00:08:34,510 --> 00:08:41,040 And we also execute the multiply by a hundred and our number as five. 138 00:08:41,050 --> 00:08:42,670 So this would be five hundred. 139 00:08:42,860 --> 00:08:43,910 Have a small mistake here. 140 00:08:45,070 --> 00:08:45,560 All right. 141 00:08:45,580 --> 00:08:47,500 So this will be the output. 142 00:08:47,500 --> 00:08:49,290 And I think it's pretty simple. 143 00:08:49,300 --> 00:08:54,040 We also have here default exit point, same as we had for if statements. 144 00:08:54,530 --> 00:08:59,230 And so we have learned several tricks applied on wire loops. 145 00:08:59,230 --> 00:09:03,160 We have seen here that if and break inside. 146 00:09:03,160 --> 00:09:09,490 So we could actually while as we wish and also we have seen an example of how can we use an AL statement 147 00:09:09,700 --> 00:09:16,840 in order to execute the code that we want once the wire loop will be false here. 148 00:09:17,020 --> 00:09:17,500 All right. 149 00:09:17,800 --> 00:09:21,180 These two examples cover the advanced part of wire loops. 150 00:09:21,190 --> 00:09:27,370 I think that we will be meeting in the next video where we go over the assignments and your homework, 151 00:09:27,370 --> 00:09:27,990 basically. 152 00:09:28,300 --> 00:09:31,450 And there'll be a bit challenging, I'm telling you, in advance. 153 00:09:31,450 --> 00:09:36,490 But I think you could get the hang of it because you don't have any material that he's not in the slides. 154 00:09:36,490 --> 00:09:43,920 So all the assignments, you can resolve them with the information that we have taught together that 155 00:09:44,090 --> 00:09:45,310 we have learned together. 156 00:09:45,970 --> 00:09:47,860 And I think that will be just fine. 157 00:09:48,190 --> 00:09:48,760 Thank you. 158 00:09:48,760 --> 00:09:50,200 And I will see you in the next video.