Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

Pre- and Post-Incrementing Do...While Loop in PowerShell

$
0
0

Summary: Microsoft Scripting Guy, Ed Wilson, talks about pre- and post-incrementing the Do...While loop in Windows PowerShell.

Hey, Scripting Guy! Question Hey, Scripting Guy! I think that I found a bug in Windows PowerShell. In fact, I am pretty sure that I have. It involves using the ++ increment operator. For example, I know that $a = $a + 1 is the same as using $a++ because the ++ operator adds one to the value stored in the $a variable. With this understanding, here is the command to reproduce the bug I found. First, my command that uses $a = $a + 1:

#Loop #1

$a=0

Do

{

    $a

} While (($a = $a + 1) -le 5)

The output from the previous command is 0,1,2,3,4,5.

Now here is my command that uses the ++ increment operator:

$a=0

Do

{

    $a

} While ($a++ -le 5)

The output from this command is 0,1,2,3,4,5,6.

So it seems that there is a bug in the ++ operator. Can you please let the Windows PowerShell team know about this? Thanks.

—AT

Hey, Scripting Guy! Answer Hello AT,

Microsoft Scripting Guy, Ed Wilson, is here. This morning, I was able to find some Irish steel cut oats, and a nice cup of Darjeeling tea for breakfast. I added a bit of lemon grass and a cinnamon stick to the tea, along with a pinch of peppermint. It was quite good.

I am sitting on the porch looking at a lovely deep-blue and clear sky, using my Surface Pro 3 to check email sent to scripter@microsoft.com. It is a great morning.

I came across your email, and I thought it was a great question. I love that you provided sample commands to let me know what you think is going on and what you expect to go on. First of all, let me tell you that this is by design, and it is not a bug. I know—you think it is what we always say, “It’s not a bug, it's a feature.” But in this situation, this happens to be the case.

Fire up the Windows PowerShell ISE

One of the great things about the Windows PowerShell ISE is that I can highlight a portion of code in a script, and I can then press F-8 (or I can press the little green arrow, not the big green arrow), and only that portion of code will run.

I copied your two samples into a script and did just that. First, I used the old-fashioned style of $a = $a + 1 (I mean really old-fashioned—such as I had to do back in the VBScript days). This script is shown here with the output:

Image of command output

Now AT, when I use your second example, I see that, in fact, I get an extra number added to the output:

Image of command output

The solution is to pre-increment not post-increment

The solution is really very simple. I want to pre-increment the value of $a. In this way, when the evaluation is done, the value of $a will be less than or equal to the number 5. This is also a feature of the way the Do…While loop works. To pre-increment, all I need to do is move where ++ appears. The following command reflects the change:

$a=0

Do

{

    $a

} While (++$a -le 5)

Note that in the previous command, the ++ occurs before the $a and not after it as in the earlier example. The following image illustrates this change and the output (which is now the same as the $a = $a + 1 example in the first loop):

Image of command output

AT, that is all there is to using the Do…While loop and pre-incrementing the variable instead of post- incrementing it.  Join me tomorrow when I will talk about more way cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy


Viewing all articles
Browse latest Browse all 3333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>