views:

446

answers:

8

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?

A: 

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

Michael Mrozek
I personally also think the C version makes more sense, and I come from a pascal background, so I found myself writing the wrong condition in C once I started studying C.
Bob
+3  A: 

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.

Artelius
and `for`, for that matter.
caf
+5  A: 

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() );
VoteyDisciple
+1 for checking if the world didn't end.
Bob
At least, we all do one excitingThings :D
Nyan
+1  A: 

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.

dan04
+1  A: 

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.

AndreyT
+3  A: 

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.

Dipstick
I use `while (cond) continue;` to make it clear that the loop body is empty.
Loadmaster
@Loadmaster - good idea - I might adopt that one for my own code.
Dipstick
@ChrisHarris: Yep, an empty statement (a bare semicolon) for a loop body is a bug just waiting to happen. Bad code style.
Loadmaster
A: 

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.

Loadmaster
You would need only 1 keyword more. `while` for precondition, either `repeat` or `do` and `until` for postcondition
tristopia
On the other hand, C recycles keywords, potentially leading to confusion. See ChrisHarris' post
Marco van de Voort
+2  A: 

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.

Norman Ramsey
So take perl, it has even the complementary of `if` which is `unless` and you can place them before the statement or after it. But the orthogonality of perl is not really an advantage.
tristopia
+1, providing both forms in the language allows for more readable code.
Artelius