【发布时间】: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