Help! "for (;;)"?

P

Perro Flaco

Hi everybody!

I've got a very simple, but very difficult to me, question: can anybody
explain me this?

for (;;)
{
(...)
}


I just don't get it. Inside the loop, I've got a couple of "break".

Thank you for your help!
 
M

mlimber

Perro said:
Hi everybody!

I've got a very simple, but very difficult to me, question: can anybody
explain me this?

for (;;)
{
(...)
}


I just don't get it. Inside the loop, I've got a couple of "break".

Thank you for your help!

It's an infinite loop (same as "while(true) { /*...*/ }"). The "break"s
are the only way to get out of it.

Cheers! --M
 
S

Sumit Rajan

Perro Flaco said:
Hi everybody!

I've got a very simple, but very difficult to me, question: can anybody
explain me this?

for (;;)

The above line starts the loop. The is no exit condition so the loop will go
on forever -- unless you "break" out of it.
{
(...)
}

Regards,
Sumit.
 
P

Perro Flaco

In only five minutes I've got two answers to my question. This is
great! :)

Thank you very much!
¡Muchas gracias!
 
K

Kwan Lim

This is called a mid-test loop or multi-exit loop. It can be used to
avoid a common software engineering problem: duplicate code.
For example,

read(input, d)
while (!eof(input)) do
do something with d;
read(input, d);
end

- here, read(input, d) is duplicated.

vs.

while(true) do
read(input, d);
if(eof(input)) break;
do something with d;
end

- here there's no code duplication
 
F

Ferdi Smit

mlimber said:
It's an infinite loop (same as "while(true) { /*...*/ }"). The "break"s
are the only way to get out of it.

Well, not the "only" way. You could issue a 'return' (heck even a
'goto'...yikes) or more importantly an exception might be thrown
implicitly by other code in the loop; perhaps in a completely different
source module. Always consider exceptions and what they might do.


--
Regards,

Ferdi Smit (M.Sc.)
Email: (e-mail address removed)
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
 
D

deane_gavin

Kwan said:
This is called a mid-test loop or multi-exit loop. It can be used to
avoid a common software engineering problem: duplicate code.
For example,

read(input, d)
while (!eof(input)) do
do something with d;
read(input, d);
end

- here, read(input, d) is duplicated.

vs.

while(true) do
read(input, d);
if(eof(input)) break;
do something with d;
end

- here there's no code duplication

I am changing the subject here so not detracting from your point at all
....

I think it's worth mentioning that with C++ streams that pseudo-code
should look like this (note: still no code duplication)

while (read(input, d))
do something with d
end
if (!eof(input)) ... something went wrong reading from the stream

I want to make that point because there is a common misunderstanding
about how to use eof with streams.

Gavin Deane
 
D

deane_gavin

Ferdi said:
Well, not the "only" way. You could issue a 'return' (heck even a
'goto'...yikes) or more importantly an exception might be thrown
implicitly by other code in the loop; perhaps in a completely different
source module. Always consider exceptions and what they might do.

Always write exception safe code so you never have to worry about what
exceptions might do :)

Gavin Deane
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Kwan said:
This is called a mid-test loop or multi-exit loop. It can be used to
avoid a common software engineering problem: duplicate code.
For example,

read(input, d)
while (!eof(input)) do
do something with d;
read(input, d);
end

- here, read(input, d) is duplicated.

vs.

while(true) do
read(input, d);
if(eof(input)) break;
do something with d;
end

- here there's no code duplication

I think a better solution in this case and many similar others is something
like that (pseudocode):

bool read_eof (input, d)
{
read (input, d);
return ! eof (input);
}

while (read_eof (input, d) )
do_something;
 
K

Karl Heinz Buchegger

Julián Albo said:
I think a better solution in this case and many similar others is something
like that (pseudocode):

bool read_eof (input, d)
{
read (input, d);
return ! eof (input);
}

while (read_eof (input, d) )
do_something;

Actually this is not a better solution in C++.
The main problem is, that you still try to use eof()
for controlling the loop.

There is nothing wrong with what Deane posted
and it is the way to go:

while( read( input, d ) )
do_something;

if( !eof( input ) )
something_went_wrong;

Note that this loop terminates even if some other thing
then eof() happens. Things like: user removed device (floppy, cd,
memory stick, ...) during the read operation, the network connection
broke down, the modem had a transmission failure, phone line broke
or simply a bad block on the hard disc.
 

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,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top