有没有更有效的方法来编写嵌套的 While 循环?

发布于 2024-09-02 22:52:19 字数 806 浏览 5 评论 0原文

下面的代码显示了嵌套的 while 循环,但效率不是很高。假设我想扩展代码以包含 100 个嵌套 while 循环。有更好的方法来完成这项任务吗?

<?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;
 ?>

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;
 ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独木成林 2024-09-09 22:52:19

您可能应该调整您的算法以使用递归而不是嵌套的 while 循环。

You should probably adapt your algorithm to use recursion instead of a nested while loop.

陈甜 2024-09-09 22:52:19

如果循环需要嵌套,那么它就足够高效了。

正如其他人所说,递归将允许您更优雅地“嵌套”不同级别,并且嵌套级别可以动态控制,而不是硬编码到您的程序中 - 但如果您发现您的设计需要您嵌套100 个循环,那么你的设计几乎肯定是完全疯狂的。

(但是,在您给出的具体示例中,根本不需要任何循环(或递归)。由于所有循环的长度都是恒定的,您可以预先计算结果并仅回显单个常量字符串。我认为这是一个假设的示例而不是您希望编写的实际代码?)

It's perfectly efficient enough if the loops need to be nested.

As others have said, recursion would allow you to "nest" different levels within each other more elegantly, and the level of nesting could be controlled dynamically rather than being hard-coed into your program - but if you find your design requires you to nest 100 loops, then your design is almost certainly completely mad.

(However, in the specific example you give there is no need for any loops (or recursion) at all. As all the loops are of constant length, you can precalculate the result and just echo a single constant string. I presume this is a hyporthetical example rather than the actual code you wish to write?)

云裳 2024-09-09 22:52:19

您可以使用递归函数。

You could use recursive functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文