# Do While and Do Until

Apologies if this has been asked before but what are the differences between a Do While loop and a Do Until loop? I appreciate that one would evaluate while a condition is true and the other until a condition is false however these evaluations for these conditions could be swapped so what are the merits of each? Is one more efficient than the other?

You are right, They’re like inverses of each other
one would evaluate while a condition is true and the other until a condition is false

there is no merits and change in efficiency.

Many thanks @prasoon-karunan-v I’ve struggled to find any meaningful differences between the two and thought I was missing something.

You might get more responses from others which will have points that I have missed(if any)

As mentioned, they’re basically identical apart from inverting the boolean check at the end.

Worth noting that they are together just half of the While/Until loop possibilities.

```# always executes the loop once, checking condition at the tail of the loop before looping. Runs until condition evaluates to \$false.
do {script} while (condition)

# as above, but inverts the condition. Runs until condition evaluates to \$true.
do {script} until (condition)

# same as do..while, except it checks the condition before executing the loop. If the condition does not evaluate as \$true, the loop can be skipped entirely. Runs until condition evaluates to \$false.
while (condition) {script}

# as above, but inverts the condition. Runs until condition evaluates to \$true.
until (condition) {script} ```

It’s easiest to remember do loops as ‘do once, then check’, I think.

There are two types of do/while loops

• Exit condition(Do loops)
Exit condition loops checks condition at the very end of the loop and exits based on condition.
• Entry condition(While/Until loops)
Entry condition loops check the condition first and enter.

Hi Joel, Many thanks for your feedback which is most appreciated. However, the point that I was trying to understand was that the logic could be flipped in each instance of the loop.

A Do-While loop could be coded to process until a condtion was \$true and a Do-Until loop could be coded to process until a condition was \$false. I wanted to understand whether there were any reasons why one would be chosen over the other in terms of peformance or whether this was purely good coding practice.

Kind regards

Ben

Yes, the two conditional statements are very similar, and the logic can be flipped/manipulated to make them almost identical. However, the 2 exist for a reason, specifically based on the exit statement of the loop. This article gives a very good example of how the 2 differ and why one would be chosen over the other for performance reasons:

https://blogs.technet.microsoft.com/heyscriptingguy/2014/05/06/powershell-looping-understanding-and-using-dountil/