BASIC: DO … LOOP, EXIT, ELSE, block IF and SELECT CASE

DO WHILE/UNTIL … LOOP WHILE/UNTIL, EXIT DO and EXIT FOR; IF … THEN … ELSE on
one line; block IF … ELSEIF … ELSE … END IF; SELECT CASE with values, ranges
and IS comparisons, numeric or string. RENUM also fixes line numbers after
ELSE. basic/test-blocks.py checks them; the manual and README cover them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-12-01 16:09:41 -08:00
1 parent fd876fd44d
commit 79aef8ed38
7 files changed
+2216 -1756

No files matched your search

+89 -2
View File
@@ -99,7 +99,7 @@ L End Enter now it is 10 PRINT "HELLO"
`RENUM` gives the lines new numbers, starting at 10 (or the first number
given) and going up in tens (or the second). It also changes the line numbers
after `GOTO`, `GOSUB`, `THEN`, `RUN` and `ON … GOTO`/`GOSUB` so the program
after `GOTO`, `GOSUB`, `THEN`, `ELSE`, `RUN` and `ON … GOTO`/`GOSUB` so the program
runs as before. A number that isn't a line in the program is left alone.
Numbers inside quotes, `REM` and `DATA` are left as they are, and so is a
line number worked out at run time. If the new numbers would pass 65535, or
@@ -270,6 +270,37 @@ by statements to run. When it is false, the rest of the line is skipped, so
statements after the colon run only when the condition is true. Any non-zero
value counts as true.
`ELSE` gives the line a second part, run only when the condition is false:
```
IF X > 100 THEN 500 ELSE 600
IF A$ = "YES" THEN PRINT "OK": GOTO 20 ELSE PRINT "NO"
IF A THEN IF B THEN PRINT "BOTH" ELSE PRINT "A ONLY" ELSE PRINT "NOT A"
```
An `ELSE` belongs to the nearest `IF` before it that has none yet.
**Block IF.** When `THEN` is the last thing on its line, the statements that
follow, down to `END IF`, are the `THEN` part, and the block can have
`ELSEIF` and `ELSE` parts too:
```
10 IF T < 0 THEN
20 PRINT "FREEZING"
30 C = 1
40 ELSEIF T < 20 THEN
50 PRINT "COOL"
60 ELSE
70 PRINT "WARM"
80 END IF
```
Only the first part whose condition is true runs (or `ELSE`'s, if none is);
then the program carries on after `END IF`. Blocks nest inside each other and
inside loops. `ELSEIF` may be written `ELSE IF`, and `END IF` `ENDIF`. Each
`ELSEIF` must end its line with `THEN`. Block IFs work in a program, not in
a direct statement.
### GOTO, GOSUB, RETURN, ON
```
@@ -304,6 +335,56 @@ NEXT J, I
- Loops nest 16 deep.
- Jumping out of a loop with `GOTO` is allowed. Starting a new `FOR` with the
same variable discards the old loop.
- `EXIT FOR` leaves the innermost loop at once, carrying on after its `NEXT`;
the variable keeps the value it had.
### DO … LOOP — loops on a condition
```
10 DO WHILE I < 10
20 I = I + 1
30 LOOP
10 DO
20 INPUT "GUESS"; G
30 LOOP UNTIL G = 7
```
- `DO WHILE` *condition* runs the loop while the condition is true, testing it
before each time round, so the body may not run at all. `DO UNTIL`
*condition* runs it until the condition is true.
- `LOOP WHILE` and `LOOP UNTIL` test at the end instead, so the body always
runs at least once.
- A plain `DO` … `LOOP` goes round for ever, until `EXIT DO` (or `GOTO`, `END`
or Ctrl-C) leaves it. `EXIT DO` carries on after the `LOOP`.
- Loops nest 16 deep, with `FOR` loops inside and out. A whole loop can be on
one line, or in a direct statement:
`I = 0: DO: I = I + 1: PRINT I: LOOP UNTIL I = 5`.
### SELECT CASE — choosing by value
```
10 SELECT CASE N
20 CASE 0
30 PRINT "NONE"
40 CASE 1, 2, 3
50 PRINT "A FEW"
60 CASE 4 TO 9
70 PRINT "SEVERAL"
80 CASE IS >= 10
90 PRINT "LOTS"
100 CASE ELSE
110 PRINT "LESS THAN NONE"
120 END SELECT
```
`SELECT CASE` works out its value once, then runs the statements under the
first `CASE` that matches it and carries on after `END SELECT`. A `CASE` lists
values, ranges (*low* `TO` *high*) and comparisons (`IS` followed by `<`,
`<=`, `=`, `<>`, `>=` or `>` and a value), separated by commas; any one of
them matching is enough. `CASE ELSE` matches anything, so it goes last. When
no `CASE` matches, nothing runs. The value may be a string, with strings in
the `CASE`s: `CASE "A" TO "M"` compares in alphabetical order, as `<` does.
### DIM — arrays
@@ -1228,7 +1309,13 @@ An error stops the program and reports the line, for example
| `STRING TOO LONG` | A string over 79 characters, or a program line too long to store (also from `RENUM`) |
| `ILLEGAL FUNCTION CALL` | A bad argument: `SQR(-1)`, `LOG(0)`, a colour over 15, `AND` on a number beyond ±32767, a file number other than 1–3, an impossible `DATE`, a `RENUM` past 65535 or with a step of 0 |
| `UNDEFINED FUNCTION` | `FNx` used before its `DEF` |
| `FOR NESTED TOO DEEPLY` | More than 16 loops open |
| `FOR NESTED TOO DEEPLY` | More than 16 `FOR` loops open |
| `DO NESTED TOO DEEPLY` | More than 16 `DO` loops open |
| `FOR WITHOUT NEXT` | A `FOR` that runs no times, or `EXIT FOR`, with no `NEXT` after it |
| `DO WITHOUT LOOP` | A `DO` whose condition fails, or `EXIT DO`, with no `LOOP` after it |
| `LOOP WITHOUT DO` | `LOOP` or `EXIT DO` with no `DO` loop open |
| `IF WITHOUT END IF` | A block `IF` (or its `ELSE`) with no `END IF` after it |
| `SELECT WITHOUT END SELECT` | A `SELECT CASE` (or its `CASE`) with no `END SELECT` after it |
| `GOSUB NESTED TOO DEEPLY` | More than 32 subroutines in progress |
| `FILE NOT FOUND` | `LOAD`, `RUN` or `DEL` of a name not in `DIR` |
| `BAD FILE NAME` | A name that is empty, longer than 8 or not just letters and digits |