PowerShell

PowerShell: Do-While vs. Do-Until vs. While

Understanding the differences between a do-while, do-until and while loop could be confusing. Is it the same? Why then two different techniques?

In this blog post I will show the differences. But first, we need to take a look at the basics.

Do-Until

Do-Until loops will do something until a condition becomes true.

Note the equal condition for this loop $number -eq ‘0’.

For instance, the code below will run until you press 0 on your keyboard. Then and only then the condition becomes true (0).


do {

$number=Read-Host "Enter number (Press 0 to Quit)"

}

until ($number -eq '0')

Do-While

Do-While loops will do something while a condition is true.

Note the not equal condition for this loop $number -ne ‘0’.

The code below will run until you press 0 on your keyboard, in other words, the script will be executed as long as (while) the condition doesn’t become true (0).


do {

$number=Read-Host "Enter number"

}

while ($number -ne 0)

While

While loops are similar to Do-While loops. But this type of loop performs the check first, while the Do-While loop performs the check after the first pass.

At the penultimate pass, $i is 9, so the last pass will be done, but then no more.


while ($i -ne 10) {

$i++
Write-Host $i

}

Unbenannt.PNG

Do-Until vs. Do-While vs. While

The following example shows the differences that are not significant at first glance, but they are.

The first loop will run only once because it will only run while (as long as) $i is greater than 10. This is not the case. Therefore the loop will stop after the first run when the check is processed.

The second loop will run endless because $i will never become greater than 10 and therefore the statemtent is true.


$i = 10

do {

Write-Output 'This is executed once.'

}

while ($i -gt 10)

do {

Write-Output 'This is done over and over again!'

}

until ($i -gt 10)

Unbenannt.PNG

Last but not least the while loop without the do statement. This loop will never run because the check is done before the loop starts.


$i = 10

while ($i -gt 10) {

Write-Output 'This will never be executed.'

}

Unbenannt.PNG

I hope I could bring some light into the dark when it comes to looping with the do statement.

See you next time with PowerShell!

Categories: PowerShell

Tagged as: ,

3 replies »

  1. Can you provide real life example where above differences can make major difference ? I still have difficulty figuring out which one to use.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.