• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Programming noob concept I've never understood...

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.
 
Its purpose is that you will execute the contents of the loop at least once, since the condition is not evaluated until afterwards.
 
Like Tencntraze said, it makes the logic behind your code clearer if you always want the code in a loop to execute at least once before testing the stopping condition.

Here's how I used it in the VIN thread:

create empty list

for ( 1 to 1000 )
{

do {
generate a new random number
check if already in the list
} while ( already in the list ) ;

add new number to list
}
 
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.
 
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.

Yeah. It's not obvious how that's useful until you actually have a use for it 🙂

While-do is more common.
 
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.
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.

 
the purpose is that you don't have to set anything up if you want to run this one piece of code at least one time.

If you wanted to do the same thing with a while loop, you'd have to do a conditional statement beforehand.
 
Private sub Test()
Dim i as integer
i = 0
do while i < 5
debug.print i
i = i + 1
Loop
End sub
 
While it's already been made clear enough, a loop allows you to repeat an action until certain conditions are met.

<?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

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
 
Do/While is good for reading input from a file. You want to read atleast once, and if the value is null (or EOF) you can stop, but if there is still more in the file you want to loop and read again.

do {
input = read_from_file(inputFile);
} while(input != null) //or EOF
 
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

Of course it's the same loop. You just moved where the condition is tested, that's all.
 
Originally posted by: KLin
Of course it's the same loop. You just moved where the condition is tested, that's all.

The output is the same for the value of 'i' in the block of code, yes... but the loops aren't the same for all values of 'i'.
 
Do-While is also good for menus.

Do
<List 5 menu options>
while x>5 || x<1

That way, it will keep listing the menu if they enter a value not listed.
 
Back
Top