2013-10-15 101 views
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。算法檢查自從循環開始以來變量是否發生了變化PHP

對於迭代1,我可以把if($ i == 0){,但是在迭代2和3中有可能失敗。

+0

添加另一個計數器,你也增加太多,不僅$ i? – bwoebi

+0

因爲你有'i = 0;'作爲你的循環的第一行,你在哪裏增加'$ i'? – putvande

+0

在這裏寫下你的確切代碼 –

回答

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; 
    } 
} 
+0

謝謝,因爲這個! – user2278120

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. 
    } 
} 
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; 
} 
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 
    } 
} 
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. 
    } 
} 
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); 
    } 

} 
相關問題