0

In niek_e's example, the init-expression, cond-expression, and loop-expression are all optional as long as you include the separating semicolons. That's why an infinite "forEVER" loop looks like this:

for (;;) {
  ...
}

As long as the loop does what you want, the syntax is very flexible. In Ed's experience, the usual culprits are a very long initialization that makes the loop line too long and a more complicated update:

// So long it makes more sense outside the loop
std::vector<std::pair<std::string, double> >::size_type index = 0;

for (; index != v.size(); ++index) {
  ...
}

// As a part of the loop (too long)
for (std::vector<std::pair<std::string, double> >::size_type index = 0; index != v.size(); ++index) {
  ...
}
// n isn't incremented with every loop
for (n = 0; n < 10 && std::getline(std::cin, input); ) {
  if (input[0] == '*')
    ++n;
}

Cool, huh? :)

0

Reiterating the point, niek_e made, its possible. All of them are not necessary, in fact none are.
Also, I did not want to use sort variable outside the loop, so I used it inside as, if there are more bubble sorts and I use the sort variable again and don't initialize it, well, that would give me some headache. Won't it?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.