Difference between revisions of "For"
(Created an example for iterating over a range of numbers and an example of iterating over a list.) |
(Added new when to use for loops and how to create them. Added the nested for loop example. Added outputs in text.) |
||
Line 4: | Line 4: | ||
[[Category:Keywords]] | [[Category:Keywords]] | ||
− | == | + | == When to use for loops == |
− | Iterate over a range of numbers | + | 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''. | ||
+ | For loops are closed off with an <c>end for</c> keyword statement. | ||
+ | All code that is to be repeated must be indented in between the <c>for</c> and <c> end for</c> statements. | ||
+ | |||
+ | == Iterate over a range of numbers == | ||
+ | === Example === | ||
<ms> | <ms> | ||
− | for i in range(1, | + | for i in range(1,3) |
print i | print i | ||
end for | end for | ||
</ms> | </ms> | ||
+ | ---- | ||
+ | '''outputs:''' | ||
− | == | + | 1 |
− | Iterate over a list | + | |
+ | 2 | ||
+ | |||
+ | 3 | ||
+ | |||
+ | |||
+ | == Iterate over a list == | ||
+ | === Example === | ||
<ms> | <ms> | ||
words = ["hello", "MiniScript", "wiki"] | words = ["hello", "MiniScript", "wiki"] | ||
Line 20: | Line 40: | ||
end for | end for | ||
</ms> | </ms> | ||
+ | ---- | ||
+ | '''outputs:''' | ||
+ | |||
+ | hello | ||
+ | |||
+ | MiniScript | ||
+ | |||
+ | wiki | ||
+ | |||
+ | |||
+ | == Nested For Loops == | ||
+ | For loops can be nest inside one another the inner most loop will loop through all its iterations for every iteration the its outer loop has. | ||
+ | === Example === | ||
+ | <ms> | ||
+ | for i in range(1,2) | ||
+ | for j in range(3,4) | ||
+ | print val(i) + " + " + val(j) + " = " + val(i+j) | ||
+ | end for | ||
+ | end for | ||
+ | </ms> | ||
+ | ---- | ||
+ | '''outputs:''' | ||
+ | |||
+ | 1 + 3 = 4 | ||
+ | |||
+ | 1 + 4 = 5 | ||
+ | |||
+ | 2 + 3 = 5 | ||
+ | |||
+ | 2 + 4 = 6 |
Revision as of 01:32, 18 May 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.
For loops are closed off with an end for
keyword statement.
All code that is to be repeated must be 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
Nested For Loops
For loops can be nest inside one another the inner most loop will loop through all its iterations for every iteration the its outer loop has.
Example
for i in range(1,2)
for j in range(3,4)
print val(i) + " + " + val(j) + " = " + val(i+j)
end for
end for
outputs:
1 + 3 = 4
1 + 4 = 5
2 + 3 = 5
2 + 4 = 6