【问题标题】:Algorithm to check if variable changed since start of the loop PHP检查自循环PHP开始以来变量是否更改的算法
【发布时间】:2013-10-15 13:53:03
【问题描述】:
//Initialize
i=0;
foreach(runs 8 times) {
  if(some condition that sometimes happens) {
    $i = $i + 3;
  } else if (some other condition that sometimes happens) {
    //Do nothing with i
  } else if(some condition that sometimes happens) {
    $i = $i -4;
  }

  if(what condition do i put here to check if $i changed from the start of the loop?) {
    //nothing changed, do action 1
  } else {
    //Else something changed and do action 2.  
  }
}

大家好,我确信这很明显,但是我很难使用这个算法,而且每次我都需要确保 $i 与开始时不同循环并根据该区别执行操作 1 或操作 2。

对于第 1 次迭代,我可以输入 if($i == 0) {,但在第 2 次和第 3 次迭代中可能会失败。

【问题讨论】:

  • 添加另一个您也增加的计数器,而不仅仅是 $i?
  • 你在哪里增加$i,因为你有i=0;作为你循环的第一行?
  • 在这里写下你的确切代码
  • 一开始你正在初始化$i=0,你可以很容易地检查$i的值是否为零。
  • 为什么会失败? $i 在每次迭代时重置。并且由于 if 在 for 循环内,它应该可以正常工作

标签: php algorithm variables loops compare


【解决方案1】:

您应该能够使用另一个变量并将 $i 的值保存在该变量中(如果它已更改)。然后,当您检查更改时,您始终可以将该变量与 $i 进行比较。

我认为你不应该在循环中包含 $i。

$previousValue = null;
$i=0;
foreach(runs 8 times) {
  //Initialize
  if(some condition that sometimes happens) {
    $i = $i + 3;
  } else if (some other condition that sometimes happens) {
    //Do nothing with i
  } else if(some condition that sometimes happens) {
    $i = $i -4;
  }

  if($previousValue == $i) {
    //nothing changed, do action 1
  } else {
    //Else something changed and do action 2.  
    $previousValue = $i;
  }
}

【讨论】:

  • 谢谢,正因为如此!
【解决方案2】:

只记得一开始的状态,然后再检查:

foreach(runs 8 times) {
  //Initialize
  i=0;
  oldi = i;
  if(some condition that sometimes happens) {
    $i = $i + 3;
  } else if (some other condition that sometimes happens) {
    //Do nothing with i
  } else if(some condition that sometimes happens) {
    $i = $i -4;
  }

  if(i != oldi) {
    //nothing changed, do action 1
  } else {
    //Else something changed and do action 2.  
  }
}

【讨论】:

    【解决方案3】:

    试试这个:

    $previous = 0;
    foreach(runs 8 times) {
    //Initialize
    $i = 0;
    if(some condition that sometimes happens) {
        $i = $i + 3;
    } else if (some other condition that sometimes happens) {
    //Do nothing with i
    } else if(some condition that sometimes happens) {
        $i = $i -4;
    }
    
    
    if($i != $previous) {
        //nothing changed, do action 1
    } else {
        //Else something changed and do action 2.  
    }
    $previous = $i;
    }
    

    【讨论】:

      【解决方案4】:
      $ichanged = false;
      i=0;
      foreach(runs 8 times) {
        //Initialize
      
        if(some condition that sometimes happens) {
          $ichanged = true;
          $i = $i + 3;
        } else if (some other condition that sometimes happens) {
          //Do nothing with i
        } else if(some condition that sometimes happens) {
          $ichanged = true;
          $i = $i -4;
        }
      
        if($ichanged == false) {
          //nothing changed, do action 1        
        } else {
          //Else something changed and do action 2.  
          $ichanged = false;//RESET for next iteration
        }
      }
      

      【讨论】:

        【解决方案5】:

        很简单,只需在循环中再添加一个变量,并在每次循环迭代时将其初始化为 i

        试试这个代码:

        //Initialize
        i=0;
        j=0;
        foreach(runs 8 times) {
          j=i;
          if(some condition that sometimes happens) {
            $i = $i + 3;
          } else if (some other condition that sometimes happens) {
            //Do nothing with i
          } else if(some condition that sometimes happens) {
            $i = $i -4;
          }
        
          if(j==i) {
            //nothing changed, do action 1
          } else {
            //Else something changed and do action 2.  
          }
        }
        

        【讨论】:

          【解决方案6】:

          既然只评估其中一种情况,为什么不直接调用在 $i 立即发生变化时执行某项操作的函数?

          $i = 0;
          foreach(/*runs 8 times*/) {
          
            if(/*some condition that sometimes happens*/) {
              iChanged($i, $i + 3);
              $i -= 3;
            } elseif (/*some other condition that sometimes happens*/) {
              iDidntChange($i);
            } elseif(/*some condition that sometimes happens*/) {
              iChanged($i, $i - 4);
              $i -= 4;
            } else {
              iDidntChange($i);
            }
          
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-05-25
            • 1970-01-01
            • 2017-08-16
            • 2011-05-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-08-16
            相关资源
            最近更新 更多