PHP For循环到While循环转换

时间:2013-03-19 17:05:43

标签: php

简单的问题我希望,坚持几个小时,所以会感激一些帮助。

我需要知道如何将其转换为do while:

for ($counter = 0 ; $counter < 10 ; $counter++) {

这一段时间了:

for ($mower = $counter ; $mower ; $mower--) {

感谢您的帮助,并在必要时提供更多信息

5 个答案:

答案 0 :(得分:3)

for (init; condition; increment) {
    stuff; 
}

几乎完全等同于

init;
while (condition) {
    stuff;
    increment;
}

(在大多数情况下甚至编译为相同的字节序列),几乎所有语言都使用类C语法(包括PHP)。

它也类似于

init;
if (condition) do {
    stuff;
    increment;
} while (condition);

除了后者是可怕的。 :)请注意,如果初始状态和条件是这样的,你知道第一次迭代将始终运行,你可以摆脱if

答案 1 :(得分:1)

嗯,这样的事情?

$counter = 0;
do {
  $counter++;
} while($counter < 10);

$mower = $counter;
while($mower) {
  $mower--;
}

答案 2 :(得分:0)

$counter = 0;
do {
    // Do things
    $counter ++;
} while ($counter  < 10);

$mower = $counter;
while ($mower) {
    // Do things
    $mower--;
}

更多信息:

答案 3 :(得分:0)

第一个:

$cont = 0;
do{
   //whatever
   $cont++;
}while($cont<10);

第二

$mover = $counter;
while($mower){
    //whatever
   $mower--;
}

答案 4 :(得分:0)

首先做while循环:

$counter = 0;

do{

// some statement
$counter ++;
} while($counter < 10);

for while循环:

$mower = $counter; 
while($mower){
   //statement
$mower--;
}