- In this video we're going to look at a different
type of loop, namely the while loop.
The while is a little bit different than the for loop
because it doesn't assume a preset
or predetermined number of iterations.
Rather, while depends on a condition
to start and continue the loop.
The syntax is very simple, while,
the condition is true, doSomething.
Let's look at an example.
We're setting a variable flag to false,
assumed to have been defined previously as an int.
While not flag, in other words while flag
is still false, readTheNextPage.
That's a function defined elsewhere.
Then we have an if.
If charactersToRead equals zero, we bring
to finished the book and we're setting
the flag to true.
Once the flag is set to true, the execution
goes back to the while because we reached
the end of the while structure,
and again we'll test is flag false or true.
If the flag is true, meaning the condition
is no longer valid to continue the loop,
the while loop stops.
If however, the characters to read is not zero,
then the flag is not set to true,
it continues to be false,
and the while loop continues.
One of the things to pay attention to
when using while loop is to ensure
that a condition for halting eventually occurs.
Otherwise, we may get into a situation
of an infinite loop.
This might probably happen especially
to beginning programmers, and it's
a common error when you write code.
So just a word of caution here.
Whenever you implement a while loop
or any other loop, ensure that the conditions
for halting the loop eventually will occur.
Let's switch to C and see how this works.
In this little program I created two functions
to illustrate how the while loop works.
The first one, it's called while loop one,
simply prints numbers until a value
reaches the number 10.
I'm initiating the while loop with a certain value
of n received externally.
In this case, in line 22, the while loop
is initiated with a value of one,
which has passed as an argument
to the int parameter n in line 27.
I'm initializing a flag to false,
and then I'm starting the loop.
While not flagged, in other words while flag
is still false, print the value of n,
in this case will be one, increment n by one,
and then test is n bigger or equal to 10.
In this case not, because n is currently two.
So the if condition doesn't set the flag.
The execution returns to line 29,
and we continue to print n, increment n,
it's now three, and so on.
When n reaches the value 10 flag is switched
to true, and when the execution turns
back to line 29 the condition no longer holds,
which means flag is now true, the loop stops,
and the program ends.
Let's test this and then we'll look
at the other function.
Indeed, you see the numbers one through nine
have printed and the while loop have stopped.
You could say that in this example
we could have used a for loop
to print the numbers one to nine.
I just wanted to show you that you have
always options, think creatively.
It is possible in many cases to use
one loop to emulate the behavior
of another loop structure.
In this case, indeed, it might have been
more appropriate to use a for loop
if you knew exactly how many times you need
to iterate and all the conditions were fixed.
So this is a slightly different situation
which a flag and a condition for an if
were working together.
I just wanted you to get the idea
that you have now an arsenal of tools.
Pick and choose the most appropriate tools
to make decisions to create loops as you see fit,
and it's more appropriate for a situation at hand.
I'm now going to comment this function,
and instead, we're going to look at
another function called getInputData.
The InputData, that's something slightly different.
We're going to ask a user to enter a letter,
we will print the letter back to the user,
and as long as the letter does not equal x
in line 39 the loop will continue.
The loop will only stop if the user enters x.
Let's see how this works.
I'm going to type a, we entered a,
I'm going to type d, g.
The loop continues.
I can type all kind of letters.
There's absolutely nothing to stop it
short of shutting down the computer,
the program, or typing x.
So this was a great instance where a for loop
clearly would not been appropriate because
I don't know in advance the number
of times I need to repeat the commands.
Therefore, a condition was the proper way
to go forward.
As a final detail, in case you haven't noticed,
the use of the flag equals true or false
was allowed because in line 14 and 15
I defined true or false to be equal one and zero
as we have seen in previous videos.