drux41001 2015-09-14 10:11
浏览 69
已采纳

while()和for()之间有什么区别? [重复]

This question already has an answer here:

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 ...!

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 php has both of them?

</div>
  • 写回答

3条回答 默认 最新

  • douxian1939 2015-09-14 10:14
    关注

    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 loop - used for looping until a condition is satisfied and when it is unsure how many times the code should be in loop

    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 loop - executes the content of the loop once before checking the condition of the while.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试