1 00:00:00,450 --> 00:00:02,460 ‫Instructor: Hi. Within this lecture, 2 00:00:02,460 --> 00:00:05,430 ‫we are going to start learning about loops. 3 00:00:05,430 --> 00:00:07,830 ‫So loops are great tools 4 00:00:07,830 --> 00:00:11,880 ‫to implement some operation repetitively 5 00:00:11,880 --> 00:00:14,970 ‫as long as a condition holds. 6 00:00:14,970 --> 00:00:17,970 ‫So we're gonna see what that means 7 00:00:17,970 --> 00:00:20,370 ‫in real examples in a minute. 8 00:00:20,370 --> 00:00:24,060 ‫So let me create a new playground over here 9 00:00:24,060 --> 00:00:26,070 ‫and name it Loops. 10 00:00:26,070 --> 00:00:28,290 ‫So I'm gonna go over to my desktop 11 00:00:28,290 --> 00:00:32,640 ‫and save this into my iOS complete folder. 12 00:00:32,640 --> 00:00:34,623 ‫And now we are ready. 13 00:00:35,760 --> 00:00:38,250 ‫So we're gonna learn about this loop. 14 00:00:38,250 --> 00:00:40,890 ‫So we are gonna perform an operation 15 00:00:40,890 --> 00:00:42,600 ‫in a repetitive way 16 00:00:42,600 --> 00:00:46,080 ‫as long as a condition holds, okay? 17 00:00:46,080 --> 00:00:48,480 ‫And the first loop that we are gonna learn 18 00:00:48,480 --> 00:00:51,120 ‫is called While Loop. 19 00:00:51,120 --> 00:00:55,590 ‫So we are gonna say while this condition holds, 20 00:00:55,590 --> 00:00:57,660 ‫do the following. 21 00:00:57,660 --> 00:00:59,460 ‫And in order to do that, 22 00:00:59,460 --> 00:01:03,280 ‫we have to learn about some mathematical notations 23 00:01:05,430 --> 00:01:07,650 ‫in Swift actually. 24 00:01:07,650 --> 00:01:11,070 ‫So let me say that I have a number 25 00:01:11,070 --> 00:01:13,890 ‫and this number is one. 26 00:01:13,890 --> 00:01:17,520 ‫So I can add some new thing to this number 27 00:01:17,520 --> 00:01:22,350 ‫by saying that my number is now my number plus one. 28 00:01:22,350 --> 00:01:25,500 ‫So if I call my number right now, 29 00:01:25,500 --> 00:01:27,870 ‫what will happen to my number? 30 00:01:27,870 --> 00:01:29,730 ‫It will be two, right? 31 00:01:29,730 --> 00:01:31,710 ‫Because it will add one to one 32 00:01:31,710 --> 00:01:33,513 ‫and it will find two. 33 00:01:34,500 --> 00:01:36,510 ‫So this is easy. 34 00:01:36,510 --> 00:01:39,870 ‫And we have another way of doing this. 35 00:01:39,870 --> 00:01:42,780 ‫And it is even easier, 36 00:01:42,780 --> 00:01:44,850 ‫so you can do it like this: 37 00:01:44,850 --> 00:01:47,250 ‫plus equals to one. 38 00:01:47,250 --> 00:01:50,940 ‫And these two lines are exactly the same. 39 00:01:50,940 --> 00:01:55,770 ‫So it means that my number equals my number plus one. 40 00:01:55,770 --> 00:01:58,140 ‫So if I do this one more time 41 00:01:58,140 --> 00:01:59,880 ‫and if I call my number, 42 00:01:59,880 --> 00:02:02,280 ‫what will happen to my number? 43 00:02:02,280 --> 00:02:05,430 ‫So it'll be three, right? 44 00:02:05,430 --> 00:02:08,970 ‫Because it started as a one and we added one, 45 00:02:08,970 --> 00:02:10,980 ‫so we came up with two, 46 00:02:10,980 --> 00:02:14,043 ‫and now we added one and we came up with three. 47 00:02:14,910 --> 00:02:16,920 ‫So why are we using this? 48 00:02:16,920 --> 00:02:19,650 ‫Because I'm going to use this technique 49 00:02:19,650 --> 00:02:20,880 ‫in the loops as well. 50 00:02:20,880 --> 00:02:23,580 ‫So right now you know how to add one 51 00:02:23,580 --> 00:02:28,020 ‫or how to do this plus equal thing 52 00:02:28,020 --> 00:02:30,120 ‫and it works for minus as well. 53 00:02:30,120 --> 00:02:33,240 ‫So you can say minus equals to one 54 00:02:33,240 --> 00:02:37,500 ‫and it will subtract one from my number. 55 00:02:37,500 --> 00:02:39,210 ‫And at the end of this lecture 56 00:02:39,210 --> 00:02:42,990 ‫we are going to learn how to compare some numbers. 57 00:02:42,990 --> 00:02:46,440 ‫For example, how to compare my number with one, 58 00:02:46,440 --> 00:02:49,620 ‫how to see if my number is less than one, 59 00:02:49,620 --> 00:02:51,090 ‫greater than one. 60 00:02:51,090 --> 00:02:52,920 ‫And we will use this a lot 61 00:02:52,920 --> 00:02:56,010 ‫during when we are building apps, okay? 62 00:02:56,010 --> 00:02:59,940 ‫So let's suppose that I have a number, zero. 63 00:02:59,940 --> 00:03:04,830 ‫So this is another variable and its value is zero. 64 00:03:04,830 --> 00:03:06,840 ‫And we are going to use this number 65 00:03:06,840 --> 00:03:09,300 ‫inside of our while loop. 66 00:03:09,300 --> 00:03:11,220 ‫So basically we are going to use 67 00:03:11,220 --> 00:03:14,130 ‫the key word while, okay? 68 00:03:14,130 --> 00:03:18,510 ‫Call while, and after the while statement, 69 00:03:18,510 --> 00:03:22,800 ‫you have to give your condition. 70 00:03:22,800 --> 00:03:25,380 ‫So we are literally gonna say, 71 00:03:25,380 --> 00:03:30,380 ‫while this condition holds, do the following, okay? 72 00:03:30,420 --> 00:03:34,770 ‫And maybe you realize that while is shown in pink, 73 00:03:34,770 --> 00:03:37,530 ‫and so var and so import. 74 00:03:37,530 --> 00:03:40,650 ‫So we are importing a library here. 75 00:03:40,650 --> 00:03:43,950 ‫So these keywords are shown in a different color. 76 00:03:43,950 --> 00:03:47,460 ‫So these are predefined keywords 77 00:03:47,460 --> 00:03:50,550 ‫so that we can understand something will happen 78 00:03:50,550 --> 00:03:53,190 ‫when we use those keywords. 79 00:03:53,190 --> 00:03:57,600 ‫And again, while is one of them, okay? 80 00:03:57,600 --> 00:04:00,330 ‫And we didn't really talk about library. 81 00:04:00,330 --> 00:04:04,590 ‫So, what does it mean to import some UIKit? 82 00:04:04,590 --> 00:04:08,550 ‫So UIKit basically provides the basic functionality 83 00:04:08,550 --> 00:04:10,890 ‫so that we can use Swift code, 84 00:04:10,890 --> 00:04:12,840 ‫we can see the inputs and outputs, 85 00:04:12,840 --> 00:04:15,720 ‫we can reach the string class, 86 00:04:15,720 --> 00:04:19,230 ‫we can reach the integers and everything, okay? 87 00:04:19,230 --> 00:04:22,740 ‫We are going to import another libraries later on 88 00:04:22,740 --> 00:04:25,080 ‫during our course. 89 00:04:25,080 --> 00:04:27,720 ‫Don't worry about this right now. 90 00:04:27,720 --> 00:04:29,696 ‫So for example, I can do this, 91 00:04:29,696 --> 00:04:33,600 ‫while my number is less than 10. 92 00:04:33,600 --> 00:04:36,450 ‫So, that's what I was talking about. 93 00:04:36,450 --> 00:04:38,850 ‫We are gonna compare this number. 94 00:04:38,850 --> 00:04:41,040 ‫So what does this mean? 95 00:04:41,040 --> 00:04:45,090 ‫I say while number is less than 10, 96 00:04:45,090 --> 00:04:50,090 ‫I open a curly brace, and it closes it automatically. 97 00:04:50,190 --> 00:04:54,030 ‫So remember this creates a coding block for me, 98 00:04:54,030 --> 00:04:57,180 ‫and whatever I write here 99 00:04:57,180 --> 00:05:00,510 ‫will be only called inside of while loop. 100 00:05:00,510 --> 00:05:04,530 ‫So, will be only executed inside of while loop. 101 00:05:04,530 --> 00:05:09,530 ‫So whatever I write here is only going to be executed 102 00:05:10,230 --> 00:05:12,783 ‫while number is less than 10. 103 00:05:13,980 --> 00:05:16,470 ‫So for example, let me print the number 104 00:05:16,470 --> 00:05:19,740 ‫and let me add one to my number. 105 00:05:19,740 --> 00:05:21,930 ‫So, let's see what will happen. 106 00:05:21,930 --> 00:05:25,620 ‫Let me run this and you will see what I mean. 107 00:05:25,620 --> 00:05:28,470 ‫In the logs you will see zero, one, two, three, 108 00:05:28,470 --> 00:05:31,290 ‫four, five, six, seven, eight, nine. 109 00:05:31,290 --> 00:05:34,410 ‫So that's fantastic, how did it happen? 110 00:05:34,410 --> 00:05:38,160 ‫So as long as number was less than 10, 111 00:05:38,160 --> 00:05:39,900 ‫this went in a loop. 112 00:05:39,900 --> 00:05:44,640 ‫So it started with zero and it was less than 10, 113 00:05:44,640 --> 00:05:48,750 ‫so it printed out zero and it added one to zero 114 00:05:48,750 --> 00:05:51,480 ‫and it went back to the beginning. 115 00:05:51,480 --> 00:05:53,760 ‫And it controlled one more time, 116 00:05:53,760 --> 00:05:57,810 ‫one was less than 10, and it printed out one, 117 00:05:57,810 --> 00:06:00,330 ‫and it added one to the one. 118 00:06:00,330 --> 00:06:02,790 ‫It went up until nine. 119 00:06:02,790 --> 00:06:06,810 ‫So it printed out nine and it added one to the nine, 120 00:06:06,810 --> 00:06:10,410 ‫and it was 10 right now. 121 00:06:10,410 --> 00:06:13,260 ‫So 10 was not less than 10, 122 00:06:13,260 --> 00:06:17,790 ‫so our loop was ended, okay? 123 00:06:17,790 --> 00:06:21,840 ‫So as long as number was less than 10, 124 00:06:21,840 --> 00:06:23,820 ‫it got executed. 125 00:06:23,820 --> 00:06:27,510 ‫And once it was more than 10, 126 00:06:27,510 --> 00:06:30,990 ‫it stopped being executed. 127 00:06:30,990 --> 00:06:34,980 ‫So if I change this to less than and equal to, 128 00:06:34,980 --> 00:06:38,253 ‫it will also print out the 10 as you might see. 129 00:06:39,150 --> 00:06:43,320 ‫So you can change this number and you can play with it 130 00:06:43,320 --> 00:06:47,340 ‫and you will see that as long as this condition holds, 131 00:06:47,340 --> 00:06:50,070 ‫this will continue the writing, 132 00:06:50,070 --> 00:06:52,230 ‫this will continue printing. 133 00:06:52,230 --> 00:06:55,560 ‫And as you can see whatever I write inside 134 00:06:55,560 --> 00:06:58,443 ‫of this block, the coding block, 135 00:07:00,240 --> 00:07:03,843 ‫is being executed inside of this while loop. 136 00:07:05,190 --> 00:07:08,760 ‫So we are going to learn about if later on, 137 00:07:08,760 --> 00:07:12,360 ‫but let me create a character over here. 138 00:07:12,360 --> 00:07:15,723 ‫So I'm gonna call this var characterAlive true. 139 00:07:16,920 --> 00:07:19,470 ‫So I can check to see 140 00:07:19,470 --> 00:07:23,910 ‫if my characterAlive condition holds. 141 00:07:23,910 --> 00:07:26,580 ‫So I can just go over here and say, 142 00:07:26,580 --> 00:07:31,110 ‫while characterAlive is true, do the following, right? 143 00:07:31,110 --> 00:07:33,630 ‫So it doesn't have to be an integer, 144 00:07:33,630 --> 00:07:36,480 ‫it doesn't have to be less than, greater than. 145 00:07:36,480 --> 00:07:38,190 ‫So I can just say like this, 146 00:07:38,190 --> 00:07:41,730 ‫while characterAlive is true, 147 00:07:41,730 --> 00:07:43,173 ‫then do the following. 148 00:07:44,160 --> 00:07:46,350 ‫For example you can print out, 149 00:07:46,350 --> 00:07:50,070 ‫and why am I using two equations, 150 00:07:50,070 --> 00:07:53,190 ‫two equal signs rather than one? 151 00:07:53,190 --> 00:07:55,620 ‫If I do two equal signs, 152 00:07:55,620 --> 00:07:59,340 ‫it means that just check if they're equal or not. 153 00:07:59,340 --> 00:08:01,920 ‫If I do one equal sign, 154 00:08:01,920 --> 00:08:05,280 ‫it means that make it equal, okay? 155 00:08:05,280 --> 00:08:09,000 ‫So when I use two equal signs, 156 00:08:09,000 --> 00:08:10,920 ‫I'm checking for something 157 00:08:10,920 --> 00:08:13,383 ‫to see if they are true or not. 158 00:08:14,310 --> 00:08:17,310 ‫So in this case I'm checking to see 159 00:08:17,310 --> 00:08:21,483 ‫if the characterAlive is actually true. 160 00:08:22,380 --> 00:08:25,260 ‫So while characterAlive is true, 161 00:08:25,260 --> 00:08:27,453 ‫I'm going to print characterAlive. 162 00:08:28,470 --> 00:08:32,250 ‫And of course I'm going to say characterAlive false, 163 00:08:32,250 --> 00:08:36,840 ‫so this will get only just run one time 164 00:08:36,840 --> 00:08:38,550 ‫because it will change it to false 165 00:08:38,550 --> 00:08:41,160 ‫and it will stop doing this, 166 00:08:41,160 --> 00:08:43,500 ‫and it doesn't make sense, I know, 167 00:08:43,500 --> 00:08:47,400 ‫but if I don't do this, it will be an infinite loop. 168 00:08:47,400 --> 00:08:50,970 ‫So we will see an infinite number 169 00:08:50,970 --> 00:08:53,490 ‫of characterAlive printing over here 170 00:08:53,490 --> 00:08:57,510 ‫because it won't just break out of the loop, 171 00:08:57,510 --> 00:08:59,790 ‫it won't stop the loop. 172 00:08:59,790 --> 00:09:01,800 ‫So just for an example, 173 00:09:01,800 --> 00:09:04,650 ‫I did that so that you would see 174 00:09:04,650 --> 00:09:07,950 ‫you can use a Boolean, you can see an integer, 175 00:09:07,950 --> 00:09:11,370 ‫you can even use a string in here as well. 176 00:09:11,370 --> 00:09:15,000 ‫So while this condition holds, 177 00:09:15,000 --> 00:09:17,910 ‫and the condition doesn't even matter, 178 00:09:17,910 --> 00:09:21,300 ‫as long as it makes sense obviously. 179 00:09:21,300 --> 00:09:25,200 ‫So right now let me show you the other checkpoints 180 00:09:25,200 --> 00:09:28,230 ‫like we can do something like this, right? 181 00:09:28,230 --> 00:09:31,590 ‫So three is less than five. 182 00:09:31,590 --> 00:09:33,900 ‫So you can control to see 183 00:09:33,900 --> 00:09:35,760 ‫if three is less than five 184 00:09:35,760 --> 00:09:38,580 ‫and it'll give you a true Boolean. 185 00:09:38,580 --> 00:09:41,040 ‫So this is indeed less than five. 186 00:09:41,040 --> 00:09:43,140 ‫And you can do something like this: 187 00:09:43,140 --> 00:09:46,710 ‫five is less than three and it'll give you false. 188 00:09:46,710 --> 00:09:48,030 ‫So this is less than, 189 00:09:48,030 --> 00:09:51,600 ‫of course you can do a greater than, okay, 190 00:09:51,600 --> 00:09:53,820 ‫five is greater than three. 191 00:09:53,820 --> 00:09:57,180 ‫You can go for less than or equal to, 192 00:09:57,180 --> 00:09:59,940 ‫greater than and equal to as well. 193 00:09:59,940 --> 00:10:01,800 ‫And you can do something like this: 194 00:10:01,800 --> 00:10:04,770 ‫equal to, two equal signs, 195 00:10:04,770 --> 00:10:09,180 ‫so important here, greater than, equal to, 196 00:10:09,180 --> 00:10:13,020 ‫first, greater sign, later equal sign. 197 00:10:13,020 --> 00:10:17,100 ‫So less than and equal to, less sign, equal sign. 198 00:10:17,100 --> 00:10:22,013 ‫And if you're checking for being equal, two equal signs. 199 00:10:23,160 --> 00:10:25,170 ‫And actually we have something 200 00:10:25,170 --> 00:10:28,800 ‫called a not equal to as well. 201 00:10:28,800 --> 00:10:31,290 ‫So you can control it like this" 202 00:10:31,290 --> 00:10:35,220 ‫exclamation point and an equal sign. 203 00:10:35,220 --> 00:10:38,610 ‫So it means that this is not equal to, 204 00:10:38,610 --> 00:10:40,830 ‫and as you can see, we get true 205 00:10:40,830 --> 00:10:42,993 ‫as the output of this. 206 00:10:44,430 --> 00:10:46,080 ‫So this is kind of cool, right? 207 00:10:46,080 --> 00:10:49,380 ‫We can use this kind of mathematical operations 208 00:10:49,380 --> 00:10:53,340 ‫in our codes, and we're gonna use those a lot. 209 00:10:53,340 --> 00:10:57,870 ‫And even if we are not using those with integers, 210 00:10:57,870 --> 00:11:01,980 ‫we are using these with Booleans, 211 00:11:01,980 --> 00:11:04,050 ‫with strings and everything. 212 00:11:04,050 --> 00:11:07,080 ‫Later on we are going to go 213 00:11:07,080 --> 00:11:09,720 ‫a little bit deeper on this subject as well. 214 00:11:09,720 --> 00:11:11,880 ‫Right now I'm going to stop here 215 00:11:11,880 --> 00:11:13,140 ‫and within the next lecture 216 00:11:13,140 --> 00:11:14,430 ‫we are going to continue 217 00:11:14,430 --> 00:11:17,433 ‫with another loop called for Loop.