- [Instructor] The basic while loop control looks a lot
like the simple IF statement
in this case
the statement block will execute repeatedly
until the condition is false.
The flow of a while loop also looks a lot
like an IF statement.
The difference is,
after the statement block is executed,
flow returns back to the condition
and this creates a loop.
The loop only ends when the condition becomes false.
Here I have a working copy of while.cpp
and you can see this is a simple while loop
and the condition while I is less than five,
is the condition that keeps the loop going
until the condition is false
and so we have an array with five elements
we initialize an integer variable called I
and we initialize it at zero
and so while is less than five,
So we enter the block
and we print the first element of the array
and then we increment I
so this is the C++ increment operator
and so now I
is one,
it was zero
and now it's one and we do it again
and we keep incrementing and repeating the loop
until this condition becomes false.
So as long as I is less than five it'll loop
once it hits five, it will stop.
So I'm going to go ahead and run this
and you see it prints out,
element zero is one element
one is two element four is five like that.
So the condition here in line 10,
the I is less than five condition,
is called the loop control.
As it controls the execution of the loop.
There's two additional loop controls available,
I can end a loop prematurely with a break statement
so I can come in here
and I can say
if I is
equal to three
break
and you'll notice that,
I have a conditional
and I have one statement on the same line
as the conditional.
So it's worth noting that the statement block,
which is what this represents here
this break statement represents,
the statement block may be a single statement
and this is a common technique
when you're using a single statement as a block
you don't need the curly braces
if there's only one statement in a block,
be careful though this can lead to errors
if you add statements to the block later.
Here I have that one statement statement block for the IF
and the break statement,
actually transfers control to the end of the block.
We saw this with the switch statement as well
and so now I'm going to go ahead and run this
and as soon as I is equal to three,
which is the fourth element it'll break
and it won't print this fourth element
it'll just end after the first three.
So go ahead and we'll run this
and there we see it ends,
when I equals three It ends before
printing that fourth element.
There's one more loop control
that I can use instead of the break.
I can use a continue statement
and so here, I'm going to say, if I is equal to two
and this time I'm going to use
the curly braces cause I have two statements in here,
I'm going to increment I
and I'm going to say continue
and what continue does,
is it jumps down to the end of the loop
and instead of breaking out a loop,
it continues so it just skips the rest of the loop
but it still continues
and so in this case I equals two would be the third element.
It's just going to skip that element.
So we'll go ahead and we'll save and we'll build
you can see it just skips that element
where I is equal to two.
So I'm going to take this out now and I'm going to show you,
one more different type of a while loop.
Instead of putting the condition at the top
we can put the condition at the bottom.
So instead of while here, I say do
and I put the while at the end
and so now instead of testing at the top of the loop,
I'm testing at the bottom of the loop.
So in this case,
the result is the same
but there are times when you might want to test something
at the bottom of a loop where
the result might not end up being the same.
This is not as common.
You won't see this very often
but you will see it occasionally
and it's good to understand how this works.
So this is just like the regular while loop
except the test is at the bottom.
So we have this do
and sometimes it's called a do while loop
because you have the do at the beginning
of the block and the while at the end of the block.
So the while loop is inherited from C
and it's the same in both languages.
It's a fundamental control in C++
do while is useless frequently,
but it's still important to understand.