Difference between revisions of "For"
(7 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | <c>for</c> is a [[:Category: | + | <c>for</c> is a [[:Category:Keywords|keyword]] used to loop over a sequence (i.e. a [[list]], [[map]], or [[string]]). |
[[Category:Language]] | [[Category:Language]] | ||
[[Category:Keywords]] | [[Category:Keywords]] | ||
− | { | + | == When to use for loops == |
+ | A for loop is used when you want to repeat a block of code a fixed number of times. | ||
+ | For loops in Miniscript utilize the for each construct. | ||
+ | Instead of initializing a counter in the for loop statement the for each construct iterates a variable | ||
+ | over a sequence, this mitigates the risk of creating an off by one error. | ||
+ | |||
+ | == How to create for loops == | ||
+ | For loops are created with the <c>for</c> keyword followed by ''element'' <c>in</c> ''sequence''. This causes the ''element'' variable to be assigned each value of ''sequence'' in turn. Any [[string]], [[list]], or [[map]] may be used for the sequence, but a common idiom is to use the [[range]] intrinsic function to iterate over a numeric range. Other common options are to use the [[indexes]] intrinsic to iterate over the indexes of a sequence, or [[values]] to iterate over just the values of a map. | ||
+ | |||
+ | For loops are closed off with an <c>end for</c> keyword statement. | ||
+ | |||
+ | As a matter of style, all code to be repeated is usually indented in between the <c>for</c> and <c> end for</c> statements. | ||
+ | |||
+ | == Iterate over a range of numbers == | ||
+ | === Example === | ||
+ | <ms> | ||
+ | for i in range(1,3) | ||
+ | print i | ||
+ | end for | ||
+ | </ms> | ||
+ | ---- | ||
+ | '''outputs:''' | ||
+ | |||
+ | 1 | ||
+ | |||
+ | 2 | ||
+ | |||
+ | 3 | ||
+ | |||
+ | |||
+ | == Iterate over a list == | ||
+ | === Example === | ||
+ | <ms> | ||
+ | words = ["hello", "MiniScript", "wiki"] | ||
+ | for word in words | ||
+ | print word | ||
+ | end for | ||
+ | </ms> | ||
+ | ---- | ||
+ | '''outputs:''' | ||
+ | |||
+ | hello | ||
+ | |||
+ | MiniScript | ||
+ | |||
+ | wiki | ||
+ | |||
+ | == Iterate Over a Map == | ||
+ | |||
+ | When using <c>for</c> with a [[map]], the loop variable on each iteration is a little map containing "key" and "value". (But note that, since maps themselves are unordered, the order in which these key/value pairs appear is not guaranteed.) | ||
+ | |||
+ | === Example === | ||
+ | <ms>m = {"one":"uno", "two":"dos", "three":"tres"} | ||
+ | for kv in m | ||
+ | print "Key " + kv.key + " has value " + kv.value | ||
+ | end for</ms> | ||
+ | ---- | ||
+ | '''outputs:''' | ||
+ | |||
+ | Key one has value uno | ||
+ | |||
+ | Key two has value dos | ||
+ | |||
+ | Key three has value tres | ||
+ | |||
+ | == Nested For Loops == | ||
+ | For loops can be nested inside one another; the inner most loop will loop through all its iterations for every iteration of the outer loop. | ||
+ | === Example === | ||
+ | <ms> | ||
+ | for i in range(1,2) | ||
+ | for j in range(3,4) | ||
+ | print i + " * " + j + " = " + (i*j) | ||
+ | end for | ||
+ | end for | ||
+ | </ms> | ||
+ | ---- | ||
+ | '''outputs:''' | ||
+ | |||
+ | 1 * 3 = 3 | ||
+ | |||
+ | 1 * 4 = 4 | ||
+ | |||
+ | 2 * 3 = 6 | ||
+ | |||
+ | 2 * 4 = 8 |
Latest revision as of 14:22, 23 November 2022
for
is a keyword used to loop over a sequence (i.e. a list, map, or string).
Contents
When to use for loops
A for loop is used when you want to repeat a block of code a fixed number of times. For loops in Miniscript utilize the for each construct. Instead of initializing a counter in the for loop statement the for each construct iterates a variable over a sequence, this mitigates the risk of creating an off by one error.
How to create for loops
For loops are created with the for
keyword followed by element in
sequence. This causes the element variable to be assigned each value of sequence in turn. Any string, list, or map may be used for the sequence, but a common idiom is to use the range intrinsic function to iterate over a numeric range. Other common options are to use the indexes intrinsic to iterate over the indexes of a sequence, or values to iterate over just the values of a map.
For loops are closed off with an end for
keyword statement.
As a matter of style, all code to be repeated is usually indented in between the for
and end for
statements.
Iterate over a range of numbers
Example
for i in range(1,3)
print i
end for
outputs:
1
2
3
Iterate over a list
Example
words = ["hello", "MiniScript", "wiki"]
for word in words
print word
end for
outputs:
hello
MiniScript
wiki
Iterate Over a Map
When using for
with a map, the loop variable on each iteration is a little map containing "key" and "value". (But note that, since maps themselves are unordered, the order in which these key/value pairs appear is not guaranteed.)
Example
m = {"one":"uno", "two":"dos", "three":"tres"}
for kv in m
print "Key " + kv.key + " has value " + kv.value
end for
outputs:
Key one has value uno
Key two has value dos
Key three has value tres
Nested For Loops
For loops can be nested inside one another; the inner most loop will loop through all its iterations for every iteration of the outer loop.
Example
for i in range(1,2)
for j in range(3,4)
print i + " * " + j + " = " + (i*j)
end for
end for
outputs:
1 * 3 = 3
1 * 4 = 4
2 * 3 = 6
2 * 4 = 8