Question

It seems this is a general question of programming logic, as this issue seems to arise in my code regardless of what language it's coded in.

Basically I have 2 nested for loops, inside a for loop. The purpose of these for loops is to enumerate all possible values between two sets of values.

The problem with the code is that is the second set of values contains a 0 the values won't enumerate.

Say for example we want to enumerate all values between 0,0,0 and 1,1,1, this works perfectly fine, as each nested loop is processed. However is we try to calculate between 0,0,0 and 0,1,0 the loop will not continue on to the next loop, instead it will exit the loop and continue on to the rest of the code.

for ($i1=$coords1[0]; $i1<=$coords2[0]; $i1++) { //if $coords2[0] = 0 loop will fail
  for ($i2=$coords1[1]; $i2<=$coords2[1]; $i2++) { //if $coords2[1] = 0 loop will fail
    for ($i3=$coords1[2]; $i3<=$coords2[2]; $i3++) {//if $coords2[2] = 0 loop will fail
      $blocks.= $i1.",".$i2.",".$i3."|";
    }
  }
}
return $blocks;

Full code: PHPFIDDLE

Was it helpful?

Solution

Okay, to copy this over from our chat, I believe this is the solution:

<?php
$set1 = explode(",", '1,0,1');
$set2 = explode(",", '1,1,0');

$allbetween = _allbetween($set1, $set2);
echo 'All Between: '.$allbetween.'<br/>';
$allcount = count(explode("|", $allbetween))-1;
echo 'Number Of Blocks: '.$allcount;

function _allbetween($coords1, $coords2) {
$blocks = "";

    for ($i=0; $i<=2; $i++) {
        if ($coords1[$i] > $coords2[$i]) {
            $tmp = $coords1[$i];
            $coords1[$i] = $coords2[$i];
            $coords2[$i] = $tmp;
        }
    }

for ($i1=$coords1[0]; $i1<=$coords2[0]; $i1++)
  {
        for ($i2=$coords1[1]; $i2<=$coords2[1]; $i2++)
            {
                for ($i3=$coords1[2]; $i3<=$coords2[2]; $i3++)
                    {
                        $blocks.= $i1.",".$i2.",".$i3."|";
                    }
            }
  }
return $blocks;
}
?>

DEMO HERE

The reason this works is that there is a swap loop at the beginning of your function, which swaps any of the three sets of values if the first is greater than the second. This ensures that all values "between" them can be calculated.

Edit: Fixing the demo link to the correct URL

OTHER TIPS

the break; will do. see more information in the link:

http://www.php.net/manual/en/control-structures.break.php

Your loops will never loop as you are setting each initial value to the end range value. Instead use:

for ($i1=0; $i1<=$coords2[0]; $i1++)
...
etc

In terms of fall-through, try the following:

for ($i1=0; $i1<=$coords2[0] + 1; $i1++)
...
etc

Here's the problem: Dumping out your two $coords arrays:

$coords1:

array(3) {
  [0]=>
  string(1) "0"
  [1]=>
  string(1) "0"
  [2]=>
  string(1) "1"
}

$coords2:

array(3) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "0"
  [2]=>
  string(1) "1"
}

On your first iteration:

   $coords1[0] => 1 
   $coords2[0] => 0

   1 <= 0 -> FALSE

so your outermost loop NEVER executes at all.

I think you are searching for

GOTO function

And for the

Continue function

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top