简体   繁体   English

帕斯卡的重复...直到与C的比赛......同时

[英]Pascal's repeat… until vs. C's do… while

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 will 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). 在C中有一个while循环和pascal(几乎)等价是重复直到循环,但两者之间有一个小的差异,而两个结构将至少迭代一次并检查它们是否只需要再次循环最后,在pascal中你编写了需要满足的条件来终止循环(REPEAT UNTIL something)在C中你写入需要满足的条件来继续循环(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: 如果,在C中,你遇到的情况是“直到”是你的愿望,你总是可以否定这个条件:

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

In C the statement 在C语句中

 while(some_condition);

might either be a "do nothing" loop or might have become detached from a "do ... while" loop. 可能是“无所事事”循环,或者可能已经脱离“do ... while”循环。

 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. 尽管如此,我怀疑大多数资深C程序员都希望 - 在某些时候或其他时候 - 在这种情况下C使用“直到”。

我不知道对历史的影响,但在我看来,C是比较一致的,在这个意义上if小号需要的条件是真实的代码运行,因为这样做while S和do while秒。

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 . Pascal的设计部分源于20世纪60年代的结构化编程工作,包括Edsger Dijkstra的开创性工作A Programming of Programming Dijkstra (the same man who considered goto harmful) invented methods for creating programs that were correct by construction. Dijkstra(同样认为goto有害的人)发明了用于创建通过构造正确的程序的方法。 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. 在创建repeat... until形式时,Wirth受到Dijkstra的启发,在代码中明确表示终止条件,而不是它的补充。

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. 我一直钦佩像Smalltalk和Icon这样的语言,它们提供两种语法形式,从而允许程序员清楚地表达他或她的意图,而不必依赖于容易错过的补语操作符。 (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. (在图标中,表格是while e1 do e2until e1 do e2 ;在Smalltalk中它们是block1 whileTrue: block2block1 whileFalse: block2 。)从我的角度来看,C和Pascal都不是完全构建的正交设计。

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. QBASIC / VB DO ... LOOP语句支持pretest / posttest和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. 没有“决定”会以任何方式将Pascal repeat / until的行为与C do / while循环的行为联系起来,既不是故意的也不是任意的。 These are simply two completely unrelated issues. 这些只是两个完全不相关的问题。

Just some information. 只是一些信息。

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

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

I've always found UNTIL loops backwards, but that might just be because I'm from a C background. 我总是发现UNTIL循环向后,但这可能只是因为我来自C背景。 There are modern languages like Perl that provide both, but there isn't any particular advantage for one over the other 有像Perl这样的现代语言可以提供这两种语言,但是对于一种语言而言没有任何特别的优势

The C syntax requires no extra keywords. C语法不需要额外的关键字。

In C, the two keywords do and while work for two kinds of loops. 在C语言中,这两个关键字do ,并while有两个种类的循环工作。 Pascal requires four keywords: while , do , repeat , and until . Pascal需要四个关键字: whiledorepeatuntil

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM