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

Thread Subject: a problem with break syntax

Subject: a problem with break syntax

From: ehsan mirrahimi

Date: 26 Sep, 2007 14:40:59

Message: 1 of 7

please run this code:

clear;
d=0;
vec=zeros(1,10);vec(2)=3;vec(5)=1;vec(8)=4;
for a=1:10
    while a~=length(vec)+1
        break
    if vec(a)==0 & vec(a+1)~=0
        d=d+1;
        indext(d)=a;
    end
    end
end


the indext vector doses not create in Matlab workspace.
why?

of course I know by chaging the code to :

clear;
d=0;
vec=zeros(1,10);vec(2)=3;vec(5)=1;vec(8)=4;
for a=1:9
        if vec(a)==0 & vec(a+1)~=0
        d=d+1;
        indext(d)=a;
        end
end


the indext vector will create in Matlab workspace.
but i want this vector be create by using while and breake
command.

how sjould i change my source code?by using while.
thanks.

Subject: a problem with break syntax

From: PB

Date: 26 Sep, 2007 16:46:24

Message: 2 of 7

Den Wed, 26 Sep 2007 14:40:59 +0000 skrev ehsan mirrahimi:

> please run this code:
>
> clear;
> d=0;
> vec=zeros(1,10);vec(2)=3;vec(5)=1;vec(8)=4;
> for a=1:10
> while a~=length(vec)+1
> break
> if vec(a)==0 & vec(a+1)~=0
> d=d+1;
> indext(d)=a;
> end
> end
> end
>
>
> the indext vector doses not create in Matlab workspace.
> why?
>
> of course I know by chaging the code to :
>
> clear;
> d=0;
> vec=zeros(1,10);vec(2)=3;vec(5)=1;vec(8)=4;
> for a=1:9
> if vec(a)==0 & vec(a+1)~=0
> d=d+1;
> indext(d)=a;
> end
> end
>
>
> the indext vector will create in Matlab workspace.
> but i want this vector be create by using while and breake
> command.
>
> how sjould i change my source code?by using while.
> thanks.

Why not remove the for, whiles etc and replace it with

d=find(vec)-1

or am I missing something here?

/PB

Subject: a problem with break syntax

From: Adam

Date: 26 Sep, 2007 17:00:58

Message: 3 of 7

PB <pbodin@_REMOVE_THiS_kth.se> wrote in message
<A1wKi.9762$ZA.5842@newsb.telia.net>...
> Den Wed, 26 Sep 2007 14:40:59 +0000 skrev ehsan mirrahimi:
>
> > please run this code:
> >
> > clear;
> > d=0;
> > vec=zeros(1,10);vec(2)=3;vec(5)=1;vec(8)=4;
> > for a=1:10
> > while a~=length(vec)+1
> > break
> > if vec(a)==0 & vec(a+1)~=0
> > d=d+1;
> > indext(d)=a;
> > end
> > end
> > end
> >
> >
> > the indext vector doses not create in Matlab workspace.
> > why?
> >
> > of course I know by chaging the code to :
> >
> > clear;
> > d=0;
> > vec=zeros(1,10);vec(2)=3;vec(5)=1;vec(8)=4;
> > for a=1:9
> > if vec(a)==0 & vec(a+1)~=0
> > d=d+1;
> > indext(d)=a;
> > end
> > end
> >
> >
> > the indext vector will create in Matlab workspace.
> > but i want this vector be create by using while and breake
> > command.
> >
> > how sjould i change my source code?by using while.
> > thanks.
>
> Why not remove the for, whiles etc and replace it with
>
> d=find(vec)-1
>
> or am I missing something here?
>
> /PB

Prob what PB said. To answer your question let's properly
format your code

> > clear;
> > d=0;
> > vec=zeros(1,10);vec(2)=3;vec(5)=1;vec(8)=4;
> > for a=1:10
> > while a~=length(vec)+1
> > break
> > if vec(a)==0 & vec(a+1)~=0
> > d=d+1;
> > indext(d)=a;
> > end
> > end
> > end

