【问题标题】:while-loop will execute when there is a 0 in the while condition? [duplicate]当while条件中有0时会执行while循环吗? [复制]
【发布时间】:2018-08-13 01:55:21
【问题描述】:

我们来看示例代码:

 var limit = 3;
 while(limit--){
   console.log(limit);
   if(limit){
     console.log("limit is true");
   }else{
     console.log("limit is false")
   }
 }

输出将是:

2
"limit is true"
1
"limit is true"
0
"limit is false"

有一个0,表示最后一次while条件为假。为什么最后一次循环会执行?

【问题讨论】:

  • 会先检查(limit还是1)再递减。
  • 看看--在var限制的哪一边..它在右边,意思是比较后会递减。如果你想之前你需要--limit
  • 原因是操作执行后后减值减小
  • 谢谢大家!我明白了。

标签: javascript while-loop


【解决方案1】:

limit-- 这是一个后期减量。因此,当limit 位于1 时,它会解析为true 内的while,然后它实际上是递减的,因此当您打印它时它是0

【讨论】:

    【解决方案2】:

    在几种语言中,0 值被视为false 值。

    同样limit-- 在被while 条件评估后进行递减。

    【讨论】:

      【解决方案3】:
      while(limit--) 
      

      等于

      while(limit){
          limit = limit -1;
      }
      

      所以 while 表达式中的限制是从 3 到 1, 而在大括号中,limit 是从 2 到 0,所以 'limit is false' 将被执行。 如果您希望 'limit is false' 不会执行,您可以将 limit-- 替换为 --limit

      【讨论】:

        【解决方案4】:

        这是查看它的好方法。我们正在查看一个数字,并查看它是否高于 0,如果是,我们将带走 1,然后再次运行循环,否则我们将停在那里。

        let three = 3, two = 2, one = 1, zero = 0;
        
        console.log('3--: ' + (three-- > 0));
        console.log('2--: ' + (two-- > 0));
        console.log('1--: ' + (one-- > 0));
        console.log('0--: ' + (zero-- > 0));

        【讨论】:

          【解决方案5】:

          当您使用后增量时,它将首先检查条件,然后对该变量执行减量,因此这里的 while(limit--) 将被视为 while(limit)。

          试运行:

          var limit = 3;
          

          第一次:

          while(limit--){     //check limit while limit = 3 returns true and then decrement by 1 
             console.log(limit);  //so now print limit as 2
             if(limit){  //check limit while limit = 2
               console.log("limit is true");  //so now print this one
             }else{
               console.log("limit is false")
             }
           }
          

          第二次:

          while(limit--){     //check limit while limit = 2 returns true and then decrement by 1 
             console.log(limit);  //so now print limit as 1
             if(limit){     //check limit while limit = 1
               console.log("limit is true");  //so now print this one
             }else{
               console.log("limit is false")
             }
           }
          

          第三次:

          while(limit--){     //check limit while limit = 1 returns true and then decrement by 1 
             console.log(limit);  //so now print limit as 0
             if(limit){     //check limit while limit = 0
               console.log("limit is true");  
             }else{
               console.log("limit is false")  //so now print this one
             }
           }
          

          第四次:

          它不会进入while循环,因为现在limit = 0

          【讨论】:

            猜你喜欢
            • 2020-07-21
            • 1970-01-01
            • 2017-12-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多