1 00:00:00,630 --> 00:00:08,400 So let's look at how we control flow in Swift now, flow is one of those kind of nebulous terms that 2 00:00:08,400 --> 00:00:15,990 describes how your application flows as you go from, say, data input to displaying data on the front 3 00:00:15,990 --> 00:00:16,870 end to the user. 4 00:00:17,100 --> 00:00:18,830 That's just one example of flow. 5 00:00:19,200 --> 00:00:25,410 But the concepts we're going to look at are things that allow us to partition that flow, to send it 6 00:00:25,410 --> 00:00:31,450 down one route or another route, depending on what kind of variables we impose on the flow. 7 00:00:32,160 --> 00:00:36,170 So the very first thing we're going to look at is the four in loop. 8 00:00:36,660 --> 00:00:42,180 So previously we've looked at arrays a couple of lessons ago and we would define an array as, let's 9 00:00:42,180 --> 00:00:44,090 say, an array of integers. 10 00:00:44,090 --> 00:00:45,300 So whatever it is. 11 00:00:47,010 --> 00:00:53,730 We have that now, you'll recall that one of the homeworks I set you was to add everything in an array 12 00:00:53,730 --> 00:00:59,170 and in that array there were only three numbers, but you had to type out the following. 13 00:00:59,200 --> 00:01:03,550 You had to go array element, not waps element. 14 00:01:03,570 --> 00:01:05,790 Element, not plus. 15 00:01:06,970 --> 00:01:13,240 Array, element one, etc, etc., to get your final answer, and I said, well, there's two major 16 00:01:13,240 --> 00:01:13,990 problems with that. 17 00:01:14,320 --> 00:01:19,090 The first problem, of course, is that your statements are going to get longer and longer, the larger 18 00:01:19,090 --> 00:01:21,460 the array, and you don't want to be typing that out manually. 19 00:01:21,880 --> 00:01:25,570 Secondly, arrays are sometimes of an unknown length. 20 00:01:25,570 --> 00:01:28,980 In fact, most of the time they are of an unknown length. 21 00:01:29,350 --> 00:01:36,880 If I get some return values from a server, for example, I might not know how many music tracks I need 22 00:01:36,880 --> 00:01:38,010 to display to the user. 23 00:01:38,320 --> 00:01:40,500 And so it's impossible to code in that way. 24 00:01:40,510 --> 00:01:46,010 I just showed you the solution is for the four in loop, right? 25 00:01:46,030 --> 00:01:48,460 So we go for a in array. 26 00:01:50,160 --> 00:01:56,190 What this does is go through the array iteratively starting at Element zero and working its way up one, 27 00:01:56,190 --> 00:01:59,940 two, three until it hits the end of the array. 28 00:02:01,350 --> 00:02:04,140 In that case, we can print a. 29 00:02:05,670 --> 00:02:12,600 So let's run that every time we go round and round, this loop is going to print a and every time we 30 00:02:12,600 --> 00:02:17,900 go round the loop, it moves from six to seven to three to seventy seven. 31 00:02:18,060 --> 00:02:21,540 And you can see all those results down here in the console. 32 00:02:22,530 --> 00:02:27,350 So that's the four in very, very, very useful, right? 33 00:02:27,420 --> 00:02:28,490 So what else we got here? 34 00:02:28,870 --> 00:02:30,360 Uh, what's this? 35 00:02:31,200 --> 00:02:32,190 And he paced this in. 36 00:02:35,930 --> 00:02:40,670 OK, so here we have an array with some key values. 37 00:02:41,390 --> 00:02:47,690 OK, so we have a spider which has eight legs and ant which is six and a cat which has four. 38 00:02:48,350 --> 00:02:52,620 Now, we can separate out each of these things in the four in a row. 39 00:02:52,910 --> 00:02:56,620 If I drop this in here for animal name and leg count. 40 00:02:56,630 --> 00:03:01,350 So we're dealing with tuples here in a way in a number of legs. 41 00:03:01,610 --> 00:03:03,940 So what it does is it goes its a number of legs. 42 00:03:04,280 --> 00:03:11,960 It looks at the first element, which is this, it assigns spider to animal name and the number to leg 43 00:03:11,960 --> 00:03:14,630 count and these two items here. 44 00:03:15,600 --> 00:03:22,320 We can set to whatever we like, whatever we need, we just giving names to the variables so that when 45 00:03:22,320 --> 00:03:26,200 we're working inside the for loop, we know what we're referring to. 46 00:03:26,400 --> 00:03:35,880 So if I have print animal name s have X legs, then when I fill in these variables, I know what I'm 47 00:03:35,880 --> 00:03:36,550 talking about. 48 00:03:37,290 --> 00:03:42,210 The second thing you'll notice about this little statement is I've gone away from saying animal name 49 00:03:42,210 --> 00:03:44,800 plus open quote, space, et cetera. 50 00:03:45,060 --> 00:03:52,290 Instead, we're using this backslash character, which allows us to grab a variable in brackets and 51 00:03:52,290 --> 00:03:58,150 print that directly in the string so we don't have to had all the convoluted pluses anymore. 52 00:03:59,010 --> 00:04:00,150 So if I hit play on that. 53 00:04:01,080 --> 00:04:05,970 Ants have six legs, spiders have eight and cats have four legs. 54 00:04:06,840 --> 00:04:08,580 OK, so that's simple enough. 55 00:04:08,590 --> 00:04:13,200 That's how you name variables in the for loop and how we interpolate our strings. 56 00:04:13,540 --> 00:04:14,270 What's next? 57 00:04:15,380 --> 00:04:20,410 Now, this is the for loop without in, OK? 58 00:04:21,450 --> 00:04:29,940 Well, actually, it is within my bed for index, so what's the index index is just a name we give to 59 00:04:29,940 --> 00:04:36,360 a variable and then we say to the loop, I want you to go from number one to number five. 60 00:04:37,330 --> 00:04:44,110 And what the fallout will do is automatically fill in index equals one, index equals two, etc. until 61 00:04:44,110 --> 00:04:44,920 it gets to five. 62 00:04:45,220 --> 00:04:49,870 Once it's reached five, it'll stop the loop and then carry on to the next line of code. 63 00:04:50,680 --> 00:04:57,370 So if we played that, we're going to get one times five is five times five is ten, etc. until we get 64 00:04:57,370 --> 00:05:00,340 five times five and then the loop ends. 65 00:05:01,030 --> 00:05:03,700 So there's something important to note here, about four loops. 66 00:05:04,090 --> 00:05:07,870 They have an inherent ending, the ending of the array. 67 00:05:07,870 --> 00:05:09,490 One is the length of the array. 68 00:05:09,970 --> 00:05:13,180 The ending of the number of legs is the number of elements in that array. 69 00:05:13,690 --> 00:05:18,550 And the ending in this four index, one to five is the number five. 70 00:05:18,550 --> 00:05:20,810 Once it hits five loop has finished. 71 00:05:21,130 --> 00:05:24,730 So these are pretty safe loops as they go. 72 00:05:24,730 --> 00:05:29,860 And as we go on through this course, I'm going to show you what unsafe loops look like, including 73 00:05:29,860 --> 00:05:34,570 one that [REMOVED] me in this code as I was preparing it. 74 00:05:34,750 --> 00:05:38,800 And I could believe such a silly error got me who's been programming for donkey's years. 75 00:05:39,340 --> 00:05:39,910 But it does. 76 00:05:39,910 --> 00:05:40,930 It happens to all of us. 77 00:05:41,940 --> 00:05:45,100 OK, what have we got next up? 78 00:05:45,280 --> 00:05:51,460 What happens if we don't actually want to print index? 79 00:05:51,790 --> 00:05:56,290 So let's say we want to save a little bit of memory and we don't want to store that value in index. 80 00:05:56,620 --> 00:06:02,560 While we can use this underscore notation, which is a general notation across SWIFT, it's not just 81 00:06:02,560 --> 00:06:03,250 for for loops. 82 00:06:04,060 --> 00:06:08,170 And what we'll do is have no variable that gets carried through to the loop. 83 00:06:08,500 --> 00:06:12,370 So that makes it probably a touch quicker, in my opinion. 84 00:06:12,370 --> 00:06:14,030 It's not going to make it that much quicker. 85 00:06:14,050 --> 00:06:20,140 There are lots of other areas that you need to address as a developer that will speed up your app and 86 00:06:20,140 --> 00:06:22,410 things like this, I don't think make that much difference. 87 00:06:22,720 --> 00:06:28,150 Now, again, if you're making a financial app and you are trading on the markets within nanoseconds, 88 00:06:28,870 --> 00:06:31,210 this is something you have to do, something you need to be aware of. 89 00:06:31,420 --> 00:06:37,330 Although you probably wouldn't be using Swift, you'd be using something low level like C to do that 90 00:06:37,330 --> 00:06:39,660 for just for the utter speed that you can get out of it. 91 00:06:40,640 --> 00:06:43,950 But for us regular developers, no problems whatsoever. 92 00:06:44,480 --> 00:06:50,240 So this fall, it goes one to five in the same way as the previous one without carrying a variable through 93 00:06:50,240 --> 00:06:52,160 to the loop itself. 94 00:06:52,790 --> 00:06:55,150 OK, so that's all about the fallout. 95 00:06:55,490 --> 00:06:56,740 I've got a bit of homework here. 96 00:07:00,160 --> 00:07:02,890 Make a set of four string variables. 97 00:07:04,170 --> 00:07:08,540 Make three unique and one the same as another and print them out. 98 00:07:09,720 --> 00:07:12,210 OK, now notice. 99 00:07:16,800 --> 00:07:22,050 This magic word set, I know it's not part of this lesson, but it was part of a previous lesson, wasn't 100 00:07:22,050 --> 00:07:22,180 it? 101 00:07:22,710 --> 00:07:24,210 A set is not an array. 102 00:07:24,840 --> 00:07:25,010 Hmm. 103 00:07:25,890 --> 00:07:28,220 So if you don't remember what that means, go back and look it up. 104 00:07:28,740 --> 00:07:36,990 Make for variables of which three are unique and one is the same as one of those three and then print 105 00:07:36,990 --> 00:07:39,280 all of them out using a for loop. 106 00:07:40,200 --> 00:07:40,710 Good luck.