Sei sulla pagina 1di 3

DOUG LLOYD: All right, so let's talk about loops.

So loops are kind of cool because


they allow your program to execute lines of code over and over and over.
Repeatedly, without having to copy and paste or otherwise repeat them. There are
three major kinds of loops you'll see. You'll probably have occasion to use each
one as you progress through CS50. Some of these loops are pretty familiar to you
from Scratch, so again, as we did with conditionals, we'll put them up side by side
if there is an analogy we can draw.

First is forever from Scratch, which compares to while (true). This is what we call
an infinite loop. The lines of code between the curly braces, much like the puzzle
pieces that fit inside the C of the forever block, will execute repeatedly from top
to bottom, over and over and over, forever. After all, if you recall our discussion
of Boolean expressions, true is always true. So if we're doing something so long as
true is true-- it seems a little silly-- true is always true, so it'll always run.
It'll run forever and ever and ever until we find a way to break out of it with a
break statement, which again, we saw a little while ago we were talking about
switch.

Or otherwise just kill our program, which incidentally, if you ever find yourself
in a situation with an infinite loop and you don't know how to stop your program
from running. Just hit control and C and that'll kill your program for you. But
this, again, is called an infinite loop. It is a loop that will run forever. Now,
while loops are not always infinite, because also we can replace the Boolean
expression with something a little more useful than just true. So here's another
example. While and some Boolean expression and then curly braces. That's pretty
much analogous to repeat until, from scratch. Although in a second, I'll explain
the difference.

So in C, if the Boolean expression inside of the while loop evaluates to true, the
lines of code between the curly braces will execute repeatedly over and over and
over until Boolean expression evaluates to false. So for example, you might have
some sort of counter. So say you at the beginning of this loop say int x equals
zero. While x is less than 100, do a couple things inside that curly braces, then
in the end of your loop, you say x plus plus, you're incrementing x. Eventually x
will get to 100 and you will stop because the condition x is less than 100 will no
longer be true as soon as x is equal to 100. 100 is not less than 100.

Now somewhat confusingly, the behavior of the scratch block is reversed. So be


really careful if this is your first foray into using loops. Basically, the repeat
until block in Scratch will repeat something over and over and over, until the
expression, the Boolean expression in the hexagon is true. So it will keep doing it
until it is true. Meanwhile, the while loop will continue to do it until it is
false. So they're quite similar, but there's that little distinction just to be
careful of especially as you make your first foray from transitioning to Scratch
into c. The next kind of loop is actually pretty similar. It's called the do while
loop. This loop will execute all the lines of code between the curly braces once,
and then it will check the Boolean expression. If the Boolean expression evaluates
to true, it'll go back and repeat that process over and over and over until the
Boolean expression evaluates to false. So this loop, unlike a while loop, is
guaranteed to run at least one time. This can be pretty useful.

Lastly, we have a for loop. For loops are kind of syntactically unattractive and
there's a lot going on here, so bear with me as we try and break these pieces
apart. Generally, the use case of a for loop is you want to repeat something a
specific number of times. In this example here, I have the repeat block from
Scratch, which is analogous to the for loop in C, repeating something 10 times. And
the for loop on the left there, which is a four loop that would do the same thing.
This would go 10 times. Counting from zero, we increment each pass of the loop. And
we keep doing that until i is less than 10.
So what happens? There's a whole lot of code there. Let's break it down to what is
happening step by step as we dig through a for loop. The first thing that happened
is the counter variable is center. Everything to the left of that first semicolon
inside the parentheses is executed. In this case, we are saying int i equals 0.
Declaring a new variable called i, that variables is a type integer and we're
saying the value inside that variable is going to be zero.

The second thing we do is we then immediately evaluate the Boolean expression.


Boolean expression is in the middle of the two semicolons. I is less than 10.
That's true here, right? We just said i is equal to zero, and so 0 is less than 10,
and so that condition is true and we will now proceed to execute the body of the
loop. If it was false. If, for example, I said instead of i equals 0 there I said i
equals 15, int i equals 15. Well 15 is not less than 10, so we would never enter
the body of the loop because the Boolean expression there would evaluate to be
false.

