Have I missed something or is there no built-in which mimics the behaviour of do ... while loop?
I am looking for a construct that evaluates procedure
once and then repeatedly evaluates it while test
is not fulfilled.
I'm ended up using:
While[procedure; test, {}]
But clearly the second argument is redundant, so I have a feeling that I'm missing something. Maybe not, but I just want to be sure :)
Answer
While While[procedure; test]
works, it looks very similar to While[test, procedure]
. The only difference is ;
vs ,
. While
is not the most commonly used construct, so when used like this there's a high chance of misunderstanding/misreading.
If readability/reliability is a concern (for example a collaboratively developed published package), I'd use the longer but clearer
While[True,
procedure;
If[Not[test], Break[]]
]
The only argument here is readability and "defensive programming" (extra effort to avoid accidental problems). Readability is subjective. If you are the only person who writes/reads the code and you get used to this use of While
(and thus always pay special attention to the ,
vs ;
) then this argument doesn't apply.
Comments
Post a Comment