in reply to For loop trouble

for ($i=500;$i<=900;$i+=100)

Replies are listed 'Best First'.
Re^2: For loop trouble
by cheech (Beadle) on Jun 13, 2009 at 16:55 UTC
    I was taught that for structure was like this: (starting value, ending value, increment). But I infer from your response that it must be: (starting value, continue while this is true, increment)?
      It's for (EXPR1; EXPR2; EXPR3) {...}. Note that there are semi-colons between the expressions, not commas.

      The first expression is run before entering the loop. Before each iteration the second expression is run; if false, the loop terminates. After each iteration, the third expression is run. For further details, see man perlsyn.

        Note that there are semi-colons between the expressions, not commas.

        In this case, the OP has constructed a foreach loop iterating $_ over a list of three elements: $i=500, $i=900 , $i += 100, which evaluates to 500, 900, 1000 and $i set to 1000 for ALL iterations of the loop.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        Ugh the carelessness.. Thank you, I just needed to realize semi colons were to be used.

      Perls three-argument for loop is exactly like in C: initializer, test, and counting. See also http://en.wikipedia.org/wiki/For_loop#Three-expression_for_loops. Other languages may implement for loops differently, notably BASIC and its derivates often have constructs like for i=1 to 100 step 3 (counting up from 1 with increments to 3 until 100 is reached or exceeded) or even for i=100 downto 1 step 3 (counting down in steps of 3, starting at 100, stopping when 1 is reached or exceeded). Other languages have even stranger for loops, see the wikipedia article.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      I was taught that for structure was like this: (starting value, ending value, increment). But I infer from your response that it must be: (starting value, continue while this is true, increment)?

      Won't hurt you to read the docs...

      What's wrong:

      • The first expression has nothing to do with a "starting value". (Starting value of what??? It's not like C-style loops have counters.)

      • The last expression has nothing to do with an "increment". (Incrementing what??? It's not like C-style loops have counters.)

      What it really means

      • The first expression (if specified) is evaluated unconditionally before the loop is started.

      • The second expression is evaluated at the start of every loop pass (incl the first). If an expression is specified and it evaluates to something false, the look exits.

      • The third expression (if specified) is evaluated at the end of every loop pass (even if next is called).

      A common usage:

      for (my $i = 0; $i < $n; ++$i) { ... }

      The above is equivalent to the following, except $i is scoped to the loop.

      my $i = 0; while ($i < $n) { ... } continue { ++$i }