Understanding a particular For Loop
So I was researching C++ yesterday; looking at some example code, and trying to get the feel of things. I saw this:
for (bool b = true; b; )
{
b = true;
//Other stuff.
}
开发者_如何转开发It's making me feel stupid because this is the first time I've seen a for loop used this way. Basically, what is this saying? What would be an equivalent while loop?
It's the same as:
bool b = true; // 1
while(b) // 2
{
b = true;
//Other stuff.
// 3
}
The 3 semicolon-separated parts of a for loop always correspond to the places I commented in the while loop.
Don't think of it as a clever way to save a couple lines, though. Anyone who writes code like you saw should be taken out and shot.
do
{
b = true;
// Other stuff
} while(b);
精彩评论