
4.3.1. The
f
f
o
o
r
r Loop
The
f
f
o
o
r
r loop repeats a group of statements a fixed, predetermined number of
times. A matching
e
e
n
n
d
d delineates the statements. The syntax for the
f
f
o
o
r
r loop
is exhibited in Table 9.
Syntax Example
for index=first_value:step:last_value
segment of executable programming code
end
for i=1:10
disp(i)
end
sumj=0;
for j=25:-2:-12
sumj=sumj+j;
end
Table 9: The
f
f
o
o
r
r statement syntax and example.
The colon notation is similar as in the case of the vectors. Actually, index in
the
f
f
o
o
r
r syntax is a vector with n elements with first element being the
first_value and last the last_value. The difference between the index
elements is step. Afterwards, the
f
f
o
o
r
r
statement executes n times the segment
of executable programming code moving from the first element of the index
to the last one element-by-element at each time. If step is not displayed,
then by default is set to 1.
4.3.2. The while Loop
The
w
w
h
h
i
i
l
l
e
e loop repeats a group of statements an indefinite number of times
under control of a logical condition. That is, as long as an expression is
TRUE, then the segment of executable programming code that is included in
the
w
w
h
h
i
i
l
l
e
e statement is executed. A matching
e
e
n
n
d
d delineates the statements
[2]. The syntax for the
w
w
h
h
i
i
l
l
e
e loop is exhibited in Table 10.
Syntax Example
while expression
segment of executable programming code
end
X=-3;
while X<=10
disp(X)
X=X+1;
end
Table 10: The
w
w
h
h
i
i
l
l
e
e statement syntax and example.
Kommentare zu diesen Handbüchern