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

查看:125
本文介绍了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是一个进入控制循环,而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

执行while循环-在检查while的条件之前执行一次循环的内容.

do while loop - executes the content of the loop once before checking the condition of the while.

这篇关于while()和for()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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