Windows Vista Forums

I don't understand this while loop
  1. #1


    Fred Jacobowitz Guest

    I don't understand this while loop

    I don't understand this;
    PS> $x=0;while ($x++ -lt 10) {$x}
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    PS>
    Why doesn't the loop stop at 9.

    On the other hand
    $x=0;while ($x+=1 -lt 10) {$x}
    never stops looping.

    Fred Jacobowitz





      My System SpecsSystem Spec

  2. #2


    Brandon Shell Guest

    Re: I don't understand this while loop

    For your first. I believe it is because your adding 1 before the write $x,
    thus its starting at 1 not 0.
    PS>$x = 0
    PS>$x++
    PS>$x
    1

    For the second: This is because its $x += 1 doesnt return anything. It just
    modifies the value of $x.
    PS> $x = 0
    PS> $x+=1
    PS> $x
    1

    man about_while would suggest
    PS> $x=0;while ($x -lt 10) {$x;$x++}
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9

    p.s. you are getting back 10.

    --
    Brandon Shell
    ---------------
    Stop by my blog some time
    Blog: http://www.bsonposh.com/
    PSH Scripts Project: www.codeplex.com/psobject
    --------------------------------------

    "Fred Jacobowitz" <fjacobow@OptOnline.Net> wrote in message
    news:Ot%23X0JwZHHA.4856@TK2MSFTNGP03.phx.gbl...
    >I don't understand this;
    > PS> $x=0;while ($x++ -lt 10) {$x}
    > 1
    > 2
    > 3
    > 4
    > 5
    > 6
    > 7
    > 8
    > 9
    > 10
    > PS>
    > Why doesn't the loop stop at 9.
    >
    > On the other hand
    > $x=0;while ($x+=1 -lt 10) {$x}
    > never stops looping.
    >
    > Fred Jacobowitz
    >
    >



      My System SpecsSystem Spec

  3. #3


    Jacques Barathon [MS] Guest

    Re: I don't understand this while loop

    "Brandon Shell" <tshell.mask@gmail.com> wrote in message
    news:u76ckqwZHHA.4772@TK2MSFTNGP05.phx.gbl...
    > For your first. I believe it is because your adding 1 before the write $x,
    > thus its starting at 1 not 0.
    > PS>$x = 0
    > PS>$x++
    > PS>$x
    > 1


    To be even more precise, "$x++ -lt 10" compares first and then adds one to
    $x.

    PS> $x = 0
    PS> $x++ -lt 1
    True
    PS> $x
    1

    >
    > For the second: This is because its $x += 1 doesnt return anything. It
    > just modifies the value of $x.
    > PS> $x = 0
    > PS> $x+=1
    > PS> $x
    > 1


    As shown by this:

    PS> $x = 0
    PS> $x+=1 -lt 1
    PS> $x
    1

    You will note that as opposed to the above example with $x++ the comparison
    on $x+=1 doesn't return any result.

    Jacques


      My System SpecsSystem Spec

  4. #4


    Marcel J. Ortiz [MSFT] Guest

    Re: I don't understand this while loop

    > To be even more precise, "$x++ -lt 10" compares first and then adds one to
    > $x.


    Yep. To add first and THEN compare you could use: "++$x -lt 10"

    PS> $x = 0
    PS> ++$x -lt 1
    False
    PS> $x
    1

    I think its more common to use the "for" statement for this pattern ->
    "$x=0;while ($x++ -lt 10)"

    for($x = 0; $x -lt 10; $x++)
    {
    $x
    }


    "Jacques Barathon [MS]" <jbaratho@online.microsoft.com> wrote in message
    news:%23G1gozwZHHA.5080@TK2MSFTNGP04.phx.gbl...
    > "Brandon Shell" <tshell.mask@gmail.com> wrote in message
    > news:u76ckqwZHHA.4772@TK2MSFTNGP05.phx.gbl...
    >> For your first. I believe it is because your adding 1 before the write
    >> $x, thus its starting at 1 not 0.
    >> PS>$x = 0
    >> PS>$x++
    >> PS>$x
    >> 1

    >
    > To be even more precise, "$x++ -lt 10" compares first and then adds one to
    > $x.
    >
    > PS> $x = 0
    > PS> $x++ -lt 1
    > True
    > PS> $x
    > 1
    >
    >>
    >> For the second: This is because its $x += 1 doesnt return anything. It
    >> just modifies the value of $x.
    >> PS> $x = 0
    >> PS> $x+=1
    >> PS> $x
    >> 1

    >
    > As shown by this:
    >
    > PS> $x = 0
    > PS> $x+=1 -lt 1
    > PS> $x
    > 1
    >
    > You will note that as opposed to the above example with $x++ the
    > comparison on $x+=1 doesn't return any result.
    >
    > Jacques



      My System SpecsSystem Spec

  5. #5


    Fred J. Guest

    Re: I don't understand this while loop

    Thank you for your responses. I completely understand it now.
    Actually, Bruce has the following on page 23 of his book;
    ps > (1) $i=0
    ps >(2) while ($i++ -lt 10) { if ($i % 2) {"$i is odd"}}
    1 is odd
    3 is odd
    5 is odd
    7 is odd
    9 is odd
    ps (3) >

    Normally, I would increment the counter in the statement block.
    However, this example had me scratching my head.

    Fred Jacobowitz


      My System SpecsSystem Spec

  6. #6


    Ryan Milligan Guest

    Re: I don't understand this while loop

    Actually, the += operator *does* return the value. The confusion is related
    to operator precedence. Take a look at the following:

    PS> $x = 0
    PS> $x+=1 -lt 10
    PS> $x
    1
    PS> $x+=10 -lt 10
    PS> $x
    1

    What's happening here is that += has a higher operator precedence than -lt,
    so you're getting "$x += (1 -lt 10)", not "($x += 1) -lt 10" as you might
    expect. (1 -lt 10) interpreted as an integer comes out as 1 (just as (10 -lt
    10) interpreted as an integer is zero, so $x doesn't change) , so the end
    result of the statement is that $x will keep getting incremented until it
    loops all the way around and results in a zero, which is the only integer
    that will be considered "false" when interpreted as a boolean. As long as
    the number to the right of the += is less than 10, $x will always be
    incremented by one.

    Hope this helps clear up some of the confusion.

    -- Ryan Milligan

    "Jacques Barathon [MS]" <jbaratho@online.microsoft.com> wrote in message
    news:%23G1gozwZHHA.5080@TK2MSFTNGP04.phx.gbl...
    > "Brandon Shell" <tshell.mask@gmail.com> wrote in message
    > news:u76ckqwZHHA.4772@TK2MSFTNGP05.phx.gbl...
    >> For your first. I believe it is because your adding 1 before the write
    >> $x, thus its starting at 1 not 0.
    >> PS>$x = 0
    >> PS>$x++
    >> PS>$x
    >> 1

    >
    > To be even more precise, "$x++ -lt 10" compares first and then adds one to
    > $x.
    >
    > PS> $x = 0
    > PS> $x++ -lt 1
    > True
    > PS> $x
    > 1
    >
    >>
    >> For the second: This is because its $x += 1 doesnt return anything. It
    >> just modifies the value of $x.
    >> PS> $x = 0
    >> PS> $x+=1
    >> PS> $x
    >> 1

    >
    > As shown by this:
    >
    > PS> $x = 0
    > PS> $x+=1 -lt 1
    > PS> $x
    > 1
    >
    > You will note that as opposed to the above example with $x++ the
    > comparison on $x+=1 doesn't return any result.
    >
    > Jacques




      My System SpecsSystem Spec

  7. #7


    Jacques Barathon [MS] Guest

    Re: I don't understand this while loop

    "Ryan Milligan" <Ceiled@hotmail.com> wrote in message
    news:7didnUUER6zL3WfYnZ2dnUVZ_q-vnZ2d@comcast.com...
    > Actually, the += operator *does* return the value. The confusion is
    > related to operator precedence. Take a look at the following:
    >
    > PS> $x = 0
    > PS> $x+=1 -lt 10
    > PS> $x
    > 1
    > PS> $x+=10 -lt 10
    > PS> $x
    > 1
    >
    > What's happening here is that += has a higher operator precedence
    > than -lt, so you're getting "$x += (1 -lt 10)", not "($x += 1) -lt 10" as
    > you might expect. (1 -lt 10) interpreted as an integer comes out as 1
    > (just as (10 -lt 10) interpreted as an integer is zero, so $x doesn't
    > change) , so the end result of the statement is that $x will keep getting
    > incremented until it loops all the way around and results in a zero, which
    > is the only integer that will be considered "false" when interpreted as a
    > boolean. As long as the number to the right of the += is less than 10, $x
    > will always be incremented by one.


    Great input, you're absolutely right. So the initial example provided should
    be rewritten as:

    PS> $x=0; while (($x+=1) -lt 10) {$x}
    1
    2
    3
    4
    5
    6
    7
    8
    9

    Thanks,
    Jacques


      My System SpecsSystem Spec

I don't understand this while loop problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
for ... loop through file collection instead of for each ... loop Ranah van Rijswijk VB Script 2 21 Feb 2010
I don't understand Jill Vista mail 2 21 Jul 2008
This one I understand? gurbao PowerShell 12 12 Nov 2007
I don't understand! b11_ Vista General 2 11 Nov 2007
I dun understand. Erick Vista General 9 10 May 2007