开发者

Understanding a particular For Loop

开发者 https://www.devze.com 2023-03-31 10:14 出处:网络
So I was researching C++ yesterday; looking at some example code, and trying to get the feel of things. I saw this:

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消