Is there any loop that can expressed in while that cannot be expressed in for..?

Y

yuyang08

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?

Thanks!

-Andy
 
B

Bart

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

Regards,
Bart.
 
M

Michiel.Salters

Bart said:
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.
 
P

peter koch

As we all known, each for-loop can be expressed in while statement.

We do? I do not believe that is correct. To translate a for statement
you need a while statement and a bit of initialisation in the general
case. How else would you do:

int f1();
int f2();

for (int begin = f1(), int end = f2(); begin < end; ++begin)
stmt;

/Peter

[snip]
 
H

Heinz Ozwirk

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
 
H

Heinz Ozwirk

peter koch said:
We do? I do not believe that is correct. To translate a for statement
you need a while statement and a bit of initialisation in the general
case. How else would you do:

int f1();
int f2();

for (int begin = f1(), int end = f2(); begin < end; ++begin)
stmt;

That is not a valid for statement. for (int begin=f1(), end=f2(); ...) would
be valid, but there can only be one decl-specifier-seq in a
for-init-statement.

Heinz
 
P

peter koch

Heinz said:
That is not a valid for statement. for (int begin=f1(), end=f2(); ...) would
be valid, but there can only be one decl-specifier-seq in a
for-init-statement.

Heinz

Right. But my statement remains valid even if the example was
ill-formed.

/Peter
 
E

Earl Purple

Heinz said:
That is not a valid for statement. for (int begin=f1(), end=f2(); ...) would
be valid, but there can only be one decl-specifier-seq in a
for-init-statement.

which is an annoying language feature when you want to initialise two
or more locals of different types. Of course there is a workaround
using tuples but that is relatively clumsy.
 
V

Victor Bazarov

Earl said:
which is an annoying language feature when you want to initialise two
or more locals of different types. Of course there is a workaround
using tuples but that is relatively clumsy.

What's wrong with wrapping the loop in another set of curly braces?
Initialise all you need before the 'for'.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top