Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: do while
Date: Wed, 12 Aug 2009 23:23:03 +0000 (UTC)
Organization: Universit&#228;t Heidelberg
Lines: 23
Message-ID: <h5visn$9l6$1@fred.mathworks.com>
References: <g0gt2m$h3q$1@fred.mathworks.com> <g0hp1j$k93$1@fred.mathworks.com> <g0hq26$q1o$1@canopus.cc.umanitoba.ca> <g0ht07$k7a$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1250119383 9894 172.30.248.38 (12 Aug 2009 23:23:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 12 Aug 2009 23:23:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 869888
Xref: news.mathworks.com comp.soft-sys.matlab:562845


Dear Steven Lord!

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

I've never seen a thread with so many almost equal and perfectly equivalent solutions. Nevertheless, I'd like to remove some bits from Steven's version, because I'm hoping, that this is the ultimatively most compact formulation:

done = true;
while done
    % your code
    done = condition;
end

There is just one difference to a DO-WHILE loop (if it would exist in Matlab): The operators & and | work *with* short-circuting in the condition of IF and WHILE (and therefore also in DO-WHILE), if the arguments are scalar. Outside IF or WHILE, or for non-scalar arguments, no short-circuting is performed. So be careful, if your <condition> contains & or | operators, the arguments have side effects and they are scalar or even worse: their size changes dynamically due to user input!

Kind regards, Jan