0:13
What I wanna do in this lecture is I want to explain the syntax of Objective-C,
while loops, for loops and do while loops.
And the reason why is because this is really
one of the things that computers excel at.
Computers are really good at doing repetitive work.
Work that humans could do but could do very slowly.
But computers are very good at doing many things, over and over very quickly.
And one of the ways they can do that is through loops and loops syntax,
and you as a programmer teaches a computer what to do
in the context of this repetitive loop structure.
The loop is a way that you give instructions to the computer about how
long and what to do when the computer repeats something.
It's one of several different ways that you can teach a computer how to do
repetitive work, but it's a powerful way.
So I wanna look at those in this lecture.
1:08
So let's consider this code here.
We've looked previously at command line programs, and
this is another example of a command line program where we have a main function and
in this main function, we're gonna print out three different formatted text lines.
Good morning, Good afternoon and
Good evening, each of which ends by going to the next line.
And then finally, we'll return a 0.
So, let's imagine that this is work that we wanna do every day.
We wanna write Good morning, Good afternoon and Good evening out.
This is great for one day, but what if you want to print these greetings for 2 days.
What would you do?
1:41
Well, you probably know a little bit about text editing, that you could cut and
paste with an X code and you could just say, printf Good morning, Good afternoon,
Good evening, printf Good morning, Good afternoon, Good evening.
Hey, that's great for two days, but
what if you needed to print greetings for ten days?
Well, you could do Good morning, Good afternoon, Good evening, Good morning,
Good afternoon, Good evening, Good morning,
Good afternoon, Good evening, etcetera, etcetera for ten times.
2:04
That's good for ten times, but we know that computers do things for
millions of times and tens of millions of times and billions of times in some cases.
So, clearly this can get ridiculous, and there must be a better way to do
this repetitive kind of action over and over again.
Well there is, and that way is a loop.
So one way that we can do it is a kind of loop called the while loop.
And while loops use conditional expressions.
The same conditional expressions we saw in if else statements,
in order to instruct the computer how long to continue doing something.
So we're gonna repeat the same code as long as the conditional expression
is true.
2:43
So a basic while loop looks like this.
While a conditional expression is true, run this single line of code.
Now one thing we haven't talked very much about is the syntax of the curly braces.
What the curly braces do is they allow multiple lines of code to be run, but
if you leave them out most syntax,
like the while command, like the if else statement command,
if you leave the curly brackets out then you only run a single line of code.
The one following the while or the if statement or the else statement.
3:22
Another thing you could do is use the curly brackets and
to run multiple lines of code, as long as the conditional expression is true.
So in this case, what we'll see is we'll see that while a conditional expression
is true we have three comments which represent three lines of code that would
be run periodically, continuously, in a row,
until that conditional expression is true, over and over and over again.
So for example, if we took the code that we had before,
we introduced a variable called, counter and set it equal to zero and
we made a conditional expression to which said counter less than one,
we said while counter less than one printf Good morning, printf Good afternoon,
printf Good evening, and
at the end of that we'll say counter is assigned the value of counter plus one.
So the first time we go through this loop counter will be equal to zero and
so counter is less than one.
We'll print Good morning, Good afternoon, and Good evening.
We will say counter now equals counter plus one, so now it equals one.
We'll go back to the loop and start again and we'll ask, is counter less than one?
No, counter is equal to one.
And because this is not true anymore, we don't execute that code and we return.
So if we understand this correctly, as I've described it, Good morning,
Good afternoon, and Good evening will be written out one time.
But each of them will be written out one time.
And if you compile this using the command line project and
executed on the command line, you'll see output that looks a little bit like this.
Three lines printed.
One for each of the lines within the body of the while loop.
Okay, that sort of make sense, I think.
But what if we want to print out more than one tenth?
What if we wanted to print out two times?
While we can change that code that we had before, and instead make counter, rather
than having counter be less than one, we can say while counter is less than two.
So the first time we go to the loop, counter will be equal to zero.
5:13
Will print Good morning, Good evening, Good afternoon.
Good morning, Good afternoon, Good evening.
We'll set counter equal to counter plus one.
And the second time we'll ask is counter less than 2, it is calendar is one.
So we run it again, we say Good morning, Good afternoon, Good evening.
Now when we update counter, counter will be equal to 2, we'll ask,
is counter less than 2,?
No, counter equals 2, and so we will print out those lines no more,
having printed each of those line out twice before, and so
the output of this program if we run it on the command line, using the build and
then copying it into a directory, we'll see that those three lines get printed out
two times each in sequence, according to the body of that while statement.
5:53
And just like we looked at that ridiculous code in the beginning where we cut and
pasted that same code 10 times, well now we can write it as many times as we want
just by changing that conditional expression So now we say,
where counter is less than ten, we want to print those three lines out, and
we end up printing them out ten times.
One for when the counter is zero, once when it's one, two, three, four, five,
six, seven, eight, and
the last time we print it out is when the counter is equal to nine.
So zero to nine, that's ten different times.
6:30
The for loop is a second kind of looping structure and
it's very similar to the while loop.
In fact, it has a very common pattern that it is trying to help programmers.
I'll repeat very concisely.
And it looks a little bit like what we just saw in the while loop.
So if we look at these two examples, the while loop that we just looked at said
if the integer variable counter is less than ten,
print those lines out, and then eventually once we go through the loop we're done.
If we look at the for loop, initially we can see that this code,
which does the same thing, looks very similar.
Instead of while, we have the word for, and instead of just a conditional
expression we have these strange two semicolons at the beginning and the end.
7:16
Okay, so for starters if you ran this it would be exactly the same.
But for loop can do something else, and
I want to show you the pattern that it follows in order to create
kind of a convenience for a programmer who is trying to do a common looping pattern.
So this is what we had for code that matched our ten print out,
the while loop that printed out those lines ten times.
We have this initial code where we set a value of a variable into counter = 0,
this is initialization code,
it's code that we run at the beginning of a loop, it's a common pattern.
7:50
Then we have a condition that we evaluate each time to see if we
should continue going through the loop, and
the end of that loop we have a little bit of code which updates something, and so
that counter equals counter plus one is an update line that's executed typically
at the end of this body of code, within the loop structure.
So what the for enables us to do is to take those pieces that were separated out
in the while syntax, and to move them into one line that is separated by semicolons.
And so now the same code can be written as for
(int counter = 0 ; counter < 10 ; ),
at the end of each loop I wanna execute counter = counter +1.
And so, that long while loop is now compacted into four lines and
an extra curly brace of a for loop.
8:44
Generally, we can look at this pattern of what's happening in a for
loop as for open parentheses, close parentheses, and within
those parentheses are three expressions which are separated by two semicolons.
The first is a bit of an initialization code that's run at the the beginning
of the loop.
The second is a conditional expression that is evaluated
at the beginning at each loop.
And the last bit of that for line, at the end of the second semi-colon,
is code that's executed at the end of the loop body.
So we initialize one time, we evaluate the conditional expression, we run
the multiple lines code that are within our for loop, and then we run the code
that's at the end of our loop body and that's represented in our for line.
After we run the end of our loop body we go back and
we evaluate our conditional expression again.
If it's still true, we run the multiple lines of code again, we execute the end of
the loop body code again, we go back we check the conditional expression,
we run the lines of code in the body, we check the end of the loop body again and
again and again until the conditional expression is true,
at which point we leave the body discoding, continue on the initialization
was only run one time, the conditional expression is run at least one time, and
the end of the loop body is run zero or more times.
10:05
The do-while loop is a third kind of looping structure, and
it's very similar to the initial loop we saw which is the while loop except that
a normal while loop might not ever actually execute.
And that would be true if the conditional expression if false.
The do-while loop is a slightly different structure that ensures that regardless of
what the conditional expression is,
the body of the while loop will be run at least one time.
10:30
So here's an example of a while loop that might not ever execute.
If we said that the integer A = 0 and the integer B = 1.
If we made the conditional expression for
the while loop to be while A = B, that code in the body
of the while loop will never run because when we start A does not equal B.
We just skip it.
It's like an if else statement where the if statement Is false and
there's no else clause.
Well, the do-while syntax is a little bit different.
We put the do at the beginning and the while and
conditional expression at the end.
And the way this is executed is that a control flow goes down through
the program, the multiple lines of code are run at least one time.
Then when we get to the end of it, the while conditional_expression is evaluated
and if it's true, we go to the top of the do-while loop and run the code again.
And then if the while loop is still true, we run it again.
And we run it again, and again, and again, until that conditional expression
evaluates to false, and then we continue down the execution of the program.
11:28
So the thing to be careful of is that loops can run forever.
And that would be the case if the conditional expression never
becomes false.
So here's an example.
While 1 equals 1, that loop is gonna run forever and
there's no way to change the fact 1 equals to 1, and then to leave the loop body.
So that would be an example of a loop that never ends.
Another example of a loop that never ends is do, and
that ends with while 1 equals 1.
That would be run once, the conditional expression would evaluate to true, and
forevermore that conditional expression would be true.
And those loops are just run forever.
Rarely is it the case that that is correctly written code.
Usually, that's a mistake that's written through variables in the conditional
expression that you didn't realize were always going to evaluate to true.
12:15
So in summary, loops are used to execute code repeatedly,
something which computers excel at.
There are three common patterns of loops.
The while loop, the for loop, and the do/while loop.
They're used in different cases and in different contexts, but
they're a key component of writing effective
repetitive code in objective c and in many other languages as well.
Sometimes the looping constructs are a little bit different in other languages
but these are the ones that are commonly used in Objective-C.
Thanks, [MUSIC]