CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Looping Through Exponents (http://www.codingforums.com/showthread.php?t=282416)

azoundria 11-17-2012 02:02 AM

Looping Through Exponents
 
This works fine as 1, 2, 3, 4, 5, 6, 7, 8, 9:

PHP Code:

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

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

PHP Code:

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


tangoforce 11-17-2012 02:38 AM

Quote:

Originally Posted by azoundria (Post 1292748)
PHP Code:

  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:
Code:

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.


All times are GMT +1. The time now is 02:16 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.