php while() 和 for() 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32562212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between while() and for()?
提问by Shafizadeh
Maybe my question seems primary, But actually it is not. I claim that I can do everythingis 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()
由完成的while()
,反之亦然。那么真的什么时候while()
有用for()
呢?我需要一个例子,我可以做到这一点,for()
而我不能做到这一点while()
。有?我认为没有......!
Here is the structure:
这是结构:
for (init counter; test counter; increment counter) {
code to be executed;
}
init counter;
while (test counter) {
code to be executed;
increment counter;
}
see? they are exactly the same, Now I want to know why phphas both of them?
看?它们完全一样,现在我想知道为什么php有它们两个?
回答by Insane Skull
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.
不同之处在于 do while 循环至少执行一次,因为它在退出时检查循环条件。while 是入口控制循环,而 do while 是出口控制循环。而在 do 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
while 循环- 用于循环直到满足条件以及不确定代码应该循环多少次
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
for 循环- 用于循环直到满足条件,但在您知道代码需要循环多少次时使用
do while loop- executes the content of the loop once before checking the condition of the while.
do while 循环- 在检查 while 条件之前执行一次循环内容。
回答by Halayem Anis
They are exactly the same :
它们完全相同:
WHILE
尽管
INITIALIZE_1;
INITIALIZE_2;
...
INITIALIZE_N;
while(CONDITIONS) {
// your block code
...
# not only incrementation !!
OPERATION_1;
OPERATION_2;
...
OPERATION_N;
}
FOR
为了
for (INITIALIZE_1, INITIALIZE_2, ..., INITIALIZE_N ; CONDITIONS ; OPERATION_1, OPERATION_2, ..., OPERATION_N) {
// your block code
...
}
for
is more comprehensive for a humain, so he can see in one line : the initialization, conditions and operations, but there are other cases when while
is more appropriate like :
for
对于人类来说更全面,因此他可以在一行中看到:初始化,条件和操作,但还有其他while
更合适的情况,例如:
while(NOT_QUIT_SIGNAL) { ... } vs for ( ; NOT_QUIT_SIGNAL ; ) { ... }
or
或者
while(TRUE) { ... } vs for ( ; TRUE ; ) { ...}
回答by MetalMichael
You can use while(true)
for a daemon (although there are definitely better languages for it), or a similar scenario, where you may want the script to run indefinitely.
您可以使用while(true)
守护进程(尽管肯定有更好的语言),或类似的场景,您可能希望脚本无限期地运行。
Alternatively, while can be used with a predicate, or executing a function each time to check for a property.
或者,while 可以与谓词一起使用,或者每次都执行一个函数来检查属性。
while(functionCanExecute($someVar)) {
//Do Stuff
}
This will be evaluated initially, and after every loop, to check if the while loop should run again.
这将在最初和每个循环之后进行评估,以检查 while 循环是否应该再次运行。
Their underlying functionality may be very similar/the same, but there are certainly scenarios when it's easier to use one over the other.
它们的底层功能可能非常相似/相同,但肯定存在使用一种比另一种更容易的情况。