問題描述
也許我的問題看起來很初級,但實際上并非如此.我聲稱我可以通過 while()
使用 for()
完成一切,反之亦然.那么當有 for()
時 while()
真的有用嗎?我需要一個例子,我可以通過 for()
做到這一點,而我不能通過 while()
做到這一點.有?我認為沒有......!
Maybe my question seems primary, But actually it is not. I claim that I can do everything is done with for()
by while()
, And conversely. So really when while()
is useful when there is for()
? I need to a example that I can do that by for()
and I can not do that by while()
. There is? I think there is not ...!
結構如下:
for (init counter; test counter; increment counter) {
code to be executed;
}
init counter;
while (test counter) {
code to be executed;
increment counter;
}
看到了嗎?它們完全一樣,現在我想知道為什么 php 兩者都有?
see? they are exactly the same, Now I want to know why php has both of them?
推薦答案
區別在于 do while 循環至少執行一次,因為它在退出時檢查循環條件.while 是入口控制循環,而 do while 是出口控制循環.而在 do while 循環中,它將進入循環,然后檢查條件.
The difference is that the do while loop executes at least once because it checks for the loop condition while exiting. While is a entry controlled loop and do while is a exit control loop. Whereas in do while loop it will enter the loop and will then check for the condition.
while 循環 - 用于循環直到滿足條件并且不確定代碼應該循環多少次
while loop - used for looping until a condition is satisfied and when it is unsure how many times the code should be in loop
for 循環 - 用于循環直到滿足條件,但在您知道代碼需要循環多少次時使用
for loop - used for looping until a condition is satisfied but it is used when you know how many times the code needs to be in loop
do while 循環 - 在檢查 while 條件之前執行一次循環內容.
do while loop - executes the content of the loop once before checking the condition of the while.
這篇關于while() 和 for() 有什么區別?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!