• We should now be fully online following an overnight outage. Apologies for any inconvenience, we do not expect there to be any further issues.

Programming noob concept I've never understood...

irishScott

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

Tencntraze

Senior member
Aug 7, 2006
570
0
0
Its purpose is that you will execute the contents of the loop at least once, since the condition is not evaluated until afterwards.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
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
}
 

Cooler

Diamond Member
Mar 31, 2005
3,835
0
0
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.
 

Smilin

Diamond Member
Mar 4, 2002
7,357
0
0
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.
 

jjones

Lifer
Oct 9, 2001
15,424
2
0
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.

 

VIAN

Diamond Member
Aug 22, 2003
6,575
1
0
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.
 

KLin

Lifer
Feb 29, 2000
30,430
747
126
Private sub Test()
Dim i as integer
i = 0
do while i < 5
debug.print i
i = i + 1
Loop
End sub
 

Alone

Diamond Member
Nov 19, 2006
7,490
0
0
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>";
?>
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
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
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
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
 

KLin

Lifer
Feb 29, 2000
30,430
747
126
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.
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
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'.
 

James Bond

Diamond Member
Jan 21, 2005
6,023
0
0
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.