
4 Programming
else
b = x; fb = fx;
end
end
x
The result is a roo t of the polynomial x
3
-2x -5,namely
x=
2.09455148154233
The cautions involving matrix comparisons that are discussed in the section
on the
if statement also apply to the while statement.
continue
The continue statement passes control to the next iteration of the for loop
or
while loop in which it appears, skipping any remaining statements in the
bodyoftheloop. Thesameholdstruefor
continue statements in nested
loops. That is, execution continues at the beginning of the loop in which the
continue statement was encountered.
The example below shows a
continue loop that counts the lines o f code in the
file
magic.m, skipping all blank lines and comments. A continue statement is
used to advance to the next line in
magic.m without incrementing the count
whenever a blank line or comment line is enco untered:
fid = fopen('magic.m','r');
count = 0;
while ~feof(fid)
line = fgetl(fid);
if isempty(line) | strncmp(line,'%',1)
continue
end
count = count + 1;
end
disp(sprintf('%d lines',co unt));
4-6
Commenti su questo manuale