in reply to for loops

okay so just trying to understand the logic of these new commands (for me) for($i=1; $i<=100; $i++) okay so now I understand how this line is read and the syntax but if the line starts over at the end doesnt $i return to the value of 1? I know already that this code works obviously but howcome? in my brain it says it should be starting $i back to a value of $i=1 and the evaulating the rest again so I guess Im just not getting it any help??

Replies are listed 'Best First'.
Re: Re: for loops
by amarceluk (Beadle) on May 25, 2002 at 16:54 UTC
    $i++ is the equivalent of
    $i=$i+1;
    where the = is an operator. It's not just sitting there, like it does in math, saying "these two things are equal"; it tells Perl to make the two things equal. So here it's saying "take the current value of $i and add 1, and make that the new value of $i." Then the for loop continues with the new value of $i. This happens each time the loop runs, until $i reaches 100.

    _________
    "Abby-somebody. Abby-normal."
    Young Frankenstein
Re^2: for loops
by apotheon (Deacon) on Oct 11, 2004 at 01:02 UTC
    On second and following iterations, the for loop ignores the initial assignment of value to $i and uses the incremented value (or decremented, if you use a decrement operator instead of an increment operator) instead. The $i=1 assignment happens once, and once only, in for($i=1; $i<=100; $i++).

    - apotheon
    CopyWrite Chad Perrin