Path: news.mathworks.com!not-for-mail
From: "Steven Lord" <slord@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: do while
Date: Thu, 15 May 2008 13:47:51 -0400
Organization: The MathWorks, Inc.
Lines: 47
Message-ID: <g0ht07$k7a$1@fred.mathworks.com>
References: <g0gt2m$h3q$1@fred.mathworks.com> <g0hp1j$k93$1@fred.mathworks.com> <g0hq26$q1o$1@canopus.cc.umanitoba.ca>
Reply-To: "Steven Lord" <slord@mathworks.com>
NNTP-Posting-Host: lords.dhcp.mathworks.com
X-Trace: fred.mathworks.com 1210873671 20714 144.212.105.187 (15 May 2008 17:47:51 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 15 May 2008 17:47:51 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198
Xref: news.mathworks.com comp.soft-sys.matlab:468655



"Walter Roberson" <roberson@ibd.nrc-cnrc.gc.ca> wrote in message 
news:g0hq26$q1o$1@canopus.cc.umanitoba.ca...
> In article <g0hp1j$k93$1@fred.mathworks.com>,
> Steve Amphlett <Firstname.Lastname@Where-I-Work.com> wrote:
>>"Deo " <spliers@hotmail.com> wrote in message
>><g0gt2m$h3q$1@fred.mathworks.com>...
>>> is there a matlab command equivalent to the C do while
>
>>Another abomination:
>
>>first=true;
>>while(condition || first)
>
>>% stuff
>
>>first=false;
>>end
>
> Evaluating condition might have side effects. To match do while
> those side effects should not be triggered on the first trip.
> Therefore the above should be rewritten to
>
> first=true;
> while(first || condition)
> % stuff
> first=false;
> end

Alternately, you could use this:

done = false;
while ~done
    % your code
    if ~condition
        done = true;
    end
end

The condition isn't evaluated until after the loop has executed at least 
once, and it's evaluated after the loop execution each subsequent time.

-- 
Steve Lord
slord@mathworks.com