javascript - if(){do{};while();} 和 while{} 完全一样吗

标签 javascript while-loop do-while

if(a)
{
    do
    {
        b();
    }
    while(a);
}

一模一样

while(a)
{
    b();
}

?

最佳答案

它们是相同的,我将提供一个示例,您可能实际上想要使用“Do-While”而不是 while 循环。

do{
    x = superMathClass.performComplicatedCalculation(3.14);
}
while (x < 1000);

相对于

x = superMathClass.performComplicatedCalculation(3.14);
while ( x < 1000);
{
      x = superMathClass.performComplicatedCalculation(3.14);
}

使用 Do-While 的参数如上所示。假设行 x = superMathClass.performComplicatedCalculation(3.14); 不仅仅是一行代码,而是 25 行代码。如果您需要更改它,请在 do while 中只更改一次。在 while 循环中,您必须更改它两次。

我确实同意,你应该避开它们,但也有支持它们的论点。

关于javascript - if(){do{};while();} 和 while{} 完全一样吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11102374/

相关文章:

javascript - 如何将 .js 文件放入 html 中

javascript - 更改多个子元素的 HTML(这个 jquery 选择器的 JS 版本是什么?)

php - mysql查询熨烫网页上的一个搜索结果?

linux - 在 while 循环之外时,无法读取从 while 循环中存储的变量

java - 二进制到十进制转换和 while 语句的算法

javascript - 在第二个数组中排序单词

php - 设置临时打印文件的属性

r - 根据 R 中的设置限制分配值

c++ - Do-While(false) block 与返回的效率

c# - GOTO 与 DO WHILE 的区别