| MATLAB Function Reference | ![]() |
Terminate execution of a for loop or while loop
Syntax
break
Description
break
terminates the execution of a for loop or while loop. In nested loops, break exits from the innermost loop only.
Remarks
If you use break outside of a for or while loop in a MATLAB script or function, break terminates the script or function at that point.
If break is executed in an if, switch-case, or try-catch statement, it terminates the statement at that point.
Examples
The example below shows a while loop that reads the contents of the file fft.m into a MATLAB character array. A break statement is used to exit the while loop when the first empty line is encountered. The resulting character array contains the M-file help for the fft program.
fid = fopen('fft.m','r');
s = '';
while ~feof(fid)
line = fgetl(fid);
if isempty(line), break, end
s = strvcat(s,line);
end
disp(s)
See Also
| box | brighten | ![]() |