in reply to Re^2: For loop trouble
in thread For loop trouble
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 }
|
|---|