In this video we'll learn about the c++ while loop.
The while loop is an example of a pre-test loop because the test is
done at the beginning of the loop.
So it's possible that if the test fails immediately, then the
loop body will never execute.
The syntax for the while loop is the keyword while, followed
by an expression in parentheses.
This expression has to evaluate to a Boolean value.
This is followed by the statement or statement blocked to execute if
the expression evaluates to true.
In this slide, we can see a simple while loop that's simply counting.
We have a looping variable named i and we initialize it to 1.
The loop condition is i less than or equal to 5.
So as long as i is less than or equal to 5, we continue
executing the loop's body.
In the body of the loop, we display the variable i
and then increment it by 1.
Eventually, i become 6, and the loop condition fails.
And the loop terminates.
It's important that we increment i in the body of the loop.
If we don't, then the loop will never terminate since i will always be 1,
which is less than or equal to 5.
This is called an infinite loop, and it's not usually what we want.
I'll talk about infinite loops in a later video in this section because
sometimes they can be useful.
Let's see another example.
Let's write a while loop that displays the even numbers between 1 and 10,
just as we did with the for loop.
In this case we, declare i and initialize it to 1 since this is
where we want to start looping.
Then in the loop condition, we'll check to see if i is
less than or equal to 10.
So in other words, we will loop while i is less than or equal to 10.
And then at each iteration, we execute the body of the loop.
In this case, we have an if statement that uses the modulo operator
to see if it's an even number.
If it is, then we display it, then we increment it.
Note that we increment i regardless of whether it's even or not.
Eventually, I becomes 11 and the loop terminates.
Let's see another example of using a while loop with an array.
In this example, we're using a while loop to iterate over an array.
The array is named scores.
And it's an array of three integers.
We set the integer I to 0 since array indexes begin at 0.
And we loop while I is less than three as we've done in other array code.
In the loop body, we display scores sub I then increment I.
When I becomes 3, the condition fails and the loop terminates.
Loops are commonly used in programming to provide input validation.
In this example, we want the user to enter an integer less than a 100.
If they don't, we want to ask them again and again and again
until they enter a valid integer.
So we declare variable name number and initialize it to 0.
Notice now how we prompt the user for the integer and read it from
the console before the loop begins.
Now we have the value they entered in number.
The loop condition is now evaluated.
And if the user's number is not greater than or equal to 100,
then we have a valid number and we never go into the loop.
We simply print the thanks message at the end.
However, if the user enters an integer that is greater than or
equal to 100, we go into the loop and ask for another integer read
it in and then we test again.
This will continue until the user enters a number less than a 100.
Notice that we have some duplicated code here.
We have the same output and input statements outside the
loop and inside the loop.
This is usually a clue that the code could be better.
We'll make this code better in the next video when we
discuss the do while loop.
But this kind of code is commonly seen.
Let's see another example.
In this example, we ask the user to enter a number between 1 and 5.
Notice the code is very similar to our previous example with the exception
of the condition in the loop.
In this case, we're testing both ends.
So if the number is less than or equal to 1, it's not valid.
We also test number greater than equal to 5, which is also invalid.
If either of these conditions is true, then the number is invalid.
So that's why we use the logical or here.
In this example, we'll use a Boolean flag to control our while loop.
First, we declare a Boolean variable named done, and we
set it initially to false.
The idea is that when this variable becomes true, then the
user entered a valid number.
So the loop test while not done, not done will be true since we set
it to false, so we enter the loop.
In the body of the loop, we prompt the user for a number and read it.
If the number is not valid, we display an error message and loop
again since done is still false.
If the number is valid, we display thanks and set the done flag to true.
Now the loop condition will fail and we exit the loop.
There are many ways to write logic and to achieve the same outcome.
Always remind yourself of the kiss principle.
Keep it simple stupid or keep it short and simple or keep
it simple and straightforward.
However, you want to think of it.
Basically, don't be too clever.
Keep your code as simple as possible.
Let's head over to the IDE and see some other examples of the while loop.
I'm in the section 9 workspace in the while loop project.
What I want to do is write a couple of while loops some that count down,
one that counts up and a couple of more, more complicated ones.
In this case, I've already written one that counts down.
So the idea is we ask the user to end positive integer to
start the countdown and then we start the countdown and we
blast off when we get to one.
So if they type in 5, we'll say 5, 4, 3, 2, 1 blast off.
Okay.
So let's walk through this code so you can understand it.
First thing we're doing is we're defining that num right up here.
That's where the user is going to enter the number into.
And let's say they entered a 3, so they're going to enter the 3, and
we're going to store it in num.
So I've got num over here, and its value is 3 right now.
Now we check this condition is 3 greater than 0.
Well, that's true.
So we execute the output statement, which says just display num,
and I'll do that over here.
So I'm doing the display of 3, and then i decrement num.
Remember, when it's all alone on a line like this, the
pre-decrement in a post-decrement means exactly the same thing.
So I just change num from a 3 to a 2.
I go back up and test the condition again is 2 greater than 0, it is.
I output the num which is 2.
And then i decrement it to 1.
I'll check the condition again is 1 greater than 0, it is.
So I'll display the 1.
Then I decrement this to a 0.
And I check the condition one final time.
In this case, 0 greater than 0 is not true.
So I stop, and I end up executing that line right after the loop blast off.
So what we end up with is 3, 2, 1 blast off.
Okay.
So that's a good way to walk through it.
And what we'll do is we'll run this.
And let's do it with the 10.
And there you can see the code going 10, 9, 8, 7, 6,
5, 4, 3, 2, 1 blast off.
Okay so now let's write a while loop that counts up.
So I'll comment this code out right here, and I'll paste some
code that I've already written to save a little time, and
let's walk through this code.
In this case, notice that I'm defining again declaring and defining this
same variable num to be an integer.
I ask the user to enter a positive into integer to count up 2.
So in this case, I want to count from 1, 2., whatever they typed in.
And I'm reading then.
So again suppose they typed in a 3.
So now I've got num and its value here is 3.
I can't do what i did up here with just a single variable because
I don't want to change num.
Num is my target as to where I'm going to stop.
So i need another counting variable or a looping variable.
In this case, I want to use I.
So I've got I over here.
And I'm initializing i to 1 because that's where i
want to start counting at.
So I becomes 1.
All right.
Now we do the loop.
We test the condition is num greater than or equal to i.
So is 3 greater than or equal to 1, in this case.
This is true so what we do is we display i, and I'll
do the display over here.
We'll display i, which is 1 and then we increment I.
So now I is 2.
We do the loop condition again is num, which is 3 greater
than or equal to 2, it is.
We execute the body of the loop, so we print out I, which is now 2.
Okay, I hope you guys are with me.
I increment I, I now becomes 3.
Then i go back up and I test the loop again
is num which is always going to be 3.
Is num greater than or equal to three,
it is, right. 3 is equal to 3.
I want to output I again. I is 3
and then I increment I.
Now I becomes 4.
We'll test one more time. Is 4
greater than
or -- sorry is 3 greater than or equal to 4.
it is not.
So it fails at this point.
Now we come down here, and whatever statement would
be here, we would execute.
Okay.
So you can see that walking through these examples is pretty easy to do.
It's kind of time consuming, but once you get used to the
logic, you can pretty much do a lot of it in your head.
Okay.
So let's do another one now.
And I'll clear this up, and I'll comment this all out again.
So in this case, what we want to do is, we want to
do some input validation.
And I've written the code again.
Let me paste it in and let me indent all this.
I don't know why it puts it all flush left.
There we go.
Okay.
So we've got an integer here called number that we've declared,
and we've initialized it to 0.
And we're asking the user to enter an integer less than 100.
And we're reading that into that variable number, right.
Now we want to loop, remember, we want an integer less than a 100.
So we want to loop while that number they entered is not less than a 100.
So we could say not less than a 100 or greater than or equal to 100.
Either case, will work here so.
In other words, if the number they entered is greater than or
equal to 100, it's not valid.
So in that case, we'll just ask them to enter an integer less than 100.
Again, we're prompting them to enter it, and we're reading it.
And then we're just checking that while statement
over and over and over.
When this condition fails, that's when we have a valid number.
Okay.
So let's run this, and we'll print out thanks when we have
a valid number at the end.
So let's run this you see what it looks like.
Let's say I want to integer less than 100.
Let's do it correctly, we'll type in 50, and we get a thanks.
But if we run it and type in a number that's not less than 100, let's
say we typed in 120, it prompts me again I'm in the loop now.
So let's say 115, 1000, it'll just keep going forever until
I type in a valid number.
And basically, this is the basics of input validation.
So 99, that's valid thanks.
Okay.
So there you go.
You can walk through this the same way that I walked through the other one.
Notice that you can write this condition in here a lot of different
ways, right because greater than or equal to 100 or not less than 100.
Okay, so there's different ways that you can write that condition.
And you'll see it all kinds of ways out there.
All right, so the last example I want to do is one using a flag -- a Boolean flag
to do input validation so let me comment this out and I'll copy that
code in real quick okay so there's the code now we're going to walk
through this real slow and in detail so you can see what's going on this
is a common use case and this is the kind of code you see out there a lot.
So in this case we've got a flag called done.
And we're initializing it to false.
False means that we're not done right we still need to get more input from
the user for whatever reason either we haven't started yet or they've entered
something illegal or out of range.
And I've got this variable right here that I've declared here called
number that's the integer that's what we're going to read into.
Now it's important that we set that flag to false to begin
because that's how we're getting into this loop the first time.
So don't just leave that to true or just don't leave it to nothing
and take a chance that it is false.
Explicitly set it to false because that's going to get us
in that loop the first time.
So the first time we're going to say while not done well it
isn't done right it's false.
So we're in here and we ask the user to enter an integer between 1 and 5.
And they type the number right there.
We're going to store it in the variable number.
Now we do a check.
If the number is less than or equal to 1.
Remember, we want numbers.
There's the 4, here's the 5.
So we want numbers between there, right.
So we want 2, 3 or 4.
1 and 5 are not valid in this case.
So if the number is less than or equal to 1, in other words the
number is over here somewhere or the number is greater than or equal
to 5, so the number is over here somewhere, then it's out of range.
So we simply display out of range try again.
Notice how we didn't change the done flag.
The done is still false, right.
So at this point, we just executed that statement.
We don't do the else part and we loop right back up to here, right.
Done is still false.
So we're going to go right back into the loop and output again
enter an integer between 1 and 5, grab the integer and so forth.
Now when you do type an integer that's in range, suppose we typed in a 3.
Then this if statement fails here and we go to the else.
And all we're doing here is saying, thanks.
And then we set that flag to true.
This is like super, super important here.
Don't forget to do that.
That means we are done so when we loop back up again here, that condition
fails, and we're out of the loop.
Okay.
So hopefully, you can walk through this and just take it a step at
a time and really understand it.
The challenge exercise at the end of this section is a pretty
tricky and hard challenge.
But it uses this kind of logic to ensure that you've got input
validation and you're going to prompt the user every time that
they are not in range, right.
So let me clear this and let's run this example.
So we're going to enter an integer between 1 and 5.
Let's say we type 1.
1 is not valid.
We want it to be between 1 and 5.
Again, 2, 3 or 4.
So that's no good.
5 is no good.
70 is no good, right.
Negative 12 is no good.
So let's give it something good.
Let's give it a 3.
Something that's right in the middle, thanks.
So you can see that using this kind of logic is better than using
this sort of logic, where you've got a statement repeated here and
then the same statement over here, and you've got duplicated code.
We don't have that here.
And the reason we don't have that here is because we're
using that flag to really decide whether we're in the loop or not.
Now in the next video, we'll talk about a do while loop,
where we're doing the condition at the end and that makes
this kind of code even nicer.
Okay, so that's the end of this video.
Please try these examples yourself, come up with your own versions
of them, and walk through them so that you really understand
the logic, and what's going on.