<p>An until loop construct <code>until (x)</code> can almost always be achieved with a while loop <code>while (!x)</code>. What's a possible reasoning behind having it available when a language also provides a while loop?</p>

<language-design><while-loop>
What are the advantages of providing an until loop construct?
CC BY-SA 4.0
2023-05-19T19:28:08.937Z
By "until loop" do you mean do-while loop?
Ginger
CC BY-SA 4.0
2023-05-19T19:31:02.977Z
@Ginger `until(x)` means `while(!x)`.
chrisaycock
CC BY-SA 4.0
2023-05-19T19:33:08.570Z
I see it the same way I see [Perl's `unless`](https://www.perltutorial.org/perl-unless/). As in, just more filler.
chrisaycock
CC BY-SA 4.0
2023-05-19T19:56:08.197Z
Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
Community Compiler
CC BY-SA 4.0
2023-05-20T14:02:19.867Z
Although Ginger's answer is an excellent one, this really just comes down to aesthetics.
Unrelated String
CC BY-SA 4.0
2023-05-21T09:40:39.830Z
I only know two languages that have an until loop: PARI/GP and Haskell. In PARI/GP, `until` is in fact do-while-not, but there isn't an do-while loop. Haskell doesn't even have a while loop, and `until` is a function of type `(a -> Bool) -> (a -> a) -> a -> a`.
alephalpha
CC BY-SA 4.0
2023-05-24T08:29:39.920Z
@UnrelatedString [And that’s ok!](https://languagedesign.meta.stackexchange.com/a/220/269)
The Empty String Photographer
<p>In addition to Ginger's answer, I have 2 things to add:</p>
<ol>
<li>Avoiding some errors. They can prevent errors from a mistakenly negated condition in a while loop.</li>
<li>Readability. It is more clear the programmers intention, making the code more readable.</li>
</ol>

CC BY-SA 4.0
2023-05-19T20:12:52.327Z
2023-05-31T10:45:16.120Z
2023-05-31T10:45:16.120Z
Starship
<h1><code>while let</code> and <code>until let</code></h1>
<p>In Swift, <code>while let</code> allows you to call a function repeatedly and loops until it returns nil:</p>
<pre class="lang-swift prettyprint-override"><code>// Simple cat program. readLine (builtin) reads a single line from stdin, and
// returns nil when there's no input left.
while let line = readLine() {
  // Because we made it here, we know line is not nil.
  print(line)
}
// readLine returned nil, so we're done.
</code></pre>
<p>But what if you want to loop until something returns <em>non</em>-nil?</p>
<pre class="lang-swift prettyprint-override"><code>var next: Task?
repeat {
  next = getNextTask()
} while next == nil
guard let next else { fatalError(&quot;unreachable&quot;) }
</code></pre>
<p>Blech, that's a mess. If Swift had <code>until let</code>, this code would be simpler:</p>
<pre class="lang-swift prettyprint-override"><code>until let task = getNextTask() {
  // task isn't usable here; even if it were, its value would always be nil.
}
// task is usable here, and known to not be nil.
</code></pre>

CC BY-SA 4.0
2023-05-20T15:42:14.117Z
2023-05-20T15:42:14.117Z
Bbrk24
CC BY-SA 4.0
2023-05-20T15:44:07.970Z
This would function basically the same as rusts `loop {if task = getNextTask(){break task} }` construct
mousetail 'he-him'
CC BY-SA 4.0
2023-05-20T15:45:15.753Z
That's true, but Swift loops are not expressions, and you can't `break` with a value.
Bbrk24
CC BY-SA 4.0
2023-05-20T15:45:55.840Z
Your `until let` basically turns the loop into a expression with immediate assignment
mousetail 'he-him'
<h1>Code Golf</h1>
<p>In Code Golf, where most of us come from, you try to make your code as short as possible. <code>until x</code> is shorter than <code>while not x</code> and is also shorter than <code>while !x</code>.</p>

CC BY-SA 4.0
2023-05-24T08:27:08.783Z
2023-05-24T08:32:34.507Z
2023-05-24T08:32:34.507Z
The Empty String Photographer
<h2>Run the block at least once</h2>
<p>In languages that use keywords to delimit blocks (rather than braces or indentation), e.g. <code>If cond</code>…<code>Endif</code> and <code>While cond</code>…<code>EndWhile</code>, a <code>Repeat</code>…<code>Until cond</code> provides a way to get the block run at least once. A language could even allow <code>While condA</code>…<code>Until condB</code> for full control. Putting an &quot;until&quot; after a block also feels quite natural, as in &quot;do this until that happens&quot;. An example of a language that has this <a href="https://help.dyalog.com/latest/#Language/Control%20Structures/while.htm" rel="nofollow noreferrer">is APL</a>, e.g.:</p>
<pre><code>:While 100&gt;i
:AndIf 100&gt;j
    (i j)←foo i j
:Until 100&lt;i+j
:OrIf i&lt;0
:OrIf j&lt;0
</code></pre>

CC BY-SA 4.0
2023-05-31T08:43:43.930Z
2023-05-31T08:43:43.930Z
Adám
<h1>Clarity in beginner languages</h1>
<p>A construct like this could be helpful in a language designed for new programmers, like Scratch. &quot;until x&quot; may sound more natural than &quot;while not x&quot; to a new developer.</p>

CC BY-SA 4.0
2023-05-19T19:35:06.017Z
2023-05-19T19:35:06.017Z
Ginger
CC BY-SA 4.0
2023-05-19T19:35:29.203Z
I admit my logic here is rather tenuous, but I can't think of any other good reason to have this construct.
Ginger
CC BY-SA 4.0
2023-05-19T20:22:36.627Z
Some languages have a `unless` keyword for the same purpose
mousetail 'he-him'
# What are the advantages of providing an until loop construct?
Posted by: username_0 on 2023-05-19
An until loop construct `until (x)` can almost always be achieved with a while loop `while (!x)`. What's a possible reasoning behind having it available when a language also provides a while loop?
**Tags:** language-design while-loop
> **Comments:**
> **username_1**: By "until loop" do you mean do-while loop?  
> **username_2**: @Ginger `until(x)` means `while(!x)`.  
> **username_2**: I see it the same way I see [Perl's `unless`](https://www.perltutorial.org/perl-unless/). As in, just more filler.  
> **username_3**: Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.  
> **username_4**: Although Ginger's answer is an excellent one, this really just comes down to aesthetics.  
> **username_5**: I only know two languages that have an until loop: PARI/GP and Haskell. In PARI/GP, `until` is in fact do-while-not, but there isn't an do-while loop. Haskell doesn't even have a while loop, and `until` is a function of type `(a -> Bool) -> (a -> a) -> a -> a`.  
> **username_6**: @UnrelatedString [And that’s ok!](https://languagedesign.meta.stackexchange.com/a/220/269)  
---
## 5 answers
### Answer by username_1 on 2023-05-19. Score: 9
# Clarity in beginner languages

A construct like this could be helpful in a language designed for new programmers, like Scratch. "until x" may sound more natural than "while not x" to a new developer.
> **Comments:**
> **username_1** (2023-05-19): I admit my logic here is rather tenuous, but I can't think of any other good reason to have this construct.  
> **username_9** (2023-05-19): Some languages have a `unless` keyword for the same purpose  
---
### Answer by username_8 on 2023-05-20. Score: 7
while let and until let
In Swift, while let allows you to call a function repeatedly and loops until it returns nil:
// Simple cat program. readLine (builtin) reads a single line from stdin, and
// returns nil when there's no input left.
while let line = readLine() {
  // Because we made it here, we know line is not nil.
  print(line)
}
// readLine returned nil, so we're done.

But what if you want to loop until something returns non-nil?
var next: Task?
repeat {
  next = getNextTask()
} while next == nil
guard let next else { fatalError("unreachable") }

Blech, that's a mess. If Swift had until let, this code would be simpler:
until let task = getNextTask() {
  // task isn't usable here; even if it were, its value would always be nil.
}
// task is usable here, and known to not be nil.
> **Comments:**
> **username_9** (2023-05-20): This would function basically the same as rusts `loop {if task = getNextTask(){break task} }` construct  
> **username_8** (2023-05-20): That's true, but Swift loops are not expressions, and you can't `break` with a value.  
> **username_9** (2023-05-20): Your `until let` basically turns the loop into a expression with immediate assignment  
---
### Answer by username_7 on 2023-05-19. Score: 5
In addition to Ginger's answer, I have 2 things to add:

1. Avoiding some errors. They can prevent errors from a mistakenly negated condition in a while loop.
2. Readability. It is more clear the programmers intention, making the code more readable.
---
### Answer by username_6 on 2023-05-24. Score: 3
# Code Golf

In Code Golf, where most of us come from, you try to make your code as short as possible. `until x` is shorter than `while not x` and is also shorter than `while !x`.
---
### Answer by username_10 on 2023-05-31. Score: 2
Run the block at least once
In languages that use keywords to delimit blocks (rather than braces or indentation), e.g. If cond…Endif and While cond…EndWhile, a Repeat…Until cond provides a way to get the block run at least once. A language could even allow While condA…Until condB for full control. Putting an "until" after a block also feels quite natural, as in "do this until that happens". An example of a language that has this is APL, e.g.:
:While 100>i
:AndIf 100>j
    (i j)←foo i j
:Until 100
---
