But rather, it is evaluating its endpoints, and then constructing the list from that. That is the cause of your problem:$start = $a; $end = ++$a; for ($_ = $start; $_ <= $end; $_++) { ... }
You can witness a similar result from:for (++$x .. $x++ . $x) { ... } # starting at ++$x # ending at $x++ . $x # THE END HAS MODIFIED THE START
You see, the arguments to a function aren't copies of the variables, but rather aliases -- by modifying ONE of the $x's, you've modified the others as well.print ++$x, ++$x, ++$x; # 333
Knowing this, the output of this program makes sense to me:
Why is that? Well, the numerator is ++$x, and the denominator is $x--; but in subtracting 1 from $x in the denominator, we have now altered the numerator back to its original state!$x = 3; print ++$x/$x--; # 3/4 => 0.75
I've worked out a solution to your 1 .. 12 loop, then.
See if you can work out why the starting point is 1.for (++$x .. $x++ . $x--) { ... } # or for (++$....$.++.$.--) { ... }
In reply to Re: Re: Re: For loop problem
by japhy
in thread For loop problem
by srawls
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |