Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

break - Terminate execution of for or while loop

Syntax

break

Description

break terminates the execution of a for or while loop. Statements in the loop that appear after the break statement are not executed.

In nested loops, break exits only from the loop in which it occurs. Control passes to the statement that follows the end of that loop.

Remarks

break is not defined outside a for or while loop. Use return in this context instead.

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) || ~ischar(line), break, end
   s = sprintf('%s%s\n', s, line);
end
disp(s);

fclose(fid);

See Also

for, while, end, continue, return

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2010- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS