(upbeat music)
- [Bill] The basic loop control in C++ is the while loop.
The while loop test condition at the top of the loop,
and there's also a version that test the condition
at the bottom of loop,
and we'll look at both of them here.
Here in Xcode,
I've initialize an array
with five elements,
an integer array with five integers,
one, two, three, four, and five.
I initialize an integer variable called i at zero,
and then in the while loop,
I test while i is less than 5.
So the while loop has a condition in parentheses,
and that condition is tested
for while it's true,
the while loop will continue running,
and when that condition gets to false,
the while loop will stop.
So here you'll notice
that at the end of the block,
I increment i,
and so at some point i will no longer be less than five,
and the while loop will stop.
So when I run this,
build and run,
you notice that it prints out
the element zero is one,
element one is two, et cetera,
until we get to element four is five,
and that is the elements of the array.
Elements, if you remember in an array,
are numbered starting at zero,
so element zero, element one, element two, element three
and element number four.
So that's the basic while loop.
There's a couple of other controls
that are worth knowing about,
one of them is the break statement.
So if I say,
if i is equal to two,
then we'll break.
Now, while will only run
up until it sees
i is equal to two,
which is this one here, the third one,
and at that point,
it will break.
As we've seen earlier,
break branches execution
to after the end of the block.
So it will break out of the loop and it won't run anymore.
So when I compile and run this,
you see we get element zero as one, element one is two,
and when it sees that i is equal to two here,
it simply breaks out of the loop.
There's another control called continue.
I'm going to put this inside a little block here,
and I'm going to say ++i and continue.
So in this case, what happens,
what continue does,
continue branches back to the beginning
of whatever control structure it's in,
and in this case,
it's in a while loop,
continue is usually used in a loop,
and it goes back up to the top,
and executes that control again.
In this case, what it'll do
is it will simply skip the printf
and this increment,
has its own increment,
and we'll continue after that.
So it'll simply skip element number two,
which is the three here.
If I compile and run,
you'll notice it says element zero,
element one, and element three,
skipping two.
That's what the continue statement does.
Now, instead of testing at the top,
we can test at the bottom.
So I'm going to use the cut here on the Mac,
which is command X or control X on a PC.
I'm going to put in a do instead.
I'm going to put that condition at the end
with the semi-colon there,
after the condition.
What this does,
is this puts the test at the end of the loop
instead of at the beginning of the loop.
So when I run this,
you'll see that it's running exactly as we expect,
but in this case,
it's not testing that while at the top,
it's testing that while at the bottom.
You won't see the do while
as often as you will see
the regular while loop with the test at the top,
but you will see it occasionally,
and there are circumstances where it's convenient,
and so it's good to know about it.
The while loop is of course inherited from C,
and it's the same in both languages.
While is a fundamental control in C++.
Do while is used less frequently,
but it's still important to understand.
(upbeat music)