Tip:
Highlight text to annotate it
X
TOMMY MACWILLIAM: Let's talk about loops, one of the common
programming constructs we'll see throughout CS50.
We'll use loops when we want to perform a
task more than once.
For example, we might want to print "hi" 100 times or print
out all the letters of the alphabet.
In both of these cases, we have one thing we want to do
multiple times, print something out.
However, what we print out each time can vary.
So we can do something slightly different on each
iteration, or run-through, of the loop.
We'll see three different types of loops in C: while
loops, for loops, and do-while loops.
Though these three types of loops have a different syntax,
the ideas behind them are the same.
We'll define some block of code within curly braces,
called the body of the loop, that we want to be executed
some number of times.
By changing the values of variables used in the body, we
can make our loop do something different each time it's run.
With any loop we write, we'll also need to decide when the
loop will stop running.
If we don't do that, then our trusty computer will continue
to run that loop until we kill the program.
In Scratch, we could use the repeat n times
block to create a loop.
All of the pieces inside a block that said repeat 10
would be run 10 times.
And then, we'd move on to the pieces after that loop.
So our stop condition was simply, this block has been
run 10 times.
So let's recreate this in C. In order for Scratch to ensure
that the pieces within the repeat block are executed
exactly 10 times, Scratch needs to keep track of each
execution of the repeat block.
To keep track of how many times our loop body has been
executed, let's create a variable called i.
We'll start i off at 0, since our loop
hasn't been run yet.
Okay.
Now we'll use the while keyword to start off our loop.
Now we'll need to figure out when our loop will stop, but
let's leave this for now and then come back to it.
All right.
Inside of our loop, let's just print a message out, like
"hi." We can use the printf function for this.
All right.
So now we'll record that an iteration of the loop body has
been executed.
We can do that by adding 1 to, or incrementing, our counter
variable, i.
To do that, we can say i is equal to i plus 1, or more
simply, i++.
Great.
So now we can see that each time our loop is run, our
counter variable goes up by one.
So we know exactly how many iterations we've run so far.
After one iteration of the loop, our value
of i will be 1.
After two iterations, i will be 2.
And after 10 iterations, i will be 10.
So, if we want to run this loop exactly 10 times, then
this is when we want to stop.
So we want to run this loop while i is less than 10, and
that's exactly what we'll write, while i
is less than 10.
This condition looks just like the conditions we used in if
else blocks.
After the body of our loop has been executed, our program
will jump back up to the loops condition.
If the condition is true, then the body of the
loop will be run again.
If the condition is no longer true, then our loop won't be
run anymore and will move on to the next line of code
underneath our loop.
All right.
So let's take a look at a second type of
loop, the for loop.
Next to the while keyword, in parentheses, we had one thing,
the condition that needed to be true for the
loop body to be run.
That means we had to create our counter variable outside
of the loop and remember to increment it at some point
inside of the loop.
The header for our for loop, on the other hand, has three
parts, each of which will be separated with a semicolon.
In our first third, we can declare any counter or helper
variables we'd like to use in our loop.
In practice, this can be really helpful.
We really don't need that variable, i, after our while
loop has run, so we really shouldn't have to declare it
outside of the loop.
The second third will be the condition that must be true
for the body to be executed again, just like
in our while loop.
In our last third, we can run a statement that will be
executed after each iteration of the loop, so we don't have
to build it into the loop body.
So let's write a for loop that counts down from 5 to 1.
We'll start with the for keyword.
We can create a counter variable first, which we'll
set to 5 this time since we're counting down,
followed by a semicolon.
Next is our condition, which we'll come back to.
Third, we'd like to decrement our counter variable after
each iteration of the loop.
So rather than saying i++, we'll say i--.
All right.
So we want the loop body to run while i is still
greater than 0.
In the body of the loop, let's print out the value of i.
To do so, we'll use the printf function, using the %d
placeholder.
Remember, that placeholder will be replaced with the
value of i.
Finally, let's add a statement after our for loop.
When we run this loop, i will start off at 5,
so 5 will be printed.
Once i gets to 0, the continuation condition, i is
greater than 0, will no longer hold.
So our loop will stop executing, and we'll see the
statement after the loop.
So let's run this code.
First, we'll compile a
countdown.c with make countdown.
Now, we can run this code with ./countdown.
In both while loops and for loops, our continuation
condition will be checked before the body
of the loop is executed.
That means that if our condition is not initially
true, then the body of our loop will never be run.
So it's sometimes useful to check the condition after the
body of the loop rather than before it.
So let's write a loop to prompt the user for a number
until a positive number is supplied.
If the user inputs a negative number, we'll want to ask them
for another number.
So we'll want this prompt to be inside the
body off the loop.
However, when the loop is run for the first time, the user
hasn't given us the number yet.
So it doesn't make sense to check if it's positive.
Instead, we'll want to check the number after the body of
the loop is run.
We can do this with a do-while loop.
First, we'll create a variable, n, that will hold
the user's input.
Now we'll use the do keyword, followed by curly braces that
will start off the body of our loop.
In the body, we can prompt the user for a number with the
GetInt function.
Now, we'll want the body of this loop to execute again if
the user typed a negative number, so we'll say while n
is less than 0.
Notice the semicolon here after the while statement.
So let's run this code.
First, we'll compile this with make positive.
Now we can run the program with ./positive.
If we give this program a negative number, like -4,
then we'll be prompted again for a number since the
condition of our do while loop was true.
Once we give a positive number, like 8, then the
condition of our do while loop will no longer be true.
So the loop will not be executed again.
And that's it for the three types of loops we'll use in C.
My name is Tommy, and this is CS50.