Hello, and welcome. In this lecture, I’d like to talk about the while and do while loops.
We've talked about the for loop, which essentially allows you to repeat code.
There's other constructs in c called while and do while that do the same thing
with just different syntax.
So the while loop the while loop is a mechanism for repeating a set of statements
and it allows execution to continue for as long as a specified logical expression evaluates to true.
So more of the same here. Just like if statements will execute certain code,
while statements will execute certain code based on a Boolean evaluation a number of times.
Everything you can do in the for loop, you can do in the while loop,
it's just different syntax.
So what the while loops looks like is something like this.
At a very high level, if we're looking at pseudocode,
while this condition is true,keep on doing this.
So to relate it to a more world world example.
Basically, while you are hungry, eat sandwiches, something like that.
So you're going to repeat code. You're going to keep eating many sandwiches while you're hungry.
The general syntax for the while loop is as follows.
And this is showing you the syntax for one statement in the body,
so you don't need to provide brackets or curly braces.
I tend to use the terms curly braces and brackets
in the same terminology.
So here is what the syntax looks like when you have one statement versus multiple statements.
Multiple statements, you have to have those brackets,
single statement you don't, they're optional. You can't have them or you don't have to have them.
The expression goes inside the parentheses just like the for loop
and the if statement. The expression here is
different from the for loop in that it's a single exit condition.
It's just the expression. You don't initialize the loop control variable.
You don't modify the loop control variable in between the parentheses.
It's just the expression, just the exit condition.
The condition for continuation of the while loop is tested at the start.
So this is top of the loop.
So what this means is it's pretest loop. And what that means is
it could execute 0 times or it could execute multiple times in the body.
And again, this is going to be relevant because the do while loop is doing something a little different.
If the expression starts out false,
none of the loop statements in the body are going to be executed.
So that's why it's at the top test.
So if you answer the first question no I'm not hungry,
then you're never going to eat any sandwiches. You're going to move straight on the coffee
or the next line of code. You're not going to enter the body of the loop.
If the loop condition starts out as true,
the loop body must contain a mechanism for
changing this if the loop is to end. Otherwise, you're going to have an infinite loop.
So with the for loop where you modify the loop control variable in a single statement,
it's the same thing in the while loop except you have to do it in the body.
The while loop is going to initialize
loop control variable somewhere,
and then it's going to modify inside the body of the code.
So as I mentioned, the while loop can do everything a for loop does, it's just different syntax.
And again, the for loop is more is shorter. You can do everything in one line
whereas the while loop you have to do those 3 things on separate lines.
So in this example,
we have int count equals a 0. This is the loop control variable. This is outside the while loop statement.
If you were using a for statement, this would go inside the for loop as before the first semicolon.
That's where initializing loop control variable.
Then you have the while keyword and you have inside the parentheses the Boolean expression.
This would be equivalent to the for loop, the second
statement right before the second semicolon.
So this is what you check for true or false. And if it's true or false, you enter the body of the loop.
If count is less than equal to 5, then you're going to print out the count.
The next line of code after that is very important,
the plus plus count because that's modifying the loop control variable.
You have to do this plus plus count or you're never going to exit the loop
because the condition at the top saying if the count is less than equal to 5. If the count is always equal to 1,
you have an infinite loop.
So that plus plus count very, very important.
Plus plus count remember is an incrementation operator.
It's just saying count equals count plus 1.
This loop is going to run 5 times.
It's going to run with count equal to 1,
count equal to 2, count equal to 3, count equal to 4 and count equal to 5.
Once count is equal to 6, it's going to jump out of the loop and
it's not going to exit any more statements in the body.
Again, you should be thinking in terms of the for loop.
It's just the loop control variable and the modification of the loop control variable
is not in the declaration of the actual loop.
It's not all in one line like it is in the for loop.
Here's another example where the logic is controlled to end the loop.
So in that first example here that I showed, this is a counter control loop.
It's going to always execute the same number of times which is 5.
In the next example, this is an example where the loop may execute
100 times or 0 times.
The condition that we're evaluating here in our while key where num is not equal to negative 1
is all dependent on what the user enters because num
is what the user enters.
So this loop may execute 0 times, it may execute 100 times.
It all depends on the input from the keyboard.
And this is called a sentinel control loop. It's not counter controlled.
It's based on logic and variable that can change.
So while num is not equal to negative 1 and then num gets
stored the value that we enter in the keyboard.
If the user never enters a negative 1 in the keyboard, then this loop is never going to exit.
So same type of syntax here with our while.
Our loop control variable here is num. We initialize the 0 at the top,
and then we read in modify it to see if we're going to enter the loop
based on what the user entered in the keyboard.
Then inside the parenthesis for the while keyword,
we actually check it to see if it's a negative 1.
And then in the body of the while loop with the brackets,
the brackets don't need to be there because there's a single statement, but I like to put them in there.
Then we also read the next input and we stored in num.
If the user enters a negative 1 for that next input, it will jump out of loop.
If they don't, it will continue to execute and ask them to enter information.
A do while loop is a little different from the for loop
in the while loop because it's considered a post-test loop.
So in the while loop the body is executed while the condition is true.
In the do while loop where the body is executed for the first time unconditionally,
the do while loop because the test is at the bottom
means that you're always going to execute the loop at least once.
You're always going to execute the body one time
at least, it could be more, but it always one, it's never 0.
Because the condition to test the exit is at the bottom.
This is called a post-test loop. It's also called an exit controlled loop.
So you would use the do a while loop if you wanted the loop to
enter at least one time.
Typically, you don't need to use a do while loop
because you can still do the same thing in a for loop if you wanted to,
you would just put the code outside the loop.
So the do while loop is not really necessary, but there are cases maybe you want to use it.
After initial execution,
the body is only executed while the condition is true. So after the first iteration of the loop,
it's going to then act just like normal loops. It will jump out if the condition is false.
So here is what it looks like. The do is at the top
and then the testing for the condition is at the bottom with the while keyword.
So do is a new keyword
and then you're going to either have brackets or non-brackets depending on
whether or not you have multiple statements in the body.
If you only have a single statement in the body, you won't have a bracket. You'll just have to do at the top
and then you'll have the while at the bottom of the loop.
And inside the while, the parentheses, you'll have the Boolean evaluation
that will be the condition that you check. But again, because that
condition is at the bottom you're always guaranteed to execute the statement at least one time.
And so the example on the left is one statement, the example on the right
is multiple statements. So you have the brackets, the curly braces.
And again, in this second example, you're saying you may have something like prompt for password,
read the user input and while the input's not equal to password.
But you're always going to prompt for that password at least one time.
So another do a while loop example.
The first top example here is an example of
how the loop may execute
one time or many times, but never 0.
It all depends on what the user enters. It's always going to execute at least once,
but this is reading input from the keyboard. We see the scanf function.
And as long as the user doesn't enter 20,
they'll be executing this loop.
A while loop can also be a counter control loop, that means it's going to execute a certain amount of times.
In this case, it's going to execute 4 times in this example.
We have int number equals 4. And then we have do
at the top,
and then we have the brackets because we have multiple statements inside the body.
And we're incrementing that loop control variable inside the body, number plus plus.
And then the condition for checking is still at the bottom. So you're going to execute this 4 times.
And you'll notice the while keyword doesn't have to be on the same line as the right curly brace.
Typically, it is though, but it doesn't have to be.
And the while keyword at the bottom too ends in a semicolon, you should also notice that.
So we've learned all about these loops; the for loop, the while and do while.
What loops should you use?
Well, first thing you have to decide on what loop you want to use is first
do you want it to be pre-test orpost-test? Do you want it to execute at least once
or do you not want to execute at least once? If you want it to execute at least one time,
then you're going to use the do while. If that's not a condition,
then you can use the for or the while or the do while.
So for the most part when you decide what loop to use,
it's always going to be a pre-test loop. You should stay away from the do while loop.
It's just a better option in most cases. It's much easier to read.
It's much better to look before you actually loop.
Just don't start looping, it's better to check that condition at the top.
For readability sakes, if the loop is at the test, it's much more intuitive.
So have that loop check at the beginning, use a for or a while.
And in many cases, it's important that the loop be skipped entirely if the test is not initially met.
Again, because that's the case the test should be at the top. You shouldn't always be executing at least once.
For the most part you always may not want to execute.
So you should use a for a while.
Now the question comes down to well which one should I use, a for a while.
It's really a matter of personal preference. It doesn't matter
because like I said in the beginning of this lecture, everything you can do in a while, you can do in a for.
So if you just like the for loop better, the for is a little quicker sometimes because
all the loop control modification and the initialization and the
exit condition can all be in one line.
But it's really up to you.
Typically, what I do is I’ll use the for loop for counter-controlled loops.
But again, you can use the same. If you actually want to use a for loop
and make it look like a while, you just don't have the first
and the third expressions,
you just have the single one. So the for semicolon tests semicolon is the same as while test.
So this is just showing you that the for in the while can do the same thing,
just change the syntax a little.
If you want to actually use a while loop but you want it to act like a for loop,
then you would prefix it with the initialization and include update statements in the body.
So what you would do is something like this and you usually always do this for a while loop.
But this is the equivalent of a for loop. The initialization is outside the loop
of the control variable, the loop control variable.
The condition of test is inside the parentheses,
and then you're updating that loop control variable in the body of the while loop.
And that's the same as the for loop initialization test update
and then the body. There's no difference.
Again, as I mentioned I use the for loop maybe for counter control loops a little bit more.
But I’ll use both the for and the while interchangeably, depending on how I’m feeling that day.
So a for loop might be a little more appropriate
if you want to initialize and update a variable in one line.
A while loop is better when the conditions are otherwise.
Again, counter control loops for, sentinel control loops maybe the while loop.
And here's an example of a while loop with a sentinel control loop
and a for loop that's doing the same thing again or a for loop for a counter controlled.
While loop can run many times
based on what the user enters, the for loop is counter controlled.
So this is the while on the do while. Hopefully, this is clear.
Become familiar with these loops because you often want to repeat code.
In the talk a little bit about nested loops and also
a little different mechanisms to exit a loop
using a break or continue. Thank you.