1 00:00:12,950 --> 00:00:18,710 Hi, everybody, today will continue discussing for loops we were going to step it up a notch and see 2 00:00:18,710 --> 00:00:24,190 some more examples in the last lecture we have seen for loops combined with lists. 3 00:00:24,410 --> 00:00:32,120 And today we're going to see how can we use for loops combined with statements and also with a new action 4 00:00:32,140 --> 00:00:33,160 called Break. 5 00:00:33,530 --> 00:00:39,020 So before we begin, let's see a quick sketch of the flow of code. 6 00:00:39,320 --> 00:00:42,410 And right after, we'll see many, many examples. 7 00:00:42,770 --> 00:00:50,540 So, for example, you have a list with several items inside and you want to find which of these numeric 8 00:00:50,540 --> 00:00:52,620 items is bigger than five. 9 00:00:52,880 --> 00:00:54,500 So just for example. 10 00:00:54,500 --> 00:00:56,600 Yeah, it's just a theoretical example. 11 00:00:57,050 --> 00:01:01,550 So each time will we will check if the cell is bigger than five. 12 00:01:01,760 --> 00:01:02,140 All right. 13 00:01:02,150 --> 00:01:10,220 And once we find the correct value, once our requirements will be satisfied, we want to quit running 14 00:01:10,220 --> 00:01:12,500 iterations upon the loop. 15 00:01:12,830 --> 00:01:13,320 All right. 16 00:01:13,340 --> 00:01:14,480 So this is the case. 17 00:01:14,960 --> 00:01:15,720 So let's see. 18 00:01:15,740 --> 00:01:17,990 Let's see here and the sketch on the right. 19 00:01:18,680 --> 00:01:26,270 First we initialize the module, we run the code and we tackle the loop. 20 00:01:26,570 --> 00:01:35,930 And right after we extract the value out of the list in our case and we compare it, if it's bigger 21 00:01:35,930 --> 00:01:39,910 than five right after we were seeing and if statement. 22 00:01:40,370 --> 00:01:40,740 All right. 23 00:01:40,760 --> 00:01:47,150 And inside the statement, there are two options, whether the output of the if statement is either 24 00:01:47,420 --> 00:01:49,530 boolean true or boolean false. 25 00:01:49,850 --> 00:01:55,700 So we want some requirement to be fulfilled in this case, X bigger than five. 26 00:01:56,090 --> 00:02:02,150 And we can see here in the sketch that we have an error that goes to the left. 27 00:02:02,950 --> 00:02:05,510 For example, we have the first cell equals to one. 28 00:02:05,750 --> 00:02:08,360 So obviously it's not bigger than five. 29 00:02:08,360 --> 00:02:09,260 So it's a false. 30 00:02:09,620 --> 00:02:10,880 What are we doing next? 31 00:02:10,910 --> 00:02:18,200 We continue to next iteration, meaning we're taking the next cell out of the list and running again 32 00:02:18,200 --> 00:02:21,550 the loop in second iteration there. 33 00:02:21,680 --> 00:02:27,050 Obviously we encountered that statement once once again. 34 00:02:27,440 --> 00:02:34,790 And we're doing this again and again and again until we will see that our cell is actually true. 35 00:02:35,210 --> 00:02:37,760 Our output of the if condition is true. 36 00:02:38,390 --> 00:02:41,680 For example, we have the number ten, which is bigger than five. 37 00:02:41,690 --> 00:02:42,460 So it's true. 38 00:02:42,650 --> 00:02:47,600 And then we want the code inside if a block will run. 39 00:02:47,900 --> 00:02:48,350 All right. 40 00:02:48,360 --> 00:02:51,110 So many we don't want any more iterations. 41 00:02:51,110 --> 00:02:53,360 We found the item that we're looking for. 42 00:02:53,600 --> 00:02:57,530 We're looking for the first item, which is bigger than five. 43 00:02:57,770 --> 00:03:00,140 And then we use the action key break. 44 00:03:00,830 --> 00:03:05,290 And that key, that action key basically tells the loop, stop running. 45 00:03:05,300 --> 00:03:06,800 I found what I want. 46 00:03:07,730 --> 00:03:08,230 All right. 47 00:03:08,810 --> 00:03:10,550 So from here, let's see. 48 00:03:10,600 --> 00:03:16,310 Many you see here, many examples that we're going to learn in this lecture and by these examples and 49 00:03:16,310 --> 00:03:22,940 by this sketch together, I think we're going to get the hang of for loops combined with if statements 50 00:03:22,940 --> 00:03:25,910 are also actually what is the break. 51 00:03:26,420 --> 00:03:26,830 All right. 52 00:03:26,960 --> 00:03:33,230 By the way, just a quick reminder, this lecture and basically all other lectures as well, have an 53 00:03:33,230 --> 00:03:40,200 attachment file, that PDF file that you can download and we view after you watch the video. 54 00:03:40,880 --> 00:03:41,260 All right. 55 00:03:41,270 --> 00:03:47,340 So first of all, let's see how loop is applied on a collection right on the list. 56 00:03:47,750 --> 00:03:54,680 So once using a loop upon list or any other type of collection, each iteration would scan one cell 57 00:03:54,980 --> 00:03:56,140 in the collection. 58 00:03:56,150 --> 00:03:59,050 We already seen this in the previous lecture as well. 59 00:03:59,480 --> 00:04:06,310 So and if conditions statement will be used for some actions will be used for some actions to happen. 60 00:04:07,010 --> 00:04:12,380 So in example, a right here we can see a list of fruits. 61 00:04:12,380 --> 00:04:14,650 We have banana, orange and cherry. 62 00:04:15,020 --> 00:04:21,490 And right after we are seeing a for loop, we're seeing here for fruit item. 63 00:04:21,920 --> 00:04:28,580 And if you recall from previous lecture, we discussed that whatever comes after the four is a single 64 00:04:28,610 --> 00:04:35,210 item out of the whole package and the whole package comes after in. 65 00:04:35,720 --> 00:04:38,930 So fruits is the list. 66 00:04:39,200 --> 00:04:40,670 It has to be the same name. 67 00:04:41,420 --> 00:04:45,710 And what comes after the four is the single item. 68 00:04:45,710 --> 00:04:47,600 You can name it anything you want. 69 00:04:47,900 --> 00:04:49,790 You can choose whatever name you want. 70 00:04:50,090 --> 00:04:57,620 And in our case, each each of these items will be one cell out of the list. 71 00:04:57,950 --> 00:05:08,180 And we want just to print out each iteration, one item, one cell item value once we go again and again. 72 00:05:08,450 --> 00:05:11,900 And the output obviously going to be banana or and. 73 00:05:12,530 --> 00:05:18,860 We want to print all of the values in example A all right, and here an explanation. 74 00:05:18,860 --> 00:05:21,970 One last item will be printed on each iteration. 75 00:05:21,980 --> 00:05:28,440 So each time we go in the circle again and again and again, each time one item will be printed. 76 00:05:28,910 --> 00:05:29,360 All right. 77 00:05:29,360 --> 00:05:34,940 Until we'll finish the whole list and then we're going to go and continue the flow of code. 78 00:05:35,780 --> 00:05:41,850 So from here, let's see example be and we'll see something a bit different. 79 00:05:42,380 --> 00:05:44,900 So right here, we have also the same list. 80 00:05:45,200 --> 00:05:47,930 All of these examples have, by the way, they have the same list. 81 00:05:48,320 --> 00:05:51,910 And here we also have four fruit like them in fruits. 82 00:05:52,220 --> 00:05:58,940 And here we want to find something we we inserted here and each statement. 83 00:05:59,150 --> 00:06:05,740 And we think here, if fruit item equals orange, then fruit print the fruit item. 84 00:06:06,110 --> 00:06:12,500 And basically what would happen here is that it's going to run on all of these cells. 85 00:06:12,500 --> 00:06:15,620 And once it gets to orange, it will print it out. 86 00:06:15,620 --> 00:06:18,680 As you can see right here, it will print the orange. 87 00:06:19,430 --> 00:06:24,800 But once we find orange, it's not going to stop again. 88 00:06:25,040 --> 00:06:27,770 The loop is not going to stop running. 89 00:06:27,770 --> 00:06:32,630 It will run all all over the cells until we finish the last cell. 90 00:06:32,900 --> 00:06:40,520 And only after that it will exit the for loop because we haven't set any condition to exit the loop 91 00:06:40,520 --> 00:06:42,720 once we find what we want. 92 00:06:43,280 --> 00:06:46,450 So the output will be orange, this cell. 93 00:06:46,820 --> 00:06:53,750 But after we pass this cell, the the for loop also will scan this cell. 94 00:06:54,120 --> 00:06:54,720 All right. 95 00:06:54,740 --> 00:07:00,740 And if there were more, much more many cells after it, it would scan each and every one of them. 96 00:07:01,490 --> 00:07:10,360 So only once the loop will get to the to the item orange, it will print it after orange item. 97 00:07:10,370 --> 00:07:14,330 The loop will keep running until the last item without printing anything. 98 00:07:15,460 --> 00:07:21,980 And from here, we'll go straight ahead, for example, see and here we will use the break action, 99 00:07:22,520 --> 00:07:25,460 which should be with a small letter like this. 100 00:07:26,090 --> 00:07:29,720 And also this part is the same as we have seen. 101 00:07:30,290 --> 00:07:33,260 And also right here, it's also the same. 102 00:07:33,560 --> 00:07:35,300 All of these four lines are the same. 103 00:07:35,480 --> 00:07:38,530 But here we have an additional action key. 104 00:07:38,540 --> 00:07:40,130 We have the break word here. 105 00:07:40,460 --> 00:07:46,370 And basically what's going to happen, the output will be the same because we only have one orange here. 106 00:07:47,160 --> 00:07:47,720 All right. 107 00:07:48,080 --> 00:07:56,780 But once we find once we find the orange, the loop will not continue to this cell to the cherry. 108 00:07:56,780 --> 00:07:57,920 It will not continue. 109 00:07:57,920 --> 00:08:05,630 It will exit the loop completely and go further with the call with whatever comes after this for loop. 110 00:08:06,640 --> 00:08:13,750 All right, so only once the loop will get to the item orange, it will print, it will print it. 111 00:08:14,020 --> 00:08:19,480 Once Orange will be printed, the loop will stop running because we use the break word. 112 00:08:19,990 --> 00:08:21,910 All right, Prezi. 113 00:08:21,910 --> 00:08:24,190 So the break word basically tells the loop. 114 00:08:24,460 --> 00:08:24,950 All right. 115 00:08:24,970 --> 00:08:26,400 You found what you want. 116 00:08:26,680 --> 00:08:27,310 Stop. 117 00:08:27,910 --> 00:08:37,240 And that happens because the break it appears under the if in Hiroki, it appears under the if so whatever 118 00:08:37,240 --> 00:08:41,890 runs after the if it goes by the order from top to bottom. 119 00:08:41,890 --> 00:08:45,460 So we print the orange and then we break the loop. 120 00:08:46,410 --> 00:08:46,920 All right. 121 00:08:48,380 --> 00:08:55,430 Let's start here, the typical example here, we have seen an example upon a collection, but now let's 122 00:08:55,430 --> 00:08:59,330 see what happens if we do the same thing upon a string, upon a variable. 123 00:09:00,260 --> 00:09:00,680 All right. 124 00:09:00,980 --> 00:09:04,790 So we have examples here and we have fruits. 125 00:09:05,750 --> 00:09:08,750 And inside fruits, we have only one item, a banana. 126 00:09:09,590 --> 00:09:16,080 All right, and in this case, sorry, in this case, you will see here also in fruits. 127 00:09:17,240 --> 00:09:19,130 This is the whole package. 128 00:09:19,130 --> 00:09:19,600 Right. 129 00:09:19,820 --> 00:09:26,590 And whatever comes after the four in our case, it's fruit character is a single item. 130 00:09:26,780 --> 00:09:35,600 So if our whole package is a string, meaning a bunch of letters, so a single item would be one letter 131 00:09:35,600 --> 00:09:41,450 because the whole is a word and single item out of the word is just the letter. 132 00:09:42,140 --> 00:09:43,510 So you can see what happens here. 133 00:09:43,760 --> 00:09:45,680 It's like a dynamic structure. 134 00:09:45,830 --> 00:09:52,730 If you have many items, as you can see here, each item is once a one word. 135 00:09:53,960 --> 00:10:01,880 But in this case, we have only one word here, so we will see here, we will see here that each each 136 00:10:01,880 --> 00:10:07,160 item, whatever comes after four is a single character. 137 00:10:07,190 --> 00:10:07,570 All right. 138 00:10:07,580 --> 00:10:10,120 Let me just correct you a small mistake. 139 00:10:10,340 --> 00:10:16,850 I didn't mean it to be inside a list, so I'm just going to remove the square brackets because there's 140 00:10:16,850 --> 00:10:19,460 no point for this example, if it's inside a list. 141 00:10:19,940 --> 00:10:22,310 So we have just a simple string variable here. 142 00:10:22,550 --> 00:10:22,910 All right. 143 00:10:24,290 --> 00:10:31,670 So from here, we just going to go over the call again, so for fruit, character and fruits, and we 144 00:10:31,670 --> 00:10:34,750 want to print each character one by one. 145 00:10:34,790 --> 00:10:41,940 So if we print each each of these letters one by one, we going to end up with banana eventually. 146 00:10:42,290 --> 00:10:47,050 So each letter fruit character will be printed on each iteration of the loop. 147 00:10:47,060 --> 00:10:55,790 So the first iteration will print be the second iteration will print a then in the iteration after N 148 00:10:55,940 --> 00:11:02,840 and then A then and and then A until we'll get to the last item here, which is a and then the code 149 00:11:02,840 --> 00:11:04,250 will exit the loop. 150 00:11:05,300 --> 00:11:06,180 Pretty simple. 151 00:11:06,230 --> 00:11:06,640 All right. 152 00:11:06,980 --> 00:11:14,810 So in this case we have one word with which is the whole and a single item out of the hole is obviously 153 00:11:14,810 --> 00:11:15,700 one character. 154 00:11:16,040 --> 00:11:24,590 So each each iteration in the gold will extract one letter, one character out of this string variable, 155 00:11:25,550 --> 00:11:26,300 an example. 156 00:11:26,300 --> 00:11:28,730 B, we can see something a bit different. 157 00:11:29,120 --> 00:11:35,030 So here we are going to use an if statement, same as we did on collections, a list in the example 158 00:11:35,030 --> 00:11:41,390 right here in the examples right here in B, and we're going to use the if statement right here and 159 00:11:41,390 --> 00:11:48,180 we're going to say here, if fruit character equals and then print the fruit character. 160 00:11:48,500 --> 00:11:48,970 All right. 161 00:11:48,980 --> 00:11:56,810 So we're going to first iteration go B would not be printed, then A also would not be printed. 162 00:11:57,110 --> 00:12:02,690 And then we're going to get to N and and is what we're looking for inside the IFES, obviously, and 163 00:12:02,690 --> 00:12:03,740 will be printed. 164 00:12:04,380 --> 00:12:12,350 But after we print this and the for loop will continue running because we haven't said it, we haven't 165 00:12:12,350 --> 00:12:13,610 said to it to quit. 166 00:12:13,610 --> 00:12:20,270 So it's going to keep running to this cell, to this it's not a cell to the next character, a will 167 00:12:20,270 --> 00:12:22,430 not printed then out again. 168 00:12:22,430 --> 00:12:29,510 It's going to get to and here this and it will be also printed and then it's going to go to the last 169 00:12:29,510 --> 00:12:32,750 character A and only after will exit the loop. 170 00:12:32,930 --> 00:12:40,280 So eventually the output will be and because we're looking for an and every time will encounter and 171 00:12:40,280 --> 00:12:42,230 and here it will be printed out. 172 00:12:43,070 --> 00:12:43,460 All right. 173 00:12:43,470 --> 00:12:47,420 So in total, two end characters will be printed. 174 00:12:48,840 --> 00:12:54,990 And in the last example, we'll see exactly what we have seen right here, but we're going to use a 175 00:12:54,990 --> 00:12:59,900 break action as well to stop the loop whenever we want to do it right. 176 00:12:59,970 --> 00:13:01,220 We're under control here. 177 00:13:01,770 --> 00:13:07,950 So, again, we're going to look for the and character and we're going to print it. 178 00:13:08,280 --> 00:13:15,600 But this time, if fruit character equals and we're going to print the NT and right after we're going 179 00:13:15,600 --> 00:13:17,310 to break the loop. 180 00:13:17,640 --> 00:13:20,040 So it goes first iteration here. 181 00:13:20,640 --> 00:13:22,020 Second iteration. 182 00:13:23,480 --> 00:13:30,740 Third iteration, and once we encounter the NT character, we're going to print it and break the loop. 183 00:13:31,610 --> 00:13:37,510 OK, so in other words, all of these items right here would not be scanned at all. 184 00:13:37,820 --> 00:13:42,890 The code will exit the full loop once we encountered the first and. 185 00:13:43,910 --> 00:13:44,530 All right. 186 00:13:44,540 --> 00:13:53,840 So right here, the output is then, obviously, and only only once the loop will get to the first string 187 00:13:53,840 --> 00:13:59,940 and it will print it after the first and variable, the loop will stop running. 188 00:13:59,960 --> 00:14:00,400 All right. 189 00:14:00,710 --> 00:14:09,840 So this is the example once we apply the for loop upon a variable and we also use the break. 190 00:14:10,250 --> 00:14:19,340 So summing up what we have started right here in this lecture, we have studied how how can we use these 191 00:14:19,340 --> 00:14:22,310 three items, these three topics all together? 192 00:14:22,310 --> 00:14:25,700 And you can see that each of them has its own features. 193 00:14:25,860 --> 00:14:33,200 And once combined, its like it gives us a lot of power of filtering cells or filtering letters however 194 00:14:33,200 --> 00:14:42,530 we want, and basically gives us a lot of power once we build project development projects because it's 195 00:14:42,530 --> 00:14:46,220 very useful to filter items or filter information that way. 196 00:14:46,500 --> 00:14:53,270 And I think it's very, very useful in lots of various positions in the industry. 197 00:14:53,540 --> 00:14:58,850 Whether you're a web developer or whether you are an information developer, it doesn't matter front 198 00:14:58,850 --> 00:14:59,690 and back end. 199 00:15:00,020 --> 00:15:05,110 And basically it's very, very useful and it's really important to know this. 200 00:15:05,480 --> 00:15:05,900 All right. 201 00:15:05,910 --> 00:15:10,070 So this will be the conclusion for this version, for this lecture. 202 00:15:10,070 --> 00:15:11,640 And I will see you in the next one. 203 00:15:11,660 --> 00:15:12,170 Thank you.