|
|
Log in / Subscribe / Register

Or even old loop constucts

Or even old loop constucts

Posted Jul 4, 2013 16:00 UTC (Thu) by mcmechanjw (guest, #38173)
Parent article: Philosophy and "for" loops — more from Go and Rust

Once upon a time 39 years ago.
We still have the loop and a half problem Knuth pointed out even in this article's examples...

Elliot Soloway, Jeffrey Bonar, and Kate Ehrlich, "Cognitive
strategies and looping constructs: an empirical study" Communications
of the ACM, Vol. 26, No. 11, November 1983
showing that people overwhelmingly preferred
loop;
S;
if B then leave;
T;
again
even on a few minutes training to more classic loop constructs to solve the loop and a half class of problems and in using it did a better job
We still emulate this in C with
while(1) {
statements
if condition break;
statements
}
Which I suppose is good enough.
But will we ever see a language with a loop construct to make this natural pattern easy and clear?


to post comments

Or even old loop constucts

Posted Jul 4, 2013 22:08 UTC (Thu) by lonely_bear (subscriber, #2726) [Link]

Something like this: http://en.wikibooks.org/wiki/Ada_Programming/Control#Loops ? "Loop with condition in the middle" seems match your description quite nicely.

Or even old loop constucts

Posted Jul 5, 2013 6:22 UTC (Fri) by cmrx64 (guest, #89304) [Link]

Well, Rust has

loop {
S;
if cond { break }
T;
}

Or even old loop constucts

Posted Jul 5, 2013 14:22 UTC (Fri) by NRArnot (subscriber, #3033) [Link] (3 responses)

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.

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".

Trivial, solved ages ago

Posted Jul 7, 2013 15:53 UTC (Sun) by HelloWorld (guest, #56129) [Link]

We still have the loop and a half problem Knuth pointed out even in this article's examples...
No we don't, that problem was solved ages ago. In well-designed languages it's a non-issue because there's no pointless distinction between statements and expressions. Others, like GNU C, have features to cope with this kind of usage. In GNU C, your example
while(1) {
  statements
  if condition break;
  statements
}
can be rewritten as
while ({ statements; !condition; }) {
  statements
}
So, of course some people who choose to use outdated languages still have to worry about such paltry problems, but that certainly doesn't justify calling this an unsolved problem.

Or even old loop constucts

Posted Jul 11, 2013 15:02 UTC (Thu) by oldtomas (guest, #72579) [Link]

Niklaus Wirth, in his ever-lasting quest for simplicity, introduced LOOP as you envision in Modula-2.

Later, in Oberon he did away with FOR, WHILE and the other looping constructs, keeping only LOOP. It seems that FOR came back in Oberon-2.


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