Graphics Programs Reference
In-Depth Information
Loops
while
The while construct
while condition :
bl ock
end
executes a block of statements if the condition istrue.After execution of the block,
condition isevaluated again. If it isstill true, the block isexecuted again. This process
iscontinueduntil the condition becomes false.
The following example computes the number of years ittakes fora$1000 principal
to grow to $10,000 at 6% annual interest.
>>p=1000;years=0;
>>whilep<10000
years = years + 1;
p=p*(1+0.06);
end
>> years
years =
40
for
The for looprequires a target and a sequence overwhich the target loops. The form
of the construct is
for target = sequence
bl ock
end
For example,tocompute cos x from x
=
0to
π/
2at increments of
π/
10 wecould
use
>>forn=0:5 %nloopsoverthesequence012345
y(n+1) = cos(n*pi/10);
end
>> y
y=
1.0000
0.9511
0.8090
0.5878
0.3090
0.0000
Search WWH ::




Custom Search