Question about using while vs. do while loop

Recently we have been doing I/O streams and while writing the loops I seem to have a hard to knowing whether to use a 'while' or 'do while' loop. I know that 'do while' loops check the condition at the end but don't quite get when one would be better than the other.

The code in a do-while loop will always run once, then loop based on the condition. The code in a while loop will run depending on the initial condition.

1
2
3
4
5
6
7
8
9
10
11
int a = 0;

do
{
   std::cout << "I will run once\n";
} while (a > 0);

while (a > 0)
{
   std::cout << "I won't run\n";
}


Which is better is purely based on situation.
Last edited on
Topic archived. No new replies allowed.