Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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). Is there a reason why there is this difference or is it just an arbitrary decision?

like image 637
Bob Avatar asked Nov 30 '22 05:11

Bob


1 Answers

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() );
like image 93
VoteyDisciple Avatar answered Dec 15 '22 15:12

VoteyDisciple