Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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 :)

share|improve this question

closed as off-topic by Mr.Wizard Mar 19 at 9:56

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Mr.Wizard
If this question can be reworded to fit the rules in the help center, please edit the question.

2  
While[(procedure;test)] - parens. optional, makes it clear I think... –  rasher Mar 18 at 10:06
    
@rasher oh yes, it will work, but still it is rather abusing the syntax than following the docs way, isn't it? Nontheless, I will gladly upvote this answer :) –  Kuba Mar 18 at 10:08
2  
Not sure how a compound statement is abuse...first example under scope in the docs does it in compact form. –  rasher Mar 18 at 10:12
    
@rasher Agree, abuse is not a good word. I mean, docs say first arg is a test which may be missleading and the example you are referring to is ok but formed in easy-to-overlook way. Especially for new users. I think it could be an useful topic for future visitors. –  Kuba Mar 18 at 10:17
1  
@rasher To the conclusion: I don't think there should be a separate function. My goal is to show to future beginners :), who may have doubts similar to mine in the past, that this is the way. –  Kuba Mar 18 at 10:29

4 Answers 4

up vote 4 down vote accepted

As Rasher has noticed, the second argument can be omitted (Details & Options) so, yes, this is the way to go:

While[(procedure; test)] 

As I've pointed out in the comments, thr documentation could be confusing for beginners, so this question might be useful to future visitors. This is only my opinion so one may want to vote to close this topic.

share|improve this 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.

share|improve this answer

Why not write your own and place it in your init.m file?

SetAttributes[DoWhile, HoldAll];

DoWhile[procedure_, test_] := While[procedure; test]
share|improve this answer
    
One point against is that the code with it posted on SE requires explanation :) –  Kuba Mar 18 at 16:24
    
@Kuba what do you mean? I'm afraid I don't understand. –  Chip Hurst Mar 18 at 18:36
    
If I put that in my init file and then post the answer here, using this function, I will have to add definition :) –  Kuba Mar 18 at 18:37
    
Oh haha, got it! –  Chip Hurst Mar 18 at 18:37

Here's another way to write your own:

SetAttributes[doWhile, HoldAll];    
doWhile[expr_, test_] := CompoundExpression[expr, While[test, expr]]

Or just:

doWhile[expr_, test_] := (expr; While[test, expr])
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.