Thread Subject: do while

Subject: do while

From: Deo

Date: 15 May, 2008 08:43:02

Message: 1 of 20

is there a matlab command equivalent to the C do while

Subject: do while

From: French Caro

Date: 15 May, 2008 09:14:02

Message: 2 of 20

"Deo " <spliers@hotmail.com> wrote in message
<g0gt2m$h3q$1@fred.mathworks.com>...
> is there a matlab command equivalent to the C do while
help while

Subject: do while

From: Deo

Date: 15 May, 2008 09:23:02

Message: 3 of 20

"French Caro " <caro95470@nospam.free.fr> wrote in message
<g0gusq$425$1@fred.mathworks.com>...
> "Deo " <spliers@hotmail.com> wrote in message
> <g0gt2m$h3q$1@fred.mathworks.com>...
> > is there a matlab command equivalent to the C do while
> help while
That was the first place I looked and there was nothing
that could help me

Subject: do while

From: John D'Errico

Date: 15 May, 2008 10:16:02

Message: 4 of 20

"Deo " <spliers@hotmail.com> wrote in message
<g0gvdm$5tj$1@fred.mathworks.com>...
> "French Caro " <caro95470@nospam.free.fr> wrote in message
> <g0gusq$425$1@fred.mathworks.com>...
> > "Deo " <spliers@hotmail.com> wrote in message
> > <g0gt2m$h3q$1@fred.mathworks.com>...
> > > is there a matlab command equivalent to the C do while
> > help while
> That was the first place I looked and there was nothing
> that could help me

Then you did not look hard enough.

Look again. Read the examples.

John

Subject: do while

From: French Caro

Date: 15 May, 2008 11:44:02

Message: 5 of 20

"Deo " <spliers@hotmail.com> wrote in message
<g0gvdm$5tj$1@fred.mathworks.com>...
> "French Caro " <caro95470@nospam.free.fr> wrote in message
> <g0gusq$425$1@fred.mathworks.com>...
> > "Deo " <spliers@hotmail.com> wrote in message
> > <g0gt2m$h3q$1@fred.mathworks.com>...
> > > is there a matlab command equivalent to the C do while
> > help while
> That was the first place I looked and there was nothing
> that could help me

Can you explain more ?
I don't know about "do while" in C but I think that it
should be the same as "while" in MATLAB.

Caroline

Subject: do while

From: CandyGirl

Date: 15 May, 2008 13:14:04

Message: 6 of 20

Yes there is,


A=5
B=2

while(A > B)
C = (A+B)/2
B=B+1
end

hope that helped

Subject: do while

From: Dave Robinson

Date: 15 May, 2008 14:31:02

Message: 7 of 20

CandyGirl <Rasha@dqnetwork.com> wrote in message
<16944196.1210857274898.JavaMail.jakarta@nitrogen.mathforum.
org>...
> Yes there is,
>
>
> A=5
> B=2
>
> while(A > B)
> C = (A+B)/2
> B=B+1
> end
>
> hope that helped

The 'C' family has both
while(test)
{

}

and

do
{

}while(test)

they are different. With the while loop, if test is false
the code in the curlies is bypassed, if it is true it is
continuously looped until it becomes false, With the do -
while construct the code in the curlies is always executed
at least once, then the value of test controls whether the
code is reexecuted, the two constructs are slightly
different animals.

There is probably a much better way of doing it, but
perhaps the following abomination might give you the
functionality you need.

while(true) %Loop forever
    Your code
    if(!test)
      break;
    endif
endwhile

Regards

Dave Robinson

Subject: do while

From: Jos

Date: 15 May, 2008 14:39:03

Message: 8 of 20

"Deo " <spliers@hotmail.com> wrote in message
<g0gt2m$h3q$1@fred.mathworks.com>...
> is there a matlab command equivalent to the C do while

There is a subtle difference between a "do-while" loop and
"while" loop:
do-while executes the loop at least once, whereas while may
skip the loop altogether

A work-around:

% an infinite for-loop
for i=1:Inf,
   % ... code ...
   if ~condition,
     break ; % break out of the for-loop
   end
end

hth
Jos

Subject: do while

From: Bryan

Date: 15 May, 2008 14:52:02

Message: 9 of 20

Couldn't you also just do the following:

%execute commands

while (test)
  %execute commands
end

By executing the code first outside the while loop, you're
ensuring that it gets executed once even if test is never
true, just as a do-while loop will do. You'll probably
have to change the (test) part of the loop a bit, but you
should be able to get the functionality you want with a
simple while loop.

Subject: do while

From: Steve Amphlett

Date: 15 May, 2008 16:40:19

Message: 10 of 20

"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

Subject: do while

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 15 May, 2008 16:57:42

