while() 和 for() 有什么区别? [英] What's the difference between while() and for()?

查看:50
本文介绍了while() 和 for() 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我的问题看起来很初级,但实际上并非如此.我声称我可以通过 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() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