- In this video, we are going to look
at a slightly different form of while
called the do while loop.
Let's see what the difference is.
As you can see, this is to also a repeat until
some conditions are met with a major difference.
In the regular while, we tested the condition
in the beginning and prevented execution
if the condition was false.
In the do while, we execute the code at least once
and then we determine whether the condition holds or not.
So, that's the major difference.
Let's look at the specific example which
is the same example as before in
the previous video with the read page.
However, now, we are going
to read the next page at least once,
because at the beginning of the do,
there's nothing to prevent the do from starting.
Only after this cycle of the loop was completed once,
and the flag was or was not set to true,
only then we are testing the condition.
This is a very spoiled distinction but in some cases,
you may want to perform something once.
Sometimes you will want to prevent it
and it's up to you to decide which one is most appropriate.
Let's look at the code and we'll look at both
of those types of loops side by side and be able to compare.
In this code, we are looking at the same exact loop
as in the previous video with one exception.
I added a testVariable in line 30.
The testVariable is initialized to zero
and then in line 32 is being assigned a new value of 999.
And I'm doing this two ways.
First, I have a function called
getInputData1 which is a regular y loop
and then getInputData2, which is the same exact function,
except in line 46 through line 52,
I'm using the do while.
So, in the do while loop, line 46
doesn't test for any condition,
so, line 47 would be executed regardless of
the value of c which is the character I need to test.
Furthermore, I am forcing in line 44 c to equal x
which is the value that I want to use
of stopping the loop in line 52.
Notice that the condition in line 52
is only tested after the loop has executed one cycle,
lines 47 through line 51.
In the original y loop, the condition
was tested at the beginning in line 31,
and execution was prevented.
So, I'm not interested here in repeating
again the cycle of entering letters.
I want specifically to focus on the notion
that the while loop prevents execution
from being carried on.
The do while only prevents it once.
Let's put this to test and first,
we're only going to enable getInputData1,
so, we can see what happens.
Notice I wasn't even given a chance
to enter any type of input.
Let's look at the code.
I explicitly assign to c in line 29 the value of x,
which is my condition to start the loop.
In line 31, I'm testing whether c is equal or not to x,
and I'm starting the loop only when c does not equal to x.
Since c does equal to x, my test variable remains zero
as it being initialized in line 30 as a proof
that the y loop has not even started.
At the end of the y loop, I'm displaying the same
error message or feedback to the user.
You enter x and the loop exited,
and the testVariable is still zero
because the loop hasn't been started.
Let's do the same thing, clear the output.
I'm going to comment the getInputData1,
and activate getInputData2 and see what happens.
Now, I am being asked to enter a letter
because I am already on line 48.
This is the first time.
So, that's already a clue that my loop has started.
Let's see what happens if I start
typing some different letter.
I type a d.
So, my loop has started and only if I type again x,
the loop ended, and the testVariable is obviously 999.
So, even though I explicitly set c to equal x in line 44,
the do loop still started and was executed.
Therefore, you have to be very careful
when you use one loop or the other
as the program will only execute what
the programmer is asking it to do.