...

View Full Version : Looping Through Exponents



azoundria
11-17-2012, 02:02 AM
This works fine as 1, 2, 3, 4, 5, 6, 7, 8, 9:


for ($i = 1; $i < 10; $i++) {
echo $i."<br />";
}

Why does this not work to go 1, 2, 4, 8?


for ($i = 1; $i < 10; $i<<1) {
echo $i."<br />";
}

tangoforce
11-17-2012, 02:38 AM
for ($i = 1; $i < 10; $i<<1)

You can't use <<.

You can only use ++ or -- :thumbsup:

tracknut
11-17-2012, 04:30 AM
Actually I think what you meant to do was:


for ($i = 1; $i < 10; $i<<=1) {
echo $i."<br />";
}


Dave

Fou-Lu
11-17-2012, 05:40 AM
Tracknut sums it up.
The post condition in the for loop must result in something that changes control. The increment and decrement operators are (at least I'm pretty sure) the only two in PHP that alter the state of the variable when applied. The other unary operators like boolean and bitwise nots (! and ~) only work on the returned result of the operation, and likewise the binary operations (ie $i << 1 or $i + 1) would require assignment back to the $i in order to alter it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum