<Mi*************@tomtom.comschrieb im Newsbeitrag
news:11**********************@p79g2000cwp.googlegr oups.com...
>
Bart wrote:
>yu******@gmail.com wrote:
As we all known, each for-loop can be expressed in while statement.
Can we also say that each while statement can be expressed in
for-statment?
Sure. while(x) is the same as for(; x; )
No: while(int bar = foo()) { } is a while-statement that doesn't
transform
as you suggest. It does have an equivalent for-loop though.
Are you sure? If bar is a built-in type, I agree. But what about user
defined types? How would
while (SomeUglyClass bar = foo()) {...}
be written as a for loop, assuming that SomeUglyClass has all the necessary
constructors and conversions, but no assignment operator (or if assignment
behaves slightly different from copy construction). The while loop
constructs (and destructs) a new instance of SomeUglyClass for each
execution of its body. An obvious solution using a for loop (for
(SomeUglyClass bar; bar=foo(); ) {}) only creates one instance of
SomeUglyClass, so it would not be equivalent to the while loop.
The only really equivalent for loop I can think of, is something like
for (;;)
{
if (ComeUglyClass bar = foo())
{
...
}
else
break;
}
but I don't think this really counts as a valid replacement.
Heinz