A
while()
loop will not run unless the codition is true. A
do...while()
loop will always run at least once.
The following will run at least one time
bool x = true;
while (x)
{
}
The following while loop will not execute because the condition is false.
bool x = false;
while (x)
{
}
The following will always execute at least once
bool x = true;
do
{
} while (x);
That's the primary difference between the two.
You can also use
break
or
continue
inside those loops to short-circuit code to either exit the loop or start a new iteration.
Lastly, you can do this:
while (true)
{
}
...or this:
do
{
} while (true);
It creates a loop that keeps going until it encounters a
break
statment.