So the first time the while statement is satisfied it breaks
out of the loop. Thus your loop SHOULD do nothing as coded.

~Adam

Subject: a problem with break syntax

From: David

Date: 26 Sep, 2007 17:02:29

Message: 4 of 7

"ehsan mirrahimi" <ehsan.mirrahimi@mathworks.com> wrote in
message <fddr1q$svn$1@fred.mathworks.com>...
> please run this code:
>
> clear;
> d=0;
> vec=zeros(1,10);vec(2)=3;vec(5)=1;vec(8)=4;
> for a=1:10
> while a~=length(vec)+1
> break
> if vec(a)==0 & vec(a+1)~=0
> d=d+1;
> indext(d)=a;
> end
> end
> end
>
>
> the indext vector doses not create in Matlab workspace.
> why?

because your code is illogical.

look at this line by line...
> for a=1:10
this is ok, loop for a=1:10
> while a~=length(vec)+1
ok, you will get into the while if a~=length(vec)+1
length(vec)+1 looks like 11 to me, since a=1:10 then you
will always go into the while. so it really doesn't do
anything for you here.
> break
this is the real problem. as soon as you get into the
while loop you break... none of the rest of the code will
be executed between the break and the end of the loop.
what did you think the break was going to do at this
point? There is no use continuing since the code is never
executed.
> if vec(a)==0 & vec(a+1)~=0
> d=d+1;
> indext(d)=a;
> end
> end
> end

this is a good example to learn how to use the debugger
on. you could step through this and immediately see why
you never got to what you wanted to do.

Subject: a problem with break syntax

From: matt

Date: 26 Sep, 2007 17:03:47

Message: 5 of 7

The break statement is performing just fine. In your for
loop, 'a' goes from 1 to 10, so it is never be equal to
length(vec)+1 = 11.
Thus for every 'a' value the while loop will execute and
immediately break, which puts you back in the for loop. So
with that break statement where it is you are basically
doing just this:

clear;
d=0;
vec=zeros(1,10);vec(2)=3;vec(5)=1;vec(8)=4;
for a=1:10
end

exactly as you wrote it!

Subject: a problem with break syntax

From: Peter Boettcher

Date: 26 Sep, 2007 17:04:14

Message: 6 of 7

"ehsan mirrahimi" <ehsan.mirrahimi@mathworks.com> writes:

> please run this code:
>
> clear;
> d=0;
> vec=zeros(1,10);vec(2)=3;vec(5)=1;vec(8)=4;
> for a=1:10
> while a~=length(vec)+1
> break
> if vec(a)==0 & vec(a+1)~=0
> d=d+1;
> indext(d)=a;
> end
> end
> end
>
>
> the indext vector doses not create in Matlab workspace.
> why?

My crystal ball says:

for a=1:10
  if a == 10
    continue;
  end
  if vec(a) == 0 % snip rest of your code
  end
end


-Peter

Subject: a problem with break syntax

From: matt

Date: 26 Sep, 2007 17:26:06

Message: 7 of 7

If you really must use a while and a break, which is not at
all the best way to do this, here is one way. Note this
assumes the output from your second code is the one you want!

clear;
d=1;
vec=zeros(1,10);vec(2)=3;vec(5)=1;vec(8)=4;
while true
    if vec(d)==0 & vec(d+1)~=0
        indext(d) = d;
    end
    d = d+1;
    if d+1 > length(vec)
        break
    end
end
indext = indext(find(indext));

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
debugger Ned Gulley 28 Sep, 2007 17:51:01
break Ned Gulley 28 Sep, 2007 17:51:01
use debugger David 26 Sep, 2007 13:05:08
broken break David 26 Sep, 2007 13:05:08
break syntax problem ehsan mirrahimi 26 Sep, 2007 10:45:03
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