After we go through from top to bottom, we encounter that closing curly brace, a
third thing happens. The counter variable is incremented, or rather, the lines of
code of a statement after the second semicolon inside of the for loops parentheses
is executed. So we start out by saying int i equals 0. Then we check whether or not
the Boolean expression is true. 0 is less than, 10 so it's true. So we're going to
proceed into the body the loop. Things will happen inside the loop, and when we
encounter that closing curly brace, the next thing we do is we say i plus, plus. I
was zero now i is one. Then, we again check the value of the Boolean expression in
the middle. One is less than 10. So we'll go through this process again and again.
We'll get to the closing curly brace again. We'll increment i from 1 to 2 and from
2 to 3. And so on and so on and so on, until eventually i's value becomes 10. Is 10
less than 10? No. Counting from 0, we've gone through 10 iterations of loop. We've
repeated 10 times, just as we did in the Scratch block. And so that's basically the
process for a four loop.

Now taking away the actual code and putting it in just some basic general
statements. Here's what happens. All the statements in start are executed first.
You might have more than one. Then, the Boolean expression is checked. If the
expression evaluates to true, execute the body the loop one time. If the expression
evaluates to false, we're done. We don't execute the body of the loop at all. After
we've executed the body the loop one time, we then do what is in the increment part
there. Which is usually going to be something like i plus, plus or something like
that, that modifies a counter variable. Then, after we increment, we check the
expression again and repeat those steps over and over and over until the expression
is no longer true.

So what are the use cases for a loop? Use you use a while loop when you want a loop
to repeat an unknown number of times, but it possibly might not run all. A really
common example of a while looping being used is to run the control flow for a game.
You might not know how long the user is going to be playing, but you want to keep
doing the same thing. Updating the positions of various sprites on the board. You
want to keep things moving at all times, but you don't know when the user is going
to stop playing or when they're going to run out of lives. So a while loop is
really good for something like that. That's a good use case.

For a do while loop, it's pretty similar. You want a loop to repeat an unknown
number of times, but at least once. You might use this for a game too, but a really
common use case is prompting the user for input. Generally, you might say something
like, give me a positive integer, and you might put that inside of a do while loop.
So it will always ask them at least once for a positive integer. If they give you a
positive integer, you might break out of the loop. You might be done. The Boolean
expression in the do while might be false.
And you won't ask them again. If they give you a negative number where they type
some word or something that's not really useful to you, you might use the do while
loop to go back and do it again and again and again. You definitely want to ask
them to give you a number once, but you don't know how many times after that they
might be pests. And so a do while it's a great use case for prompting the user for
input. And a for loops use case typically is when you want to repeat a loop a
discrete number of times, though you might not know the number of times the moment
the program is compiled.

So for example, maybe you have a program where you prompt the user for a number.
And they enter 100. And then your for loop will in that case run 100 times. Or
maybe they enter 1,000 in your program and then run the loop 1,000 times. It's a
specific number of times. They don't necessarily know what that number is the
moment program is compiled. It's not like a while loop where it could be infinite.
It's rather a number that you just don't know. Now even though I've outlined all
these use cases, you should know that in pretty much every circumstance, you can
interchange all three of these loops. You can use a for loop where you would
otherwise use a while loop. You can use a for loop wherever you'd use a do while
loop and so on. It can be a little tricky sometimes, so generally it's a good
practice to adhere to a typical use case. Use a do while loop if you wanted
something once, at least. Use a for loop if you want to do something a specific
number of times. After all, that's why there are three different kinds of loops.

So they can be used in the right context. But you can interchange them if you want.
If you feel comfortable with while loops and you always want to use while loops. It
is something that you can do, although it might be a little syntactically tricky,
where a for loop would be a lot more straightforward.

My name is Doug Lloyd and this is CS50.

Potrebbero piacerti anche