Real life example of "indirect" for loop usage?

F

Francesco S. Carta

Hi there,
here is a very simple program that uses a for loop in an uncommon way
(uncommon for me):

-------
#include <iostream>

int i;

void init() {
i = 0;
}

bool check() {
return i < 10;
}

void change() {
++i;
}

void print() {
std::cout << i << std::endl;
}

int main() {
for (init(); check(); change()) print();
}
-------

The request is: could you point out some real life example where you
have found it useful (or maybe even necessary) to use the for loop in
such an indirect way?

Please forgive me for such a candid question, I'm an hobbyist: just out
of curiosity ;-)

Cheers,
Francesco
 
R

Roy Smith

"Francesco S. Carta said:
int main() {
for (init(); check(); change()) print();
}
-------

The request is: could you point out some real life example where you
have found it useful (or maybe even necessary) to use the for loop in
such an indirect way?

The basic idea of using functions for the initialize, test, and
increment portions of the for() loop is very common when using iterators.
 
J

Jonathan Lee

int main() {
     for (init(); check(); change()) print();}

You probably see it all the time as:

init();
while(check()) {
print();
change();
}

Writing it as "for" just seems like a bit of
obfuscation.

--Jonathan
 
F

Francesco S. Carta

The basic idea of using functions for the initialize, test, and
increment portions of the for() loop is very common when using iterators.

OK, but that, in the common usage, is still very direct (all the
initialization, the test and the increment are usually done directly
within the "for" parentheses - even though all those operations call
functions, wrt iterators, they still feel like incrementing a simple
integer counter).

I'm curious about any evidently pushed indirection like the one I've
exemplified.

If such an "evidently pushed" indirection has never been used or needed
in real life, well, never mind... that was just a mind-tickling question
that crossed my mind :)

Thanks for your reply Roy.
 
F

Francesco S. Carta

You probably see it all the time as:

init();
while(check()) {
print();
change();
}

Writing it as "for" just seems like a bit of
obfuscation.

Yes, you're right, my version leans to obfuscation and yours is quite
common and normal- that must be one of the things that tickled my curiosity.

Never mind, I'm realizing that such an usage can't be reasonably
"needed", since we can always flush the deck and rearrange the cards in
a more "ordered" way, just like you did.

Thanks for your reply Jonathan.
 
Ö

Öö Tiib

OK, but that, in the common usage, is still very direct (all the
initialization, the test and the increment are usually done directly
within the "for" parentheses - even though all those operations call
functions, wrt iterators, they still feel like incrementing a simple
integer counter).

For statement is something like that:

for ( init // executed once as very first thing
; end_condition // after init and each loop_expression
; loop_expression // executed after statement
)
statement; // executed if end_condition is true

It has complex structure. It has lot of elements. "loop_expression"
and "statement" have visually swapped positions to their execution
order.
If you look closely then for statement is mostly same as while
statement:

init; // executed once as very first thing
while( end_condition ) // after init and each loop_expression
{
statement; // executed if end_condition is true
loop_expression; // executed after statement
}

Only differences are that if anything is declared in "init" then it is
out of scope after loop and with such usage of while the "statement"
may not contain continue statements. Since your "evidently pushed"
indirection does declare nothing in "init" the first difference drops.
So most use while for "indirect" cycles:

solver.init( problem );
while ( solver.findNextSolution() )
{
problem.solutions().push_back( solver.solution() );
}

Try same with for? Looks cryptic. So it is worse.
 
F

Francesco S. Carta

For statement is something like that:

for ( init // executed once as very first thing
; end_condition // after init and each loop_expression
; loop_expression // executed after statement
)
statement; // executed if end_condition is true

It has complex structure. It has lot of elements. "loop_expression"
and "statement" have visually swapped positions to their execution
order.
If you look closely then for statement is mostly same as while
statement:

init; // executed once as very first thing
while( end_condition ) // after init and each loop_expression
{
statement; // executed if end_condition is true
loop_expression; // executed after statement
}

Only differences are that if anything is declared in "init" then it is
out of scope after loop and with such usage of while the "statement"
may not contain continue statements. Since your "evidently pushed"
indirection does declare nothing in "init" the first difference drops.
So most use while for "indirect" cycles:

solver.init( problem );
while ( solver.findNextSolution() )
{
problem.solutions().push_back( solver.solution() );
}

Try same with for? Looks cryptic. So it is worse.

Yes, I've come to the same conclusion in the other branch of this thread.

Your analysis has made this conclusion clear, and your last while loop
is a nice piece of code - one of those "real life" examples I was
looking for (and even in case you made it up on the fly it still serves
the purpose pretty well).

Thanks for posting it Night.
 
J

James Kanze

You probably see it all the time as:
init();
while(check()) {
print();
change();
}

Not really. At least I don't. A lot of loops (for or while) do
use functions, but there is generally a control variable
involved somewhere. And that variable will have very local
scope.
Writing it as "for" just seems like a bit of obfuscation.

It depends. Exactly when to use for rather than while is
largely a matter of taste. I don't have any problems with
something like:

MyType v ;
for (v.init(); ! v.isDone(); v.next()) ...

for example, and I'd consider:

for (MyType iter; ! iter.isDone(); iter.next()) ...

an almost standard idiom.
 
J

Jonathan Lee

Not really.  At least I don't.  A lot of loops (for or while) do
use functions, but there is generally a control variable
involved somewhere.  And that variable will have very local
scope.

Plenty of objects use this when a local control variable
is inappropriate, but a persistent state is. Event loops,
cellular automata, hash functions, simulated annealing,
root finding, move to front coders, downloading a file,
etc.

However, my point to the OP was that a simple rewrite
made the uncommon code seem more familiar. Judging from
his response above, he understood me.

--Jonathan
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top