# WHILE loop implementation

**URL:** <https://connect.superhuman.com/t/while-loop-implementation/7367>\
**Category:** Tips and Hacks\
**Created:** [April 4, 2019, 6:59pm UTC](https://connect.superhuman.com/t/while-loop-implementation/7367 "2019-04-04T18:59:25Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Danyliuk](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/paul_danyliuk/32/27228_2.png) [@Paul\_Danyliuk](https://connect.superhuman.com/u/Paul_Danyliuk)\
**Post date:** [April 4, 2019, 6:59pm UTC](https://connect.superhuman.com/t/while-loop-implementation/7367/1 "2019-04-04T18:59:25Z")

</div>

Coda is now officially Turing-complete 😁

Here’s an implementation of a `while` loop that runs as long as the value in the table is not equal to 0. It can be easily modified for any boolean condition by changing the formula in `While condition`.

The hack used here: a button cannot push itself, but it can push another button, which will then push the first button.

https://coda.io/embed/Yp0v_my17s?viewMode=embedplay

---

<div class="post-metadata">

**Author:** ![Jean\_Pierre\_Traets](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/jean_pierre_traets/32/16275_2.png) [@Jean\_Pierre\_Traets](https://connect.superhuman.com/u/Jean_Pierre_Traets)\
**Post date:** [April 4, 2019, 7:08pm UTC](https://connect.superhuman.com/t/while-loop-implementation/7367/2 "2019-04-04T19:08:13Z")

</div>

Dear @Paul_Danyliuk,

Thank you sharing your creative approach and thereby supporting the “out of the box thinking” ⛲

Amicable,  
JP

---

<div class="post-metadata">

**Author:** ![shishir](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/shishir/32/6017_2.png) [@shishir](https://connect.superhuman.com/u/shishir)\
**Post date:** [April 4, 2019, 9:23pm UTC](https://connect.superhuman.com/t/while-loop-implementation/7367/3 "2019-04-04T21:23:50Z")

</div>

Ha nice!

Btw, in case you didn’t find it, a commonly used tricky for a `FOR()` loop is to use the `Sequence()` formula and `FormulaMap()` - e.g. `FormulaMap(Sequence(1, 10), CurrentValue+1)` or `FormulaMap(Contacts, Gmail::SendEmail(... CurrentValue ...))`

---

<div class="post-metadata">

**Author:** ![Paul\_Danyliuk](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/paul_danyliuk/32/27228_2.png) [@Paul\_Danyliuk](https://connect.superhuman.com/u/Paul_Danyliuk)\
**Post date:** [April 4, 2019, 9:25pm UTC](https://connect.superhuman.com/t/while-loop-implementation/7367/4 "2019-04-04T21:25:27Z")

</div>

I did, and I posted a link in that thread to here.

The problem with Sequence/FormulaMap approach is that it only works for specified number of iteration, whereas `while` loops can have dynamic condition.

Hold on, I’m about to post another doc show-and-tell right now.

---

<div class="post-metadata">

**Author:** ![Dalmo\_Mendonca](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/dalmo_mendonca/32/5813_2.png) [@Dalmo\_Mendonca](https://connect.superhuman.com/u/Dalmo_Mendonca)\
**Post date:** [April 4, 2019, 11:30pm UTC](https://connect.superhuman.com/t/while-loop-implementation/7367/5 "2019-04-04T23:30:54Z")

</div>

Very cool!

I suppose you could separate the check from the action button for clarity.

Also where’s the panic button? I just broke Coda, it’s counting to -Inf ! 😛

---

<div class="post-metadata">

**Author:** ![Paul\_Danyliuk](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/paul_danyliuk/32/27228_2.png) [@Paul\_Danyliuk](https://connect.superhuman.com/u/Paul_Danyliuk)\
**Post date:** [April 5, 2019, 6:53pm UTC](https://connect.superhuman.com/t/while-loop-implementation/7367/6 "2019-04-05T18:53:43Z")

</div>

Thanks. Updated the example.

No panic button in this demo 🙂 but you can manually set the counter value to above zero to terminate the loop eventually.

Here’s what I did with this newly acquired knowledge, lol:

> [@Brainfuck interpreter on Coda](https://connect.superhuman.com/t/brainfuck-interpreter-on-coda/7368):
>
> I’ve been enjoying Coda for the last two weeks. There’s only one thing I found lacking in this no-code tool. Guess which one? … Some code! So I went on and wrote a [Brainfuck](https://en.wikipedia.org/wiki/Brainfuck) interpreter, in Coda! Now you can code while you no-code! slight_smile (A gentle reminder that I have two submissions in this year’s [Makers Festival](https://www.producthunt.com/makers-festival-2019/voting): Paul’s Plan and Tiny chat bot. Please check them out. You can find the chat bot [here as well](https://connect.superhuman.com/t/chat-bot-engine/7317/2))

---

<div class="post-metadata">

**Author:** ![cblock](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/cblock/32/6263_2.png) [@cblock](https://connect.superhuman.com/u/cblock)\
**Post date:** [September 21, 2019, 12:41am UTC](https://connect.superhuman.com/t/while-loop-implementation/7367/7 "2019-09-21T00:41:20Z")

</div>

This is my new favorite thread on this forum. What a laugh!

---

<div class="post-metadata">

**Author:** ![Paul\_Danyliuk](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/paul_danyliuk/32/27228_2.png) [@Paul\_Danyliuk](https://connect.superhuman.com/u/Paul_Danyliuk)\
**Post date:** [September 21, 2019, 6:51am UTC](https://connect.superhuman.com/t/while-loop-implementation/7367/8 "2019-09-21T06:51:27Z")

</div>

Why?  
This actually proved to be very useful. E.g., if you need to make two or more nested loops (e.g. you have two lists and need to make MxN rows for a cross-join), the only way to do it for now is via my WHILE implementation and a backing table to store current indices. That’s because we cannot access outer `CurrentValue`. I’ll make a demo on this when I get back from vacation.

UPD: it’s also possible to do it in one `Sequence().FormulaMap()` loop if we know M and N in advance. But even then it gets very impractical if you have 3 and more lists.

Example scenario I’m talking about: you have students, you have courses, and each course has variable number of lessons. You need to make rows where you link each student to each lesson directly.

---

<div class="post-metadata">

**Author:** ![GJ\_Roelofs](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/gj_roelofs/32/9171_2.png) [@GJ\_Roelofs](https://connect.superhuman.com/u/GJ_Roelofs)\
**Post date:** [September 21, 2019, 12:03pm UTC](https://connect.superhuman.com/t/while-loop-implementation/7367/9 "2019-09-21T12:03:42Z")

</div>

@Paul_Danyliuk  
**I wholeheartedly commend the solution and creativity**.

I suspect the laugh is mostly for the fact that this is required to handle these types of things - the fact that there is a legitimate need indicates it should be something that exists as a top-level function.

I myself used `RegexExtract` to get outer `CurrentValue` access so I could do `GroupBy()`. (I.e.: Map the indices to a: “X1-Y1,…,Xn-Yn” pattern, then used `((\d+)-[^,]+,*(\2-[^,]+,)*)`, `"gm"` to group and extract separate lists.)

And… I was absolutely frustrated by the mental exercise required to get the functionality I need as I needed to get things done, not have to reinvent the concept of `GroupBy`. I don’t want to have to jump through these hoops to do these things, nor have to implement what is essentially a manual-memory-using-multiple-tables turing machine.

And that is not even to mention the fact that you really don’t want to have this in a production document other people will touch.

---

<div class="post-metadata">

**Author:** ![Paul\_Danyliuk](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/paul_danyliuk/32/27228_2.png) [@Paul\_Danyliuk](https://connect.superhuman.com/u/Paul_Danyliuk)\
**Post date:** [September 26, 2019, 8:21pm UTC](https://connect.superhuman.com/t/while-loop-implementation/7367/10 "2019-09-26T20:21:37Z")

</div>

@GJ_Roelofs @cblock maybe you’ll also like this? 😂

> [@Granular access control! (not the one we need, but the one we deserve)](https://connect.superhuman.com/t/granular-access-control-not-the-one-we-need-but-the-one-we-deserve/10852):
>
> As you already know, Coda still doesn’t have [granular permissions](https://connect.superhuman.com/t/granular-permissions/169). So what’s the better option than to take the matter into my own hands and create my own implementation of access control? Presenting… ACCESS CONTROL DEMO v.0.1! The idea is simple: crash the doc when a certain condition is not met joy For example: If the user is not from the list of authorized users — checking for User() != ... If document URL is not the one of the original document — checking for thisDocument.Url().EndsW…

---

<div class="post-metadata">

**Author:** ![GJ\_Roelofs](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/gj_roelofs/32/9171_2.png) [@GJ\_Roelofs](https://connect.superhuman.com/u/GJ_Roelofs)\
**Post date:** [September 26, 2019, 8:53pm UTC](https://connect.superhuman.com/t/while-loop-implementation/7367/11 "2019-09-26T20:53:23Z")

</div>

If only Windows had this level of operational security.

---

<div class="post-metadata">

**Author:** ![cblock](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/cblock/32/6263_2.png) [@cblock](https://connect.superhuman.com/u/cblock)\
**Post date:** [September 26, 2019, 9:08pm UTC](https://connect.superhuman.com/t/while-loop-implementation/7367/12 "2019-09-26T21:08:05Z")

</div>

![useless-machines](https://global.discourse-cdn.com/coda1/original/2X/8/8036dff9b00296824a805b414d249c258cceb10d.gif)

That’s why!

(This is not to say that I do not appreciate the work here. I find it amusing, but also impressive and useful. )

---

<div class="post-metadata">

**Author:** ![Max\_OBrien](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/max_obrien/32/49285_2.png) [@Max\_OBrien](https://connect.superhuman.com/u/Max_OBrien)\
**Post date:** [December 24, 2021, 11:23pm UTC](https://connect.superhuman.com/t/while-loop-implementation/7367/13 "2021-12-24T23:23:19Z")

</div>

https://coda.io/embed/vbAvJB_kVP/_sutER?viewMode=embedplay&hideSections=true
