MATLAB中return和break

时间:2022-07-12 14:00:05

return:

RETURN Return to invoking function.
RETURN causes a return to the invoking function or to the keyboard.
It also terminates the KEYBOARD mode.

Normally functions return when the end of the function is reached.
A RETURN statement can be used to force an early return.

Example
function d = det(A)
if isempty(A)
d = 1;
return
else
...
end

break:

BREAK Terminate execution of WHILE or FOR loop.
BREAK terminates the execution of FOR and WHILE loops.
In nested loops, BREAK exits from the innermost loop only.

BREAK is not defined outside of a FOR or WHILE loop.
Use RETURN in this context instead.