Graphics Programs Reference
In-Depth Information
Loops shouldbe avoidedwheneverpossible in favor of vectorized expressions,
which execute much faster.Avectorized solution to the last computationwouldbe
>>n=0:5;
>>y=cos(n*pi/10)
y=
1.0000
0.9511
0.8090
0.5878
0.3090
0.0000
break
Any loop can beterminatedbythe break statement. Upon encountering a break
statement, the control is passed to the first statementoutside the loop. In the fol-
lowing example the function buildvec constructs arowvector of arbitrary length
by prompting forits elements. The process isterminatedwhenanempty element is
encountered.
functionx=buildvec
fori=1:1000
elem = input('==> '); % Prompts for input of element
if isempty(elem)
% Check for empty element
break
end
x(i) = elem;
end
>>x=buildvec
==> 3
==> 5
==> 7
==> 2
==>
x=
3
5
7
2
continue
When the continue statement isencounteredinaloop, the control is passed to
the next iterationwithoutexecuting the statements in the current iteration. As an
illustration,consider the following function thatstrips all the blanks fromthe string s1 :
function s2 = strip(s1)
s2 = '';
% Create an empty string
fori=1:length(s1)
Search WWH ::




Custom Search