return or break in nested loop?

Hi everybody,
I have written the codes below :
for i=1:25
if isempty(in_x)
return
else
int = int+1;
aa = in_x-in_x(1);
bb = find(aa>((SOP+SPH)*dsr/SL));
if isempty(bb)
return
else
int = int+1;
ll = in_x(bb(1):size(in_x,2))-in_x(bb(1));
bb2 = find(ll>((SOP+SPH)*dsr/SL));
if isempty(bb2)
return
else
int = int+1;
end
end
end
end
I wanna know why when the first condition satisfies, by the return command it goes to the last "end" which is not the corresponding "end" and it should go to the first "end".
is there any other command which i could use instead of return?

1 Commento

minoo
minoo il 15 Giu 2011
I mean my "for loop" ends when first condition satisfies, but i want to do this for 25 times for 25 different data sets.

Accedi per commentare.

 Risposta accettata

David Young
David Young il 15 Giu 2011
If your code is a script (not part of a function definition), then "return" returns control to the keyboard - that is, it is like a jump to the end of the script. So the answer to your first question is: it's doing what it is supposed to do.
The answer to your second question is yes, you could use the "break" command if you want to exit the inner loop. You mention this possibility in your question title.
If your problem is that you want to go to the next case if in_x is empty, then just simplify the code to
for i=1:25
if ~isempty(in_x)
int = int+1;
% etc.
end
end
But note that your code will need to do something to change the value of in_x on each iteration.

3 Commenti

minoo
minoo il 15 Giu 2011
thanks for your help. I replaced return command with break but my problem still exist. it just do the "for" loop for one time not for 25 times. can you help me to solve this problem?
David Young
David Young il 15 Giu 2011
See the last part of my answer, which has a suggestion for this.
minoo
minoo il 15 Giu 2011
thanks David for your help.it works well

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by