So let's go back into our PowerShell console,
we'll review how to use the do/while and do/until loops,
and then we'll look at how we implement those inside PowerShell.
So let's talk about the do/while and the do/until kind of flows and what
that looks like when we execute it here within PowerShell.
So the first thing we'll do is actually create a variable
called number and assign that with a number 1.
So that's going to be our starting one.
Now, to use the do/while, we're going to use the do command,
and then, of course, we have our script block.
Then, of course, we have our while command,
and then we have our script block of anything that we need to do.
So,
what I'm going to do is I'm actually going to do each piece, so we'll
say Do, and then we'll do our first one. Go down here.
And the first thing I want to do is just render the
number that will display the number to us.
And then, of course, because we're using this kind of process,
I can do number++, that will increment my number for me.
I can then close out that do loop.
Then I can say while, and then in regular brackets,
remember we're not using the squirrely brackets here for this one,
I can say number and then say is less than or equal to the number 10.
So that gives me my loop process.
So we can say do while so and so. So I'm going to press Enter now,
and you'll see it iterates and then literally just stops at number 10
because that was the comparison operator or the statement at the end
that will say keep doing this while that one is less than specific 10
number, or whatever that would be,
whether it's greater than, less than, equal to, it
doesn't really make a difference, but it runs at the end. Now what that means is,
of course,
is that it will keep iterating all the numbers here, and only once it's
increased the number and then hits this line will it ever execute.
Now what about if we change that and do a different statement?
So we're going to do the Do Until loop. So, same thing again,
we're going to do Do, switch statement, Until a specific thing.
So let's just change my code again.
So Do question, we'll do number so and so, then we'll increase the number again.
So we'll say number++, we'll close that Do piece out, and
then we'll say until the number is greater than 10, and then
we'll do Enter. Now what happens?
It basically runs until it gets bigger, and once it
goes past the bigger, it just stops.
Now, what would happen if we just clear that and redid our do statement here?
So number, number here Do Until and changed it to have
the same syntax which we had before,
which was less than or equal to. How many times would it iterate?
Well if we enter, notice it just keeps going and going,
I'm going to stop that from running.
So if we go back to our code for a second,
you can see it says do it Until the number is less than or equal to that.
And, of course, it just continues to iterate.
Now, what is the value of number?
Well if we go to number, it's 13290.
So, of course, when you're building something like this,
notice an important thing is to actually clear the values out.
So every time I would want to run,
I need to have a default value that I set this back to.
So if we go to our Do number,
number, and Until and then Enter that, it will stop at number 1.
So just a little kind of side note there to be careful that
when you're using loops of any description,
whether it's a do/until or a do/while and you're
repopulating or increasing a variable, if you need it to be reset,
you need to somehow have a reset option; otherwise, the next time it's called it begins at the number where you left off.