Pascal 的重复...直到 vs. C 的 do...while
在 C 中,有一个 do while 循环,而 pascal 的(几乎)等价物是重复直到循环,但两者之间有一个小的区别,而这两种结构都会至少迭代一次,并仅在以下情况下检查是否需要再次执行循环:最后,在 pascal 中,您编写了终止循环所需的条件(REPEAT UNTIL某些内容),在 C 中,您编写了继续循环所需的条件(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). Is there a reason why there is this difference or is it just an arbitrary decision?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
根本没有什么根本区别,也没有任何一种比另一种有优势。它只是“语法糖”——对语言语法的更改,但不会以任何实际方式改变其行为。有些人发现“重复直到”更容易概念化,而另一些人则发现“重复同时”更容易。
如果在 C 语言中,你遇到“until”是你想要的情况,你总是可以否定这个条件:
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 ... while”循环分离。
使用不同的关键字 - 直到 - 可以避免这种可能的误解。
如今,当每个人都打开所有编译器警告并注意它们时,这不再是一个问题,不是吗?
尽管如此,我怀疑大多数资深 C 程序员在某个时候都希望 C 在这种情况下使用“until”。
In C the statement
might either be a "do nothing" loop or might have become detached from a "do ... while" loop.
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 更加一致,因为
if
需要条件为 true 才能运行代码,就像while
和do while
一样。I'm not sure about historical influences, but in my opinion C is more consistent, in the sense that
if
s require a condition to be true for the code to run, as dowhile
s anddo while
s.Pascal 的设计部分受到 20 世纪 60 年代结构化编程工作的推动,包括 Edsger Dijkstra 的开创性工作 编程规则。 Dijkstra(认为 goto 有害的同一个人)发明了创建通过构造正确的程序的方法。这些方法包括编写循环的方法,这些方法重点关注循环终止时建立的后置条件。在创建
repeat...until
形式时,Wirth 受到 Dijkstra 的启发,在代码中明确了终止条件,而不是其补集。我一直很欣赏像 Smalltalk 和 Icon 这样的语言,它们提供两种语法形式,从而允许程序员清楚地表达他或她的意图,而不必依赖于容易错过的补码运算符。 (在 Icon 中,形式为
while e1 do e2
和until e1 do e2
;在 Smalltalk 中,形式为block1 whileTrue: block2
和block1 whileFalse: block2
。)从我的角度来看,C 和 Pascal 都不是完全构建的正交设计。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 therepeat... 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
anduntil e1 do e2
; in Smalltalk they areblock1 whileTrue: block2
andblock1 whileFalse: block2
.) From my perspective neither C nor Pascal is a fully built out, orthogonal design.这只是一个任意的决定。有些语言两者都有。 QBASIC/VB DO...LOOP 语句支持预测试/后测试和 WHILE/UNTIL 的所有四种组合。
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.
没有任何“决定”能够以任何方式将 Pascal Repeat/until 循环的行为与 C do/while 循环的行为联系起来,既不是故意的,也不是任意的。这只是两个完全不相关的问题。
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.
只是一些信息。
路跑者:<代码> while(不是边缘){ run(); ;
Wily E Coyote:do { run() while(不是边缘);
Just some information.
Road Runner :
while(not edge) { run(); }
Wily E Coyote :
do { run(); } while(not edge);
我总是发现 UNTIL 向后循环,但这可能只是因为我来自 C 背景。有些现代语言(例如 Perl)同时提供这两种语言,但其中一种语言相对于另一种语言并没有任何特别的优势
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
C 语法不需要额外的关键字。
在 C 语言中,两个关键字
do
和while
适用于两种类型的循环。 Pascal 需要四个关键字:while
、do
、repeat
和until
。The C syntax requires no extra keywords.
In C, the two keywords
do
andwhile
work for two kinds of loops. Pascal requires four keywords:while
,do
,repeat
, anduntil
.