简体   繁体   English

为什么这个“if”条件不会打破循环?

[英]Why does this "if" condition not break the loop?

I have a for loop with a conditional block that breaks the loop.我有一个带有条件块的for循环,可以中断循环。

However, the loop never breaks.但是,循环永远不会中断。
This is my code:这是我的代码:

for($counter=1;$counter>5 or $counter<=100;$counter*=2){

    echo $counter."<br>";
    if($counter==500){
      break;
    }
}

Because counter is never exactly 500. It will be 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, ...因为 counter 永远不会正好是 500。它将是 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, ...

You probably want你可能想要

if($counter>=500){
  break;
}

but I can't say for sure because I have no idea what you are doing.但我不能肯定,因为我不知道你在做什么。

It looks like $counter will never be equal to 500 to satisfy the conditional if statement.看起来$counter永远不会等于 500 来满足条件 if 语句。 You should change你应该改变

if($counter==500){
  break;
}

to

if($counter>500){
  break;
}

Because your break logic is wrong:因为你的中断逻辑是错误的:

loop calculation => next value循环计算 => 下一个值

1 => 1
1*2 => 2
2*2 => 4
4*2 => 8
8*2 => 16
16*2 => 32
32*2 => 64
62*2 => 128
128*2 => 256
256*2 => 512
512*2 => 1024

To break your loop change your打破你的循环改变你的

if($counter==500){
  break;
}

to

if($counter>500){
  break;
}

Like others have said, $counter will never be exactly 500, as it goes through the powers of two (1, 2, 4, 8, 16, 32, 64, 128, 256, 512, etc).就像其他人所说的那样, $counter永远不会正好是 500,因为它会经历 2 的幂(1、2、4、8、16、32、64、128、256、512 等)。

The fix for this is simple: you use >500 instead of ==500解决这个问题很简单:你使用>500而不是==500


However, in addition, this code has general issues that I would like to point out to you, since it seems like you are learning to program. 但是,此外,这段代码还有一些一般性问题,我想向您指出,因为您似乎正在学习编程。

Your for loop condition is $counter > 5 or $counter <=100 .您的for循环条件是$counter > 5 or $counter <=100 This covers all numbers, since all numbers are either more than 5 or less than (or equal to) 100.这涵盖了所有数字,因为所有数字要么大于 5,要么小于(或等于)100。

This means that the for loop will run forever as the check will always be true.这意味着for循环将永远运行,因为检查将始终为真。

You then have a manual check after each loop that checks if $counter is greater than 500. This can be moved up to the condition with minor tinkering, as the for loop checks the condition after every loop (same as your manual check).然后,您在每个循环之后进行手动检查,以检查$counter是否大于 500。这可以通过稍加修改移动到条件,因为for循环在每个循环后检查条件(与您的手动检查相同)。

The new condition becomes "the old condition AND NOT the manual condition" (boolean AND).新条件变为“旧条件而不是手动条件”(布尔值 AND)。 Since the old condition is always true, the new condition becomes TRUE & !$counter > 500 , which is the same as $counter <= 500 .由于旧条件始终为真,新条件变为TRUE & !$counter > 500 ,这与$counter <= 500


I hope I've explained this well enough but if you have any confusion just ask me in the comments to this answer and I'll be happy to clear anything up. 我希望我已经很好地解释了这一点,但如果您有任何困惑,请在对此答案的评论中问我,我很乐意解决任何问题。

If I understand your goal correctly, this should produce what you want.如果我正确理解你的目标,这应该会产生你想要的。

for($counter = 8; $counter <= 500; $counter *= 2) {
    echo $counter."<br>";
}

This will echo 8, 16, 32, 64, 128, 256 and exit the loop because the next value, 512, will not be <= 500.这将回显 8, 16, 32, 64, 128, 256 并退出循环,因为下一个值 512 不会 <= 500。

Another option, if you need to start at 1, would be:如果您需要从 1 开始,另一种选择是:

for($counter = 1; $counter <= 500; $counter *= 2) {
  if($counter < 6 ) {
    continue; // this will jump back to the start of the for loop
  } else {
    echo $counter."<br>";
  }
}

In your original code: If you start with the $counter = 1, the values of 2, 4, 8, 16, 32, & 64 will all return true because of the <= 100 condition.在您的原始代码中:如果您以 $counter = 1 开始,由于 <= 100 条件,2、4、8、16、32 和 64 的值都将返回 true。 If you include the $counter > 5 condition, every value beyond 5 will be true (8, 16, 32, ...to infinity).如果包含 $counter > 5 条件,则超过 5 的每个值都将为真(8、16、32、...到无穷大)。 Since the counter doubles each time, it will never equal 500 (it will be 64, 128, 256, 512, 1024...) so the condition triggering the break will never happen.由于计数器每次都加倍,它永远不会等于 500(它将是 64、128、256、512、1024...),因此触发中断的条件永远不会发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM