|
|
Log in / Subscribe / Register

Or even old loop constucts

Or even old loop constucts

Posted Jul 5, 2013 14:22 UTC (Fri) by NRArnot (subscriber, #3033)
In reply to: Or even old loop constucts by mcmechanjw
Parent article: Philosophy and "for" loops — more from Go and Rust

What am I missing? What is the difference? "while( True)" clearly means loop forever unconditionally; "if( cond) break" clearly provides the test which rescues the program from the unconditional infinite loop; the structure is the same.


to post comments

Or even old loop constucts

Posted Jul 5, 2013 14:51 UTC (Fri) by etienne (guest, #25256) [Link] (2 responses)

> What am I missing? What is the difference?

The difference is that you may not want to end the loop either at the beginning or at the end - maybe you have to do something once even if the loop shall not be executed once.
You can also sometimes simplify the exit test by writing multiple "if (cond) break;", in complex tests lie more bugs.
I usually do not use "while(1)" because Quality says not to have literal numbers, nor one of the "while (true)", "while (True)", "while (TRUE)", nor "while (!false)", but "for (;;)" - there is no "always true" warning to get when there is no test.

Or even old loop constucts

Posted Jul 5, 2013 17:03 UTC (Fri) by nybble41 (subscriber, #55106) [Link]

> The difference is that you may not want to end the loop either at the beginning or at the end - maybe you have to do something once even if the loop shall not be executed once.

That is exactly what the C, Ada, and Rust versions do. There is no behavioral difference between

loop;
S;
if B then leave;
T;
again

and the C equivalent

for (;;) {
  S;
  if (B) break;
  T;
}

Or even old loop constucts

Posted Jul 5, 2013 17:13 UTC (Fri) by tjc (guest, #137) [Link]

I've never liked for(;;) -- it's a special case where "nothing" evaluates to "true", which is different from the rest of the language, where 1 is the canonical value for true, and "nothing" means ... "nothing".


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds