0
//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 次迭代中可能会失败。

4

6 回答 6

0

您应该能够使用另一个变量并将 $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;
  }
}
于 2013-10-15T13:56:40.827 回答
0

只要记住一开始的状态,然后检查它:

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.  
  }
}
于 2013-10-15T13:57:27.733 回答
0

试试这个:

$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;
}
于 2013-10-15T13:57:32.173 回答
0
$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
  }
}
于 2013-10-15T14:03:01.240 回答
0

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.  
  }
}
于 2013-10-15T14:11:44.550 回答
0

由于只会评估其中一种情况,为什么不直接调用在 $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);
  }

}
于 2013-10-15T14:15:31.437 回答