开发者

Pascal's repeat... until vs. C's do... while

开发者 https://www.devze.com 2023-01-02 02:50 出处:网络
In C there is a do while loop and pascal\'s (almost) equivalent is the repeat until loop, but there is a small difference between the two, while both structures wil开发者_如何学编程l iterate at least

In C there is a do while loop and pascal's (almost) equivalent is the repeat until loop, but there is a small difference between the two, while both structures wil开发者_如何学编程l iterate at least once and check whether they need to do the loop again only in the end, in pascal you write the condition that need to met to terminate the loop (REPEAT UNTIL something) in C you write the condition that needs to be met to continue the loop (DO WHILE something). Is there a reason why there is this difference or is it just an arbitrary decision?


There's no fundamental difference at all, and no advantage to one over the other. It's just "syntactic sugar" — a change to the language's syntax that doesn't change its behavior in any real way. Some people find "repeat until" easier to conceptualize, while others find "repeat while" easier.

If, in C, you encounter a situation where "until" is what's desire, you can always just negate the condition:

do {
    excitingThings();
} while ( !endOfTheWorld() );


In C the statement

 while(some_condition);

might either be a "do nothing" loop or might have become detached from a "do ... while" loop.

 do {
  statement;
  statement;
  statement;
  lots more statements;
 }

 while(some_condition);

Using a different keyword - until - avoids this possible misinterpretation.

Not such a problem these days when everybody turns on all compiler warnings and heeds them, don't they? Still, I suspect that most veteran C programmers have wished - at some time or other - that C used "until" in this case.


I'm not sure about historical influences, but in my opinion C is more consistent, in the sense that ifs require a condition to be true for the code to run, as do whiles and do whiles.


The design of Pascal was motivated in part by the structured-programming work of the 1960s, including Edsger Dijkstra's groundbreaking work A Discipline of Programming. Dijkstra (the same man who considered goto harmful) invented methods for creating programs that were correct by construction. These methods including methods for writing loops that focus on the postcondition established when the loop terminates. In creating the repeat... until form, Wirth was inspired by Dijkstra to make the termination condition, rather than its complement, explicit in the code.

I have always admired languages like Smalltalk and Icon, which offer two syntactic forms, thus allowing the programmer to express his or her intent clearly, without necessarily having to rely on an easily missed complement operator. (In Icon the forms are while e1 do e2 and until e1 do e2; in Smalltalk they are block1 whileTrue: block2 and block1 whileFalse: block2.) From my perspective neither C nor Pascal is a fully built out, orthogonal design.


It's just an arbitrary decision. Some languages have both. The QBASIC/VB DO...LOOP statement supports all four combinations of pretest/posttest and WHILE/UNTIL.


There was no "decision" that would in any way connect the behavior of Pascal repeat/until loop with the behavior of C do/while loop, neither deliberate nor arbitrary. These are simply two completely unrelated issues.


Just some information.

Road Runner : while(not edge) { run(); }

Wily E Coyote : do { run(); } while(not edge);


I've always found UNTIL loops backwards, but that might just be because I'm from a C background. There are modern languages like Perl that provide both, but there isn't any particular advantage for one over the other


The C syntax requires no extra keywords.

In C, the two keywords do and while work for two kinds of loops. Pascal requires four keywords: while, do, repeat, and until.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号