How to negate an outer loop when you have nested loops?

2 views (last 30 days)
I have a series of nested loops in my program. I would like to be able to type something like, "Radfrac_min=1; Radfrac_step=0; Radfrac_max=1; and just essentially ignore that outside loop and continue into the second nested loop. Is there any way to do this without making another if statement at the beginning and then copying the whole program and pasting it after an elseif statement? Is there a command that will do it? ' break' just terminates the program.
Thanks! * * bold**
for radfrac= Radfrac_min : Radfrac_step : Radfrac_max
if Radfrac_min==1
disp('Radfrac_min must not be 1.')
break %Should resolve Radfrac_min!=1 problem and just
%exit the loop.
elseif Radfrac_step==0
disp('Radfrac_step must not be 0')
break
elseif Radfrac_min==Radfrac_max
disp('Radfrac_min may not equal Radfrac_max.')
break
else radfrac_iteration_count=radfrac_iteration_count+1;
end
for u=1:Max_training
for v=0:dimension
for i=1:z
...
end
end
end
end
  2 Comments
KSSV
KSSV on 18 Nov 2016
Note that
Radfrac_step=0;
cannot be zero if you are using it like
radfrac= Radfrac_min : Radfrac_step : Radfrac_max ;
It will lead to empty matrix.
per isakson
per isakson on 18 Nov 2016
Edited: per isakson on 18 Nov 2016
"Is there a command that will do it?" &nbsp The simple answer is NO. AFAIK.
BTW: "' break' just terminates the program." &nbsp See break, Terminate execution of for or while loop

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 18 Nov 2016
Edited: John D'Errico on 18 Nov 2016
Where you put the break is in the outer-loop. When it hits the break, OF COURSE it will terminate the main loop! You told it to break out of the loop!
When you break out of a loop, you cannot hope that MATLAB will magically know to execute something that is also inside the loop, but farther down. Computers cannot read your mind. They do exactly as told. When it hits the break, it breaks out. Period.
Sadly, your code is pretty confusing as to what you really want to do. I would suggest that:
You should not be making these tests inside the loop! For example, all of the variables that you are testing are fixed constants,or they should be. Radfrac_min, Radfrac_step, and Radfrac_max are all loop parameters. I assume they do not change inside those loops, but it is a terribly bad way to write code if they do. If there is a problem with them, test and resolve those problems before the main loop ever starts!
I might suggest that you could set this up with the outer loop as a while loop. Take those silly blasted tests outside of the main loop completely. But a while loop can be set to run as long as the test that controls it tells it to run. I think this will work, but again, your code is terribly confusing as to what you truly want to happen, since the code as you have written it won't do what you claim to want to do.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!