You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/for.md
+31-4Lines changed: 31 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,18 +17,31 @@
17
17
18
18
##Description
19
19
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.
21
22
22
23
##Examples
23
24
24
25
```
26
+
REM Counts from 1 to 10
25
27
FOR i = 1 TO 10: PRINT i: NEXT
26
28
```
27
29
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
+
28
40
##Differences From Sinclair Basic
29
41
* The variable name after the NEXT statement is not required.
30
42
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.
32
45
For example:
33
46
```
34
47
DIM i as UByte
@@ -40,9 +53,23 @@ NEXT i
40
53
41
54
Clearly, since the largest value a byte can hold is 255, it's not possible for i in the above example to exceed 300.
42
55
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.
44
57
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.
0 commit comments