Author Posts

August 29, 2018 at 12:39 pm

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?

August 29, 2018 at 1:20 pm

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.

August 29, 2018 at 1:45 pm

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

August 29, 2018 at 2:19 pm

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

August 29, 2018 at 3:04 pm

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. 🙂

August 29, 2018 at 3:33 pm

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.

September 5, 2018 at 11:18 am

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

September 5, 2018 at 4:33 pm

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/