Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: do while

Subject: do while

From: Deo

Date: 15 May, 2008 08:43:02

Message: 1 of 14

is there a matlab command equivalent to the C do while

Subject: Re: do while

From: French Caro

Date: 15 May, 2008 09:14:02

Message: 2 of 14

"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: Re: do while

From: Deo

Date: 15 May, 2008 09:23:02

Message: 3 of 14

"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: Re: do while

From: John D'Errico

Date: 15 May, 2008 10:16:02

Message: 4 of 14

"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: Re: do while

From: French Caro

Date: 15 May, 2008 11:44:02

Message: 5 of 14

"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: Re: do while

From: CandyGirl

Date: 15 May, 2008 13:14:04

Message: 6 of 14

Yes there is,


A=5
B=2

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

hope that helped

Subject: Re: do while

From: Dave Robinson

Date: 15 May, 2008 14:31:02

Message: 7 of 14

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: Re: do while

From: Jos

Date: 15 May, 2008 14:39:03

Message: 8 of 14

"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: Re: do while

From: Bryan

Date: 15 May, 2008 14:52:02

Message: 9 of 14

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: Re: do while

From: Steve Amphlett

Date: 15 May, 2008 16:40:19

Message: 10 of 14

"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: Re: do while

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

Date: 15 May, 2008 16:57:42

Message: 11 of 14

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: Re: do while

From: Steven Lord

Date: 15 May, 2008 17:47:51

Message: 12 of 14


"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: Re: do while

From: helper

Date: 15 May, 2008 17:57:01

Message: 13 of 14

> 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: Re: do while

From: Dave Robinson

Date: 16 May, 2008 13:45:03

Message: 14 of 14

"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

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

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics