So let's talk about using Do‑While and Do‑Until as
part of a loop within PowerShell.
So the first thing to understand is what is a loop?
Well,
a loop is a programming and a scripting language construct that allows
you to process a sequence of instructions inside code.
It also allows iteration of several items or several times,
depending on how you're iterating, as long as the condition is met.
So, for example,
let's say you want to see the number of files in a
folder that can execute and say, go and get me the folder,
iterate through the folder, identify the items,
and then once you've identified all of them,
then stop.
Or it could be, count a certain amount and then write those values out.
Now there are specific loop types that are available to us.
The first one is what's referred to as a For loop.
So a For loop is known also as a For statement,
and it's typically used to iterate through a set of
commands a specified number of times.
You then also have what's called a While.
This repeats a command or a group of commands where the given condition is true.
Then, of course, we have the Do‑While.
So just like the While loop,
it will execute the block of statements while the condition evaluates to a true,
so do while this is true.
But there's a small difference.
That is the body of the Do‑While is executed first,
then the condition gets tested at the end,
whereas the Do‑Until have a similar syntax to the Do‑While
except the condition is evaluated first.
So think of that logic.
Iterate through these items, compare the value at the bottom,
or compare the value at the top that you've got,
and then continue.
We also then have what's referred to as a Foreach‑Object.
So this is a cmdlet, not a loop, but just like the Foreach statement,
which is another one,
it iterates through each object in a collection and performs operations.
Then we have what's called the Foreach statement, or the Foreach loop.
This is used to iterate through values in a collection of items,
for example, in an array, and then perform actions against each item.
And then, of course, we have an odd one, which is the Foreach() method.
So you can specify a collection or an array.
So let's say we create a variable called at variable
and populate it with numbers 1 to 10.
We can then use the Foreach() method,
which effectively is accessed by going to the end of the variable,
type in a period, and then using Foreach(),
and that can then go through and iterate as well.
Now,
we need to understand the difference between an entry and an exit
loop before we can choose which type to use.
So an entry loop tests the condition is checked before it enters into the loop.
So that's your first one.
The entry loop is, does this value do something?
If so, then continue the loop.
Whereas an exit loop is keep looping and then check the
value at the end and drop out that way.
So what does that look like?
Well, let's look at the difference between the two.
So I have a variable, which is number 1,
and then I have what's referred to as an entry loop,
so this is using a While loop.
So what you can see here, it says, While $number,
so 1, is less than 10, then perform your specific actions.
So in this instance,
it's going to just increment the number by one and keep
going until or while it's less than 10.
So if my number in the right‑hand side, which is $number++,
increments up to 10,
it'll then stop working at that point because it's no longer less than 10.
If we do the Do option, you can see it flips the syntax the other way around.
So it now says, here's my number, increment the number,
and while that implemented number is less than 10,
continue to happen.
Now you may look at that and think, well,
that's okay, it looks very similar,
but it can have completely different effects on the
PowerShell output that we're using.
Now the Do‑While loop is almost the same as a regular While loop.
It executes the block of statements while the condition returns true.
The Do‑While executes first, then the condition test is at the end of the loop.
Now for the Do‑While, the syntax can be very similar.
So you can see it'll say do something while this condition is met.
So if we have a variable again with a number 1,
you can see that it looks very similar to the Do‑While.
So Do $number, which would say number 1,
increment number 2 while that number that you've incremented is less than 10.
So that would then continue to run until that point.
So what are the differences between a While and a Do‑While loop?
Well, if we compare them, the While loop is an entry controlled loop,
so it comes in at the point here and then tests the condition
before the execution of the first loop or iteration.
The loop doesn't execute when the condition evaluates to false.
So, remember the less than 10?
Once it reaches 10, it's no longer less than, so the loop continues to execute.
And it doesn't require any other syntax to be able to make that work.
The Do‑While loop is an exit controlled loop.
It tests the condition after the execution of the first iteration.
So it will keep going down, and until it reaches that specific number,
it won't stop.
The loop executes at least once every single time.
So whereas the While loop can actually evaluate to false
immediately and then never execute, the Do‑While will always execute once.
It uses the Do keyword at the starting of the loop and then a
While keyword with a condition at the end.
Now the Do‑Until loop is very similar to a Do‑While loop.
The loop will repeat until the condition returns true,
and these are the opposite of a Do‑While loop.
Now that syntax is very similar.
Instead of it saying Do‑While, it's Do‑Until,
so do this command until this condition is met.
So if we use our standard variable, $number = 1,
and you can see we have a Do‑Until.
So, think how this works.
Do 1.
Increase the number by one until it's less than or
greater than or something else.
Now notice it would be different, so Do‑Until the number is less than 10.
Sounds like a strange way of doing it, but that's what I said.
Depending on the looping mechanism that you use will have different
effects on the output and what you want to achieve.
Now the Do‑While loop is an exit controlled loop.
It uses the While keyword for the condition and then
executes when the condition returns true.
The Do‑Until loop is an exit controlled loop.
It uses the until keyword and then continues execution until the condition returns true.