Message: 11 of 20

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
--
   So you found your solution
   What will be your last contribution?
   -- Supertramp (Fool's Overture)

Subject: do while

From: Steven Lord

Date: 15 May, 2008 17:47:51

Message: 12 of 20


"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

Subject: do while

From: helper

Date: 15 May, 2008 17:57:01

Message: 13 of 20

> Alternately, you could use this:
>
> done = false;
> while ~done
> % your code
> if ~condition
> done = true;
> end
> end


Or another method

while true
  % your code
  if condition
    break
  end
end

Subject: do while

From: Dave Robinson

Date: 16 May, 2008 13:45:03

Message: 14 of 20

"helper " <spamless@nospam.com> wrote in message
<g0hthd$m6b$1@fred.mathworks.com>...
> > Alternately, you could use this:
> >
> > done = false;
> > while ~done
> > % your code
> > if ~condition
> > done = true;
> > end
> > end
>
>
> Or another method
>
> while true
> % your code
> if condition
> break
> end
> end

Am I missing something - how does this differ from my
original abomination?

Regards

Dave Robinson

Subject: do while

From: Jef Statham

Date: 12 Aug, 2009 18:36:04

Message: 15 of 20

"Deo " <spliers@hotmail.com> wrote in message <g0gt2m$h3q$1@fred.mathworks.com>...
> is there a matlab command equivalent to the C do while

I thought this was a clever work around

a = 1
b = 1
for x=(a == b)-1:0
    if (not done)
        x = x-1;
    end
end

Subject: do while

From: someone

Date: 12 Aug, 2009 21:49:03

Message: 16 of 20

"Dave Robinson" <dave.robinson@somewhere.biz> wrote in message <g0k34v$4kb$1@fred.mathworks.com>...
> "helper " <spamless@nospam.com> wrote in message
> <g0hthd$m6b$1@fred.mathworks.com>...
> > > Alternately, you could use this:
> > >
> > > done = false;
> > > while ~done
> > > % your code
> > > if ~condition
> > > done = true;
> > > end
> > > end
> >
> >
> > Or another method
> >
> > while true
> > % your code
> > if condition
> > break
> > end
> > end
>
> Am I missing something - how does this differ from my
> original abomination?
>
> Regards
>
> Dave Robinson

Logically, there is no difference.

The latter code just avoids the use of ~.

Subject: do while

From: Jan Simon

Date: 12 Aug, 2009 23:23:03

Message: 17 of 20

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

Subject: do while

From: Jan Simon

Date: 12 Aug, 2009 23:29:02

Message: 18 of 20

Dear Jef Statham!

> I thought this was a clever work around
>
> a = 1
> b = 1
> for x=(a == b)-1:0
> if (not done)
> x = x-1;
> end
> end

What do you expect to happen?
  for x=(a==b)-1:0
is the same as:
  for x=0:0
So the [a] and [b] can be omitted.
Then "x = x - 1" reduces the value of [x], but this does not influence the loop counter! Try it:
  for x = 1:5, x = x - 1; disp(x); end
This displays the numbers from 0 to 4 and stops afterwards.

Kind regards, Jan

Subject: do while

From: Andy

Date: 28 Oct, 2009 17:56:02

Message: 19 of 20

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message
> I've never seen a thread with so many almost equal and perfectly equivalent solutions.

Agreed. Now I'd like to add another by very slightly modifying your variable naming:

> not_done = true;
> while not_done
> % your code
> not_done = condition;
> end

Andy

Subject: do while

From: Jan Simon

Date: 29 Oct, 2009 08:47:01

Message: 20 of 20

Dear Andy!

> Agreed. Now I'd like to add another by very slightly modifying your variable naming:
>
> > not_done = true;
> > while not_done
> > % your code
> > not_done = condition;
> > end

You are right! Thanks.
"while done" is absolutely correct in Matlab, but absolutely rubbish for human readers - except that they've stopped to think as human and started to think as a compiler. A really important debugging strategy is the usage of meaningful names for variables. I'm ashamed, that I've posted the "done" here.

But... e.g. at
  http://geosoft.no/development/cppstyle.html
you find:
> 30. Negated boolean variable names must be avoided.
> bool isError; // NOT: isNoError bool isFound; // NOT: isNotFound
> The problem arises when such a name is used in conjunction with the
> logical negation operator as this results in a double negative. It is not
>immediately apparent what !isNotFound means.

So what could the ideal name?
  go_on, goOn: Looks like a goon.
  proceed: no hint that this is logical, better doProceed.
  keepDoing: ?!
If the loop has more than 10 lines, I'd prefer a more specific name, e.g. beforeEOF, belowXLimit, beforeTeaTime, running, ...

Of course I love (and never use, because it is not backward compatible to the 31 char limit of Matlab 5.3):
 while whileConditionIsStillTrueAndNoProblemsOccurredYet ... :-)

By the way: The mentioned Cpp style guide recommends to formulate all DO-WHILE loops as WHILE loops to reduce the complexity...

Jan

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
code Deo 15 May, 2008 04:45:10
rssFeed for this Thread

Contact us at files@mathworks.com