DOTIMES primitive takes 2 lists as inputs. First one indicates the range for a variable and second one is the list of instructions to be executed. Dolist runs the instruction list for each value specified in the range.
Example:
dotimes [i 4] [show se :i sqrt :i]
will print in the Command Centre 4 pairs of numbers: a number and its square root
0 0
1 1
2 1.4142136
3 1.7320508
How to adjust the loop to start from a different value other than 0
The range for dotimes always starts at 0 and it is the second number long:
dotimes [i 6] [show :i] will give you 6 values of i starting from 0: 0, 1, 2, 3, 4 and 5.
dotimes [i 6] [show :i] will give you 6 values of i starting from 0: 0, 1, 2, 3, 4 and 5.
In case you want to get 6 values starting from some other number, you just need to shift the value of i when you use it by adding or subtracting the desired number. Examples:
for starting from 2 do
dotimes [i 6] [show :i + 2]
or
dotimes [i 6] [show 2 + :i]
for starting from -100 do
dotimes [i 6] [show :i - 100]
or
dotimes [i 6] [show -100 + :i] ;note there's a space after "-" in the first case (it's a subtraction sign there) and no space after the minus in the second case, since it's a negative number in this case being added to :i
dotimes [i 6] [show -100 + :i] ;note there's a space after "-" in the first case (it's a subtraction sign there) and no space after the minus in the second case, since it's a negative number in this case being added to :i
When modifying the range remember to use round brackets if you want the addition / subtraction to be done before multiplication / division like in any arithmetic expression :
dotimes [i 4] [show 4 * (:i - 1) ]
-- it will start from -1, go up by 4 and output -4, then 0, then 4 and 8.
but
dotimes [i 4] [show 4 * :i - 1]
-- will give you -1, 3, 7 and 11.
BTW 1
Same works for other primitives reporting or just using a range of values, like random.
random 10 outputs one of the numbers 0, 1, .... 9
To shift the range by 5, do
5 + random 10 . It will add 5 to whatever has been output by random 10 so you get one of these: 5, 6, ... 14.
BTW 2
You don't have to use i as a name of the variable in dotimes. Name it whatever suits you best:
dotimes [step 7] [make 'termNum' 4 + 3 * :step show :termNum] will work as well.