So let's look at how we control flow in Swift now, flow is one of those kind of nebulous terms that
describes how your application flows as you go from, say, data input to displaying data on the front
end to the user.
That's just one example of flow.
But the concepts we're going to look at are things that allow us to partition that flow, to send it
down one route or another route, depending on what kind of variables we impose on the flow.
So the very first thing we're going to look at is the four in loop.
So previously we've looked at arrays a couple of lessons ago and we would define an array as, let's
say, an array of integers.
So whatever it is.
We have that now, you'll recall that one of the homeworks I set you was to add everything in an array
and in that array there were only three numbers, but you had to type out the following.
You had to go array element, not waps element.
Element, not plus.
Array, element one, etc, etc., to get your final answer, and I said, well, there's two major
problems with that.
The first problem, of course, is that your statements are going to get longer and longer, the larger
the array, and you don't want to be typing that out manually.
Secondly, arrays are sometimes of an unknown length.
In fact, most of the time they are of an unknown length.
If I get some return values from a server, for example, I might not know how many music tracks I need
to display to the user.
And so it's impossible to code in that way.
I just showed you the solution is for the four in loop, right?
So we go for a in array.
What this does is go through the array iteratively starting at Element zero and working its way up one,
two, three until it hits the end of the array.
In that case, we can print a.
So let's run that every time we go round and round, this loop is going to print a and every time we
go round the loop, it moves from six to seven to three to seventy seven.
And you can see all those results down here in the console.
So that's the four in very, very, very useful, right?
So what else we got here?
Uh, what's this?
And he paced this in.
OK, so here we have an array with some key values.
OK, so we have a spider which has eight legs and ant which is six and a cat which has four.
Now, we can separate out each of these things in the four in a row.
If I drop this in here for animal name and leg count.
So we're dealing with tuples here in a way in a number of legs.
So what it does is it goes its a number of legs.
It looks at the first element, which is this, it assigns spider to animal name and the number to leg
count and these two items here.
We can set to whatever we like, whatever we need, we just giving names to the variables so that when
we're working inside the for loop, we know what we're referring to.
So if I have print animal name s have X legs, then when I fill in these variables, I know what I'm
talking about.
The second thing you'll notice about this little statement is I've gone away from saying animal name
plus open quote, space, et cetera.
Instead, we're using this backslash character, which allows us to grab a variable in brackets and
print that directly in the string so we don't have to had all the convoluted pluses anymore.
So if I hit play on that.
Ants have six legs, spiders have eight and cats have four legs.
OK, so that's simple enough.
That's how you name variables in the for loop and how we interpolate our strings.
What's next?
Now, this is the for loop without in, OK?
Well, actually, it is within my bed for index, so what's the index index is just a name we give to
a variable and then we say to the loop, I want you to go from number one to number five.
And what the fallout will do is automatically fill in index equals one, index equals two, etc. until
it gets to five.
Once it's reached five, it'll stop the loop and then carry on to the next line of code.
So if we played that, we're going to get one times five is five times five is ten, etc. until we get
five times five and then the loop ends.
So there's something important to note here, about four loops.
They have an inherent ending, the ending of the array.
One is the length of the array.
The ending of the number of legs is the number of elements in that array.
And the ending in this four index, one to five is the number five.
Once it hits five loop has finished.
So these are pretty safe loops as they go.
And as we go on through this course, I'm going to show you what unsafe loops look like, including
one that [REMOVED] me in this code as I was preparing it.
And I could believe such a silly error got me who's been programming for donkey's years.
But it does.
It happens to all of us.
OK, what have we got next up?
What happens if we don't actually want to print index?
So let's say we want to save a little bit of memory and we don't want to store that value in index.
While we can use this underscore notation, which is a general notation across SWIFT, it's not just
for for loops.
And what we'll do is have no variable that gets carried through to the loop.
So that makes it probably a touch quicker, in my opinion.
It's not going to make it that much quicker.
There are lots of other areas that you need to address as a developer that will speed up your app and
things like this, I don't think make that much difference.
Now, again, if you're making a financial app and you are trading on the markets within nanoseconds,
this is something you have to do, something you need to be aware of.
Although you probably wouldn't be using Swift, you'd be using something low level like C to do that
for just for the utter speed that you can get out of it.
But for us regular developers, no problems whatsoever.
So this fall, it goes one to five in the same way as the previous one without carrying a variable through
to the loop itself.
OK, so that's all about the fallout.
I've got a bit of homework here.
Make a set of four string variables.
Make three unique and one the same as another and print them out.
OK, now notice.
This magic word set, I know it's not part of this lesson, but it was part of a previous lesson, wasn't
it?
A set is not an array.
Hmm.
So if you don't remember what that means, go back and look it up.
Make for variables of which three are unique and one is the same as one of those three and then print
all of them out using a for loop.
Good luck.