- (Instructor) C plus plus provides
two versions of the for loop.
One is inherited from C and the other
is unique to C plus plus.
So first we're going to look at the C4 loop
as this is by far the most common.
So here we have for.cpp from chapter two
of the exercise files, but before we start with this,
I want to come back to our while.cpp
from the previous lesson, and take a look at how this works,
because this will make it obvious
why the for-loop exists and how it works.
So, remember we first had to initialize a variable,
and then we had to test that variable
in the condition for the while loop,
and then we had to increment that variable
so that the While Loop wouldn't run forever
and eventually it would hit its condition and end.
And so, in the for loop,
what we have here is we have all of those things
in one control.
We have the declaration and initialization of a variable,
which hasn't scoped within the block actually
so it's a little bit cleaner.
And we'll talk more about scope later on in this course.
And then we have the condition.
As long as this is true, the loop will continue
and then we want to make sure that eventually it isn't true.
And so we have this increment,
which happens at the end of the loop.
So these three controls.
The first one is essentially an expression
which is evaluated first before the loop begins.
And so in this case we're declaring
and initializing a variable which is usually
what you're going to do in this first part of the for loop.
And then we have a condition
which is just like the while loop.
So it's essentially a while condition.
And then we have what's called the post loop control,
which happens at the end of the block
before the loop is repeated.
So this will happen over and over at the end of the loop
until the condition is no longer true.
So when I build and run this, you'll see it works
exactly the same way as the while loop,
because it is essentially the same thing.
It's really just a syntax shortcut for all the things
that you normally do in a while loop.
So, one way this can be very useful
is when iterating through a string or any sort
of an array really, but we really use a string
So I'm want to say character S and array
equals string like this.
And you remember that a string is simply
an array of characters with an alternator.
So this is a seven element array
with six characters and a zero.
And so all of this is going to change.
I'm going to declare a pointer, a character pointer,
and initialize that to the beginning of the array.
And then I'm going to test it and I'm want to increment it.
So this test what's pointed out by P
as long as that is not zero, the loop will continue.
Remember that false is zero,
and the string terminates with zero.
So when we get to the end of the string,
what's pointed out by P will become zero
and the loop will end.
And here we increment.
And so we can just change our print F,
so that it prints out the characters,
like that, and now when I build and run,
you see it prints out each of the characters in the string,
and when it gets to that null terminator, the loop ends.
So the for loop is a fundamental control and C plus plus.
It's inherited from C and is exactly
the same in both languages.
It's incredibly powerful or useful,
and it may look cryptic at first,
but once you get to know how it works,
you'll find a lot of uses for it.
There's also another form of the for loop
that's exclusive to C plus plus,
and we'll look at that in the next lesson.