| MATLAB Function Reference | Search  Help Desk |
| break | Examples See Also |
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.
Examples
The example below shows awhile 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
end, for, return, while