PHP For循环到While循环转换
问题描述:
简单的问题,我希望,一直困住了几个小时,所以将不胜感激一些帮助。PHP For循环到While循环转换
我需要知道如何将其转换为一个做而:
for ($counter = 0 ; $counter < 10 ; $counter++) {
,这给了一会儿:
for ($mower = $counter ; $mower ; $mower--) {
感谢您的帮助,并在必要时
可以提供更多信息
答
for (init; condition; increment) {
stuff;
}
几乎完全等同于
init;
while (condition) {
stuff;
increment;
}
(在大多数情况下甚至编译为相同的字节序列),几乎所有语言都采用类C语法(包括PHP)。
这也是不同的是,后者是可怕的相似
init;
if (condition) do {
stuff;
increment;
} while (condition);
。 :)但请注意,如果初始状态和条件是这样的,以至于您知道第一次迭代将始终运行,您可以摆脱if
。
答
$counter = 0;
do {
// Do things
$counter ++;
} while ($counter < 10);
和
$mower = $counter;
while ($mower) {
// Do things
$mower--;
}
更多信息:
+0
@span烧3秒钟:D – astorije 2013-03-19 17:11:48
答
第一招:
$cont = 0;
do{
//whatever
$cont++;
}while($cont<10);
二:
$mover = $counter;
while($mower){
//whatever
$mower--;
}
答
首先做while循环:
$counter = 0;
do{
// some statement
$counter ++;
} while($counter < 10);
对于while循环:
$mower = $counter;
while($mower){
//statement
$mower--;
}
为什么这似乎是一个功课行使? – 2013-03-19 17:07:09
我已经买了一本关于如何学习PHP的书,并且在每章的结尾都有练习来完成。我有代码工作,并显示它想要的罚款,但我不确定如何转换for循环。 – user2187585 2013-03-19 17:09:42
@JonathanKuhn,因为在现实世界中,没有人需要*将'for'循环转换为'do..while'循环。 – 2013-03-19 17:09:43