454,507 Members | 1,758 Online
Need help? Post your question and get tips & solutions from a community of 454,507 IT Pros & Developers. It's quick & easy.

# Help! "for (;;)"?

 P: n/a 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! Nov 22 '05 #1
10 Replies

 P: n/a Perro Flaco wrote: 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 Nov 22 '05 #2

 P: n/a "Perro Flaco" wrote in message news:11*********************@g14g2000cwa.googlegro ups.com... 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. -- Sumit Rajan Nov 22 '05 #3

 P: n/a In only five minutes I've got two answers to my question. This is great! :) Thank you very much! ˇMuchas gracias! Nov 22 '05 #4

 P: n/a 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 Nov 22 '05 #5

 P: n/a Your example is very welcome. Again, thanks! Nov 22 '05 #6

 P: n/a mlimber wrote: 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: Fe********@cwi.nl Room: C0.07 Phone: 4229 INS3 Visualization and 3D Interfaces CWI Amsterdam, The Netherlands Nov 22 '05 #7

 P: n/a Kwan Lim wrote: 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 Nov 22 '05 #8

 P: n/a Ferdi Smit wrote: mlimber wrote: 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. Always write exception safe code so you never have to worry about what exceptions might do :-) Gavin Deane Nov 22 '05 #9

 P: n/a Kwan Lim wrote: 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; -- Salu2 Nov 22 '05 #10

 P: n/a Julián Albo wrote: 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; 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. -- Karl Heinz Buchegger kb******@gascad.at Nov 22 '05 #11

### This discussion thread is closed

Replies have been disabled for this discussion.