<p>I wrote an answer for
<a href="https://languagedesign.stackexchange.com/questions/716/what-are-the-advantages-of-providing-an-until-loop-construct/726">pros cons - What are the advantages of providing an until loop construct?</a>.<br />
The ambiguous question was later updated, making it clear that I had misinterpreted it.</p>
<p>It asks about <code>while (…) {…}</code> versus <code>until (…) {…}</code>, with the condition at the top.<br />
This question is about <code>do {…} while (…)</code> versus <code>do {…} until (…)</code>, with the condition at the end.</p>
<p>So, rather than waste my answer, I'll ask the question I thought I'd answered.</p>
<p>What are the advantages of <code>do {…} until</code> over <code>do {…} while</code>?</p>

<language-design>
What are the advantages of `do {…} until` over `do {…} while`?
CC BY-SA 4.0
2023-05-20T00:36:37.053Z
IMO this should be dupe hammered, as the only difference between that and this question is the addition of a ‘do‘, and if you did your algebra, you will know doing this to both loops cancels out (a math joke but I'm serious)
Seggan
CC BY-SA 4.0
2023-05-20T00:51:25.307Z
@Seggan. How can this be rejected as a duplicate when the answer was rejected as inappropriate for that "duplicate"?
Ray Butterworth
CC BY-SA 4.0
2023-05-20T01:13:11.247Z
Because the questions are very closely linked, the only difference is the addition of the do, which doesn't change much
Seggan
CC BY-SA 4.0
2023-05-20T01:18:42.783Z
@Seggan, No, that's not the difference. The first question is about `while (…) {…}` versus `until (…) {…}`, with the condition at the beginning of the block. This is about `do {…} while (…)` versus `do {…} until (…)`, with the condition at the *end* of the block.
Ray Butterworth
CC BY-SA 4.0
2023-05-20T02:25:00.173Z
@user16217248, no it doesn't. I refer to that question in the first line of this question. My answer was rejected for that first question, so how can it possibly be a duplicate?
Ray Butterworth
CC BY-SA 4.0
2023-05-20T13:38:43.000Z
I understand the difference, however the difference applied does not change most of the answers
Seggan
<p>Both <code>while</code> and <code>until</code> allow for writing the same loops, since the condition can always be negated by the logical <code>not</code>/<code>!</code> operator. But I think <code>while(not something)</code> reads much better than <code>until(not something)</code>, so if a language has only one then <code>while</code> is better than <code>until</code>. The latter is not <em>exactly</em> a double negative, but it's close enough that it requires more thinking for me at least.</p>
<p>So the argument for <code>do</code>/<code>until</code> being more natural rests on this kind of loop being used more commonly with &quot;negative&quot; conditions rather than &quot;positive&quot; conditions, so that the negation implicit in <code>until</code> doesn't have to be cancelled out with another negation. This seems like something that should be answerable empirically, but I would expect that <code>do</code>/<code>while</code> with &quot;positive&quot; conditions is at least roughly as common in real code as it is with &quot;negative&quot; conditions.</p>
<p>There are also some conditions like <code>length &gt; 0</code> vs. <code>length == 0</code> where the condition is negated without needing a &quot;not&quot; or &quot;not equal&quot; operator. Personally I think <code>while(length &gt; 0)</code> is more natural than <code>until(length == 0)</code>, but it probably makes sense to exclude cases like this from consideration since they aren't a &quot;win&quot; for either option.</p>
<p>Of course, a language could easily have both <code>while</code> and <code>until</code>, with relatively little extra work for the implementation; one can be <a href="https://en.wikipedia.org/wiki/Syntactic_sugar" rel="nofollow noreferrer">syntax sugar</a> for the other.</p>

CC BY-SA 4.0
2023-05-20T02:07:44.640Z
2023-05-20T02:07:44.640Z
kaya3
<p>Consider:</p>
<pre><code>while (condition) {
    ...
}

do {
    ...
} until (condition)
</code></pre>
<p>The condition before the block decides whether to do what follows.<br />
The condition after the block decides whether to stop doing what preceded.</p>
<p>Both feel natural, but having a <code>while</code> at the <em>end</em> of the block feels awkward.</p>
<p>Even worse, consider this (non-standard style) code:</p>
<pre><code>    ...
}
while (condition)
{
    ...
</code></pre>
<p>Which kind of <code>while</code> is that?
It's impossible to say from the immediate context.</p>
<p>Overloading keywords isn't always a good idea.</p>

CC BY-SA 4.0
2023-05-20T00:10:04.850Z
2023-05-20T00:10:04.850Z
Ray Butterworth
# What are the advantages of `do {…} until` over `do {…} while`?
Posted by: username_0 on 2023-05-20
I wrote an answer for
[pros cons - What are the advantages of providing an until loop construct?](https://languagedesign.stackexchange.com/questions/716/what-are-the-advantages-of-providing-an-until-loop-construct/726).  
The ambiguous question was later updated, making it clear that I had misinterpreted it.

It asks about `while (…) {…}` versus `until (…) {…}`, with the condition at the top.  
This question is about `do {…} while (…)` versus `do {…} until (…)`, with the condition at the end.

So, rather than waste my answer, I'll ask the question I thought I'd answered.

What are the advantages of `do {…} until` over `do {…} while`?
**Tags:** language-design
> **Comments:**
> **username_1**: IMO this should be dupe hammered, as the only difference between that and this question is the addition of a ‘do‘, and if you did your algebra, you will know doing this to both loops cancels out (a math joke but I'm serious)  
> **username_0**: @Seggan. How can this be rejected as a duplicate when the answer was rejected as inappropriate for that "duplicate"?  
> **username_1**: Because the questions are very closely linked, the only difference is the addition of the do, which doesn't change much  
> **username_0**: @Seggan, No, that's not the difference. The first question is about `while (…) {…}` versus `until (…) {…}`, with the condition at the beginning of the block. This is about `do {…} while (…)` versus `do {…} until (…)`, with the condition at the *end* of the block.  
> **username_0**: @user16217248, no it doesn't. I refer to that question in the first line of this question. My answer was rejected for that first question, so how can it possibly be a duplicate?  
> **username_1**: I understand the difference, however the difference applied does not change most of the answers  
---
## 2 answers
### Answer by username_2 on 2023-05-20. Score: 1
Both `while` and `until` allow for writing the same loops, since the condition can always be negated by the logical `not`/`!` operator. But I think `while(not something)` reads much better than `until(not something)`, so if a language has only one then `while` is better than `until`. The latter is not *exactly* a double negative, but it's close enough that it requires more thinking for me at least.

So the argument for `do`/`until` being more natural rests on this kind of loop being used more commonly with "negative" conditions rather than "positive" conditions, so that the negation implicit in `until` doesn't have to be cancelled out with another negation. This seems like something that should be answerable empirically, but I would expect that `do`/`while` with "positive" conditions is at least roughly as common in real code as it is with "negative" conditions.

There are also some conditions like `length > 0` vs. `length == 0` where the condition is negated without needing a "not" or "not equal" operator. Personally I think `while(length > 0)` is more natural than `until(length == 0)`, but it probably makes sense to exclude cases like this from consideration since they aren't a "win" for either option.

Of course, a language could easily have both `while` and `until`, with relatively little extra work for the implementation; one can be [syntax sugar](https://en.wikipedia.org/wiki/Syntactic_sugar) for the other.
---
### Answer by username_0 on 2023-05-20. Score: 1
Consider:
while (condition) {
    ...
}

do {
    ...
} until (condition)

The condition before the block decides whether to do what follows.
The condition after the block decides whether to stop doing what preceded.
Both feel natural, but having a while at the end of the block feels awkward.
Even worse, consider this (non-standard style) code:
    ...
}
while (condition)
{
    ...

Which kind of while is that?
It's impossible to say from the immediate context.
Overloading keywords isn't always a good idea.
---
