1

I was always under the impression that a variable declared in any kind of loop statement is scoped to that statement alone. And a little poking around in similar questions seems to confirm this idea. So I am puzzled by the following excerpt from Stroustrup's A Tour of C++ (§4.2.3 Initializing Containers p. 38):

"The push_back() is useful for input of arbitrary numbers of elements. For example:

Vector read(istream& is) { Vector v; for (double d; is>>d;) // read floating-point values into d v.push_back(d); // add d to v return v; } 

The input loop is terminated by an end-of-file or a formatting error. Until that happens, each number read is added to the Vector so that at the end, v’s size is the number of elements read. I used a for-statement rather than the more conventional while-statement to keep the scope of d limited to the loop."

This seems to imply that variables declared in the condition of a while statement persist outside the statement body.

1
  • A variable declared in a for loop statement is in scope for the entire loop body. That being said, "I used a for-statement rather than the more conventional while-statement to keep the scope of d limited to the loop" - if you use std::copy() with std::istream_iterator and std::back_inserter, you can eliminate the loop altogether, eg: std::copy(std::istream_iterator<double>(is), std::istream_iterator<double>(), std::back_inserter(v)); Commented Jan 7, 2017 at 18:04

2 Answers 2

4

Let's examine that loop:

for (double d; is>>d;) // read floating-point values into d v.push_back(d); // add d to v 

Here we have:

  • a declaration of d
  • a loop condition
  • an empty "do on each iteration" expression

And, yes, d is limited in scope to the for loop.

Now try writing a while loop to do the same job, keeping d limited in scope. You won't be able to, because there's no place to put a declaration in the preamble of a while. Only for has that feature. A while only has a condition.

That doesn't mean the scoping rules are different for while; it only means that it is not possible to write this code using while. There aren't any "variables declared in the condition of a while statement".

Sign up to request clarification or add additional context in comments.

4 Comments

Here is a C++17 paper which suggest adding optional initialization to if and switch statement, citing that while statement has that option in form of for statement.
@Revolver_Ocelot: I've seen that; can't really decide whether I like it or not tbh. In terms of its formatting, wording, explanation and diagrams, though, it's far and away my favourite proposal paper!
How stupid of me! A little thought would have immediately made it clear that it's not possible to declare a variable in the condition of a while! Since if it is initialized to zero or false, the statement body will not run and if initialized to any other value, it will be reestablished in every iteration and cause an infinite loop. cppreference explicitly explains such a construct without noting that it would be completely pointless.
@EhsanAmini: Hardly "completely pointless"; the same page gives an example of the construct in use! I actually didn't know that you could put a declaration in the while preamble, so that's nice. Still, indeed it doesn't help you, as your loop termination condition is separate to the initialiser.
4

[..] that variables declared in the condition of a while statement [..]

That's not possible.

Using a for statement allows to declare a variable like this

for(int a = 0; a < 5; a++) { // Use a } // a is not visible anymore 

If you use a while loop, it is visible

int a = 0; while(a < 5) { // Use a a++; } // a still visible 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.