Do While and Do Until

Welcome Forums General PowerShell Q&A Do While and Do Until

This topic contains 7 replies, has 4 voices, and was last updated by

 
Inactive
3 months, 1 week ago.

  • Author
    Posts
  • #110249
    Ben

    Participant
    Points: 1
    Rank: Member

    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?

  • #110255

    Participant
    Points: 894
    Helping Hand
    Rank: Major Contributor

    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.

    • #110260
      Ben

      Participant
      Points: 1
      Rank: Member

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

  • #110272

    Participant
    Points: 894
    Helping Hand
    Rank: Major Contributor

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

  • #110275

    Participant
    Points: 297
    Helping Hand
    Rank: Contributor

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

    • #110279

      Participant
      Points: 894
      Helping Hand
      Rank: Major Contributor

      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.

    • #110983
      Ben

      Participant
      Points: 1
      Rank: Member

      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

  • #111040

    Inactive
    Points: 0
    Rank: Member

    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/

     

The topic ‘Do While and Do Until’ is closed to new replies.