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? :)

I think thats clear enough, thanx a lot guys ;)

Member Avatar for DigitalPackrat

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?

Thanx a lot dude!!!!!!

Member Avatar for DigitalPackrat

Yup.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.