- Oct 10, 2006
- 21,562
- 3
- 0
What's the purpose of a do-while loop? I know how to do it, but I was never taught it, and to me it just seems like a while loop as far as capabilities.
Originally posted by: Tencntraze
Its purpose is that you will execute the contents of the loop at least once, since the condition is not evaluated until afterwards.
Same here. I remember I used to use do while loops in the past but now I can't remember the last time that was.Originally posted by: Cooler
All Loops are Ifs with GOTO in them. Anything that a For loop can do so can a while and do while. You would just have to change the setup. Most of the time I use for and while loops myself.
<?php
$x = 1;
$y = 7;
echo "<pre>";
while ($x < $y)
{
echo $x . " is less than " . $y . ".\n";
$x++;
}
/*
Since x = 1 and y = 7, this would print out:
1 is less than 7.
2 is less than 7.
3 is less than 7.
4 is less than 7.
5 is less than 7.
6 is less than 7.
Incremement x while it is less than y.
*/
if($x == $y)
{
echo $x . " and " . $y . " are the same.\n";
// If x and y are the same, let me know.
}
elseif($x > $y)
{
echo $x . " is greater than " . $y . ". I'm not dealing with this.";
// If x is greater than y, run away and cry.
}
else
{
// Nothing to do here
}
echo "</pre>";
?>
Originally posted by: KLin
Private sub Test()
Dim i as integer
i = 0
do while i < 5
debug.print i
i = i + 1
Loop
End sub
Originally posted by: Aikouka
Originally posted by: KLin
Private sub Test()
Dim i as integer
i = 0
do while i < 5
debug.print i
i = i + 1
Loop
End sub
If my knowledge serves me correctly, that's not the same loop that we're talking about here. That's just a standard While loop. What this thread is about would be this code:
Private sub Test()
Dim i as integer
i = 0
Do
debug.print i
i = i + 1
Loop While i < 5
End sub
Originally posted by: KLin
Of course it's the same loop. You just moved where the condition is tested, that's all.
