Download METAFUN manual

Transcript
Page 55
1.8
Loops
Loops
Yet another programming concept present in METAPOST is the loop statement, the familiar ‘for loop' of all
programming languages.
for i=0 step 2 until 20 :
draw (0,i) ;
endfor ;
As explained convincingly in Niklaus Wirth's book on algorithms and datastructures, the for loop is the natural
companion to an array. Given an array of length n, you can construct a path out of the points that make up the
array.
draw for i=0 step 1 until n-1 : p[i] .. endfor p[n] ;
If the step increment is not explicitly stated, it has an assumed value of 1. We can shorten the previous loop
construct as follows:
draw for i=0 upto n-1 : p[i] .. endfor p[n] ;
After seeing if in action, the following for loop will be no surprise:
draw origin for i=0 step 10 until 100 : ..{down}(i,0) endfor ;
This gives the zig--zag curve:
Welcome to MetaPost
exit content index reference Í É Ê Î