Merge pull request #311 from gradha/pr_documents_two_variable_for_loop
Documents two-variable for loop with sequences.
This commit is contained in:
commit
5568f85449
1 changed files with 25 additions and 0 deletions
25
doc/tut1.txt
25
doc/tut1.txt
|
|
@ -349,6 +349,7 @@ provides. The example uses the built-in ``countup`` iterator:
|
||||||
echo("Counting to ten: ")
|
echo("Counting to ten: ")
|
||||||
for i in countup(1, 10):
|
for i in countup(1, 10):
|
||||||
echo($i)
|
echo($i)
|
||||||
|
# --> Outputs 1 2 3 4 5 6 7 8 9 10 on different lines
|
||||||
|
|
||||||
The built-in ``$`` operator turns an integer (``int``) and many other types
|
The built-in ``$`` operator turns an integer (``int``) and many other types
|
||||||
into a string. The variable ``i`` is implicitly declared by the ``for`` loop
|
into a string. The variable ``i`` is implicitly declared by the ``for`` loop
|
||||||
|
|
@ -362,6 +363,7 @@ the same:
|
||||||
while i <= 10:
|
while i <= 10:
|
||||||
echo($i)
|
echo($i)
|
||||||
inc(i) # increment i by 1
|
inc(i) # increment i by 1
|
||||||
|
# --> Outputs 1 2 3 4 5 6 7 8 9 10 on different lines
|
||||||
|
|
||||||
Counting down can be achieved as easily (but is less often needed):
|
Counting down can be achieved as easily (but is less often needed):
|
||||||
|
|
||||||
|
|
@ -369,6 +371,7 @@ Counting down can be achieved as easily (but is less often needed):
|
||||||
echo("Counting down from 10 to 1: ")
|
echo("Counting down from 10 to 1: ")
|
||||||
for i in countdown(10, 1):
|
for i in countdown(10, 1):
|
||||||
echo($i)
|
echo($i)
|
||||||
|
# --> Outputs 10 9 8 7 6 5 4 3 2 1 on different lines
|
||||||
|
|
||||||
Since counting up occurs so often in programs, Nimrod also has a ``..`` iterator
|
Since counting up occurs so often in programs, Nimrod also has a ``..`` iterator
|
||||||
that does the same:
|
that does the same:
|
||||||
|
|
@ -1202,6 +1205,28 @@ raised) for performance reasons. Thus one should use empty sequences ``@[]``
|
||||||
rather than ``nil`` as the *empty* value. But ``@[]`` creates a sequence
|
rather than ``nil`` as the *empty* value. But ``@[]`` creates a sequence
|
||||||
object on the heap, so there is a trade-off to be made here.
|
object on the heap, so there is a trade-off to be made here.
|
||||||
|
|
||||||
|
The ``for`` statement can be used with one or two variables when used with a
|
||||||
|
sequence. When you use the one variable form, the variable will hold the value
|
||||||
|
provided by the sequence. The ``for`` statement is looping over the results
|
||||||
|
from the ``items()`` iterator from the `system <system.html>`_ module. But if
|
||||||
|
you use the two variable form, the first variable will hold the index position
|
||||||
|
and the second variable will hold the value. Here the ``for`` statement is
|
||||||
|
looping over the results from the ``pairs()`` iterator from the `system
|
||||||
|
<system.html>`_ module. Examples:
|
||||||
|
|
||||||
|
.. code-block:: nimrod
|
||||||
|
for i in @[3, 4, 5]:
|
||||||
|
echo($i)
|
||||||
|
# --> 3
|
||||||
|
# --> 4
|
||||||
|
# --> 5
|
||||||
|
|
||||||
|
for i, value in @[3, 4, 5]:
|
||||||
|
echo("index: ", $i, ", value:", $value)
|
||||||
|
# --> index: 0, value:3
|
||||||
|
# --> index: 1, value:4
|
||||||
|
# --> index: 2, value:5
|
||||||
|
|
||||||
|
|
||||||
Open arrays
|
Open arrays
|
||||||
-----------
|
-----------
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue