Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"do while" loops #567

Open
josepharhar opened this issue Apr 25, 2023 · 5 comments
Open

"do while" loops #567

josepharhar opened this issue Apr 25, 2023 · 5 comments

Comments

@josepharhar
Copy link

The current infra spec says you can have "while" and "for each" loops, but there are no "do while" loops: https://infra.spec.whatwg.org/#algorithm-iteration
I am trying to spec something that uses a "do while" loop, can we add do while loops here? Or is there another way to spec these?

@annevk @domenic

@domenic
Copy link
Member

domenic commented Apr 26, 2023

We could certainly add them. In the meantime I think you should feel free to use them in specs: the intent is pretty clear to any reader, and in general Infra is not meant to be a restriction on what you write, just a standard library so that if multiple people are writing the same thing, they do so the same way.

The spec-ese wording is a bit unclear to me: if you just wrote:

1. Do:

   1. X
   2. Y

  while Z.

the nature of English makes me think that this might just be a fancy way of writing

1. While Z, do:
   
   1. X
   2. Y

So maybe something like

1. Perform the following steps at least once:

   1. X
   2. Y
  
  and keep performing them while Z.

would be sufficiently clear.

I should also mention you can replace any do-while loop with a while loop, using a construct like

1. Let firstIteration be true.
2. While firstIteration is true or Z:
   1. Set firstIteration to false.
   2. X
   3. Y

which may be why languages like Python and Ruby don't have them.

@domenic
Copy link
Member

domenic commented Apr 26, 2023

https://stackoverflow.com/q/994905 is somewhat relevant.

@annevk
Copy link
Member

annevk commented Apr 26, 2023

I find "do while" loops a bit confusing so I'd prefer the explicit construct around "while", but I guess I could get used to them if they are defined and "Do" is xref'd.

@domenic
Copy link
Member

domenic commented Apr 26, 2023

@annevk , what'd you think of my more verbose phrasing ("...at least once... And keep performing them...")

@annevk
Copy link
Member

annevk commented Apr 26, 2023

Yeah, fair. Nit: s/the following/these/ and s/perform/run/ unless we want to switch everything from run to perform...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants