... where <condition> is a Boolean expression which evaluates to either -1 (true) or 0 (false). The assignment to P can happen anywhere in the loop.
Unlike the equivalent version with GOTO, the FOR loop remembers the jump target, so execution is likely faster - especially when the loop is located at higher line numbers. Also, the loop can easily be embedded into a larger line, as the start of the loop need not anymore be at the start of a line. Here's an example reading the error channel of the current disc drive:
Nice trick. You're right about the execution speed being quicker - the difference between a stack pop and memory scan making a lot of difference (particularly at higher line numbers in longer programs).
Yeah, but this is (essentially) Altair BASIC, where flow control is limited to IF-THEN, FOR-NEXT, [ON] GOTO, and [ON] GOSUB-RETURN. Without the niceties of later BASIC features to moderate our misdemeanours, tweaking the FOR variable is a minor crime.
Actually, in the example I gave, such a DISPOSE NEXT command was not necessary, as the dangling FOR loop gets removed with a NEXT of the further enclosing FOR loop variable. Sometimes it pays off to know a little more about the inner workings of the BASIC interpreter.
Likewise, if there's a FOR loop in a subroutine, you don't need to end the FOR loop to be 'allowed' a RETURN, you can return straightaway and the dangling FOR loop likewise is removed from the stack. For example, in my implementation of the game 'TRON Light Cycles' a subroutine searches for a free path for the enemy Light Cycle, and exits as soon one is found:
This statement is not interpreted as double assignment in BASIC V2, rather the result of the Boolean expression ST=0 (either -1 if true, or 0 if false) is assigned to P.
This statement is not interpreted as double assignment in BASIC V2, rather the result of the Boolean expression ST=0 (either -1 if true, or 0 if false) is assigned to P.
Do I get it right then, that 'A=B=0' and 'A=B=-1' are valid basic expressions, but 'A=B=33' is not?
Interesting.
No, you're mixing up expressions and statements. To clarify, most (imperative) programming languages make a difference between statements and expressions:
A statement is some instruction or command which is intended to be executed by the language, e.g. PRINT, GOTO. In that sense, the LET statement is an often overlooked case, it tells the language to evaluate an expression and assign it to a variable, i.e.
LET <variable>=<expression>
An expression is a collection of variables, numbers, functions and - possibly! - strings (the operands), which are combined with operators. Most languages feature numeric expressions, which evaluate to a number. Some languages (like BASIC) also support string expressions, which evaluate to a string. A special form are Boolean expressions, where two numeric or two string expressions are compared using a relational operator, such as '=', '<', etc. A few languages truly return a Boolean result, i.e. true or false, which then can be 'fed' into an IF statement. Most others simply map Booleans to a set of numeric values, false is always mapped to 0 (zero), true to some non-zero number - CBM BASIC uses -1, C uses 1. In any case, the THEN clause after an IF statement is executed if the expression between IF and THEN evaluates to a non-zero number, i.e. it is true (that similarily applies to C).
The result of a Boolean expression can however quite as well be assigned to a variable - that being said, all your examples are valid statements in CBM BASIC:
'A=B=0' assigns -1 to A if B is 0, and 0 to A if B is not 0,
'A=B=-1' assigns -1 to A if B is -1, and 0 to A if B is not -1,
and finally
'A=B=33' assigns -1 to A if B is 33, and 0 to A if B is not 33.
Here 'A=...' is the statement where LET had been omitted because it's optional in CBM BASIC, and 'B=...' is a Boolean expression, whose result is assigned to A. It's an alleged source of confusion, that '=' serves double use in BASIC: as assignment statement and as relational operator in expressions.
Which is why a number of languages make them explicitly different, to avoid that possible confusion (viz. C/C++/C# use '=' for assignment and '==' for equality; Pascal and Modula-2 use ':=' for assignment and '=' for equality, etc.)
Thanks Mike.
It was the '=' being a symbol for both assignment and comparison that confused me; in Python I can keep them apart with '=' and '=='.
I couldn't make out how both '=' were distinguished.
Now I get the first one, which could be preceded by LET, is the assignment and the second one is a comparison, leading to 'assign to A (the result of checking if B equals 33)'.