Skip to content

Commit 6b5d03e

Browse files
committed
Update FOR documentation
1 parent 57d2ac5 commit 6b5d03e

1 file changed

Lines changed: 31 additions & 4 deletions

File tree

docs/for.md

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,31 @@
1717

1818
##Description
1919

20-
A **For...Next** loop initializes _iterator_ to _startvalue_, then executes the _sentences_, incrementing _iterator_ by _stepvalue_ until it reaches or exceeds _endvalue_. If _stepvalue_ is not explicitly given it will set to 1.
20+
A **For...Next** loop initializes _iterator_ to _startvalue_, then executes the _sentences_, incrementing _iterator_ by
21+
_stepvalue_ until it reaches or exceeds _endvalue_. If _stepvalue_ is not explicitly given it will set to 1.
2122

2223
##Examples
2324

2425
```
26+
REM Counts from 1 to 10
2527
FOR i = 1 TO 10: PRINT i: NEXT
2628
```
2729

30+
### Counts downwards
31+
```
32+
FOR i = 10 TO 1 STEP -1: PRINT i: NEXT
33+
```
34+
35+
### Loops using odd numbers
36+
```
37+
FOR i = 1 TO 10 STEP 2: PRINT i: NEXT
38+
```
39+
2840
##Differences From Sinclair Basic
2941
* The variable name after the NEXT statement is not required.
3042

31-
* Note that variable types can cause issues with ZX Basic For...Next Loops. If the upper limit of the iterator exceeds the upper limit of the variable type, the loop may not complete.
43+
* Note that variable types can cause issues with ZX Basic For...Next Loops. If the upper limit of the iterator exceeds
44+
the upper limit of the variable type, the loop may not complete.
3245
For example:
3346
```
3447
DIM i as UByte
@@ -40,9 +53,23 @@ NEXT i
4053

4154
Clearly, since the largest value a byte can hold is 255, it's not possible for i in the above example to exceed 300.
4255
The variable will "wrap around" to 0 and as a result, the loop will not ever terminate.
43-
This can happen in much more subtle ways when STEP is used.
56+
This can happen in much more subtle ways when `STEP` is used.
4457
There has to be "room" within the variable type for the iterator to exceed the terminator when it is being
45-
incremented by "STEP" amounts.
58+
incremented by <step> amounts.
59+
60+
For example, this loop will neved end
61+
62+
```
63+
DIM i as UInteger
64+
65+
FOR i = 65000 TO 65500 STEP 100
66+
...
67+
NEXT i
68+
```
69+
70+
This loop will never end. `UInteger` type allows values in the range `[0..65535]` so apparently it's ok, because
71+
65500 fits in it. However `STEP` is 100, so 65500 + 100 = 65600 which fall out if such range. There will be an
72+
_overflow_ and the variable `i` will take the value 64 and the loop will continue.
4673

4774
##See Also
4875

0 commit comments

Comments
 (0)