The only real difference that comes to mind is with '
do while' loops. Since their body is executed once no matter the condition.
So writing out things the same such as this:
Code:
int x = 10;
for (x; x < 10; x++)
{
std::cout << "For was ran.." << std::endl;
}
while (x < 10)
{
std::cout << "While was ran.." << std::endl;
}
do
{
std::cout << "Do was ran.." << std::endl;
} while (x < 10);
The output of this will be:
Do was ran..
Given that do loops run at least once regardless of the condition since the body is executed before the condition.
Granted, each has their purpose on some aspects and are generally used more often than others in those positions. For example:
- For loops are generally used when iterating a container with no intent to alter the container.
- While loops are generally used when doing something until a specific condition is met.
- Do loops are generally used when stepping a data type that is iterated from a handle. (First/Next style API.)
Although these are common uses of each, it certainly isn't a requirement.