Is There a More Efficient Way to Write Nested While Loops?

Posted by Ryan on Stack Overflow See other posts from Stack Overflow or by Ryan
Published on 2010-05-22T18:37:20Z Indexed on 2010/05/22 18:40 UTC
Read the original article Hit count: 295

Filed under:
|
|

The code below shows nested while loops, but it's not the very efficient. Suppose I wanted to extend the code to include 100 nested while loops. Is there a better way to accomplish this task?

<?php 

$count = 1;

 $num = 1;
 $options=3;
 while ( $num <=$options ) 
    { 
echo "(".$num . ") "; 

    $num1 = 1; 
    $options1=3;
    while ( $num1 <=$options1 ) 
    { 
    echo "*".$num1 . "* "; 

        $num2 = 1; 
        $options2=3;
        while ( $num2 <=$options2 ) 
        { 
        echo "@".$num2 . "@ ";  

            $num3 = 1; 
            $options3=3;
            while ( $num3 <=$options3 ) 
            { 
            echo $num3 . " ";   
            $num3++; 
            $count++;
            }

        echo "<br />";
        $num2++; 
        }

    echo "<br />";
    $num1++; 
    }

echo "<br />";
$num++; 
} 
  echo $count;
 ?>

© Stack Overflow or respective owner

Related posts about php

Related posts about while-loops