=

From BeebWiki
Jump to: navigation, search

The equals sign, =, is a symbol with multiple meanings in BBC BASIC:

  • an assignment statement, to set a variable to the value of an expression;
  • an equality operator, to test whether two values are equal;
  • a statement to return from a function with its value.
=
Availability Present in all original versions of BBC BASIC.
Syntax BASIC I-V [LET] <num-var> = <numeric>
[LET] <string-var> = <string>
<num-var> = <numeric> = <numeric>
<num-var> = <string> = <string>
=<numeric>|<string>
Token (hex) BASIC I-V 3D (operator, statement)
Description BASIC I-V In the first two forms, assigns the value of the <numeric> or <string> to the <num-var> or <string-var>.
In the third and fourth forms, compares the values of the two operands and returns TRUE if they are equal and FALSE if they are not.
In the fifth form, evaluates the <numeric> or <string>, exits the current function (see FN) and returns the value to the caller, as the result of the function.
Associated keywords <, >, <=, >=, <>, ENDPROC, FN, LET

Contents

Description

Assignment statement

= evaluates the expression on the right hand side to obtain its value, and sets the variable, named on the left hand side, to this value. It may be an ordinary variable (which is created if it does not exist), an array element, an indirection operation (?, ! or $) or a system variable.

If the same variable is named on both sides, its value is preserved if it exists, or it is initialised without error if not. See Forcing a variable to exist.

If one attempts to assign a string value to a numeric variable, or a numeric value to a string variable, a Type mismatch error occurs. If this is intentional then STR$, VAL or EVAL may be used to convert to the correct type.

In some other BASICs the keyword LET is needed, but it is optional in BBC BASIC. When setting the system variables, LET is in fact forbidden.

Assignment is a statement, not an operator. It is not possible to daisy-chain assignments, for example X = Y = 0 to set X and Y to 0, because X = ... expects a value from Y = 0, and so the latter is evaluated as an expression, not a statement (the = becomes an equality operator, see below).

Equality operator

= evaluates the expressions on either side, and compares their values. If the values are equal, it returns the Boolean value TRUE, otherwise it returns FALSE. It is usually used as the condition of an IF statement, but it is equally valid to assign the result to a variable.

It is not possible to daisy-chain (in)equality tests (for example IF bool% >= X = Y THEN ...), as the Group 5 operators do not associate. That is, BASIC will not use the result of one test as the operand of another, unless parentheses force it to. Instead, the expression stops after the first test, and the second test's operator and the code following are treated as a statement. If the second operator is = then it becomes a function return statement (see below), otherwise a Syntax error occurs.

This means the following idiom is valid:

DEF FNcrc16(start%,length%) IF length% = 0 = 0
...

which allows the function to return zero immediately if length% is 0.

Function return statement

= evaluates the expression on the right hand side to obtain its value and exits the current function (as ENDPROC does for procedures.) If the statement has been met outside a function definition, a No FN error results.

It then continues evaluating the expression that made the FN call, substituting the call with the obtained value.

-- beardo 21:22, 2 April 2007 (BST)