2

I have three nested loop in Matlab and in each loop, I have an "if" that check a flag. If it was OK, the the algorithm proceeds, otherwise, I want that the program should be terminated. Here is my codes, I think something is wrong!

[A] = finction (...,...,...)

for i = 1:100
for j = 1:100
for k = 1:30

some operation which its results is a flag

if flag==1 % its initial value is 0
break
end;
end;
end;
end;

Where is should put that break in order I break the rest of the computations?

0

1 Answer 1

5

BREAK terminates the execution of a loop, so if you have a nested loop, break will only quit the innermost loop, and the program will continue running.

If you want the entire function to be terminated, you need to use RETURN. Please note that at the moment return is called, all the requested output arguments must be assigned to some value, otherwise the code will throw an error.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.