Commodore‎ > ‎BASIC‎ > ‎

Expressions

BASIC expressions are so fundamental that their operation is rarely documented; or at least, not fully documented.  BASIC expressions are a core element of the BASIC language: they are used in almost every BASIC command/function/statement!

There are four types of BASIC expressions:
1. a literal value (for example, the number 10, or the string "HELLO")
2. a variable [ for example, a scalar floating-point variable X, or a string Q\$, or an array variable, like Q\$(2) ]
3. a function [ for example, SQR(9) or FN Y(X) ]
4. a generic expression (for example, 2*X - 1)
In regards to case 3 example "FN Y(X)" [a user function], this is a special instance of case 2 (a variable) because user functions are stored much like normal (scalar) variables.

 1: Literal value
These can be considered as constants.  Examples:
 ```PRINT 10 the EXPRESSION is simply 10 10 READY. PRINT "HELLO" the EXPRESSION is simply HELLO HELLO READY. 10 A = 10 the EXPRESSION is simply 10 20 Q\$ = "HELLO" the EXPRESSION is simply HELLO ```

 2: Variable
These are some value created/saved by a program (typically) or the user in direct mode (rarely).  Examples:
 ```NEW READY. 10 A = 10 20 Q\$ = "HELLO" 30 PRINT Q\$ the EXPRESSION is 'variable' Q\$ 40 PRINT STR\$(A) the EXPRESSION is 'variable' STR\$(A) READY. RUN HELLO 10 READY. ```

 3: (User) function
This a formula which is auto-calculated by BASIC.  Example:
 ```NEW READY. 10 DEF FN DEG(X) = 180/π*X 20 PRINT FN DEG(1.57) the EXPRESSION is the user function 'DEG' READY. RUN 89.9543739 READY. NEW READY. 10 PRINT SQR(9) SQR is a built-in function "Square Root00000" READY. RUN 3 READY. ```

 4: Generic expression
This a generic (usually algebraic) formula.  NOTE: this is (by far) the most common type of expression. Examples:
 ```X=5 READY. PRINT 2*X-1 the EXPRESSION is 2*X-1 9 READY. A\$="HELLO " READY. PRINT A\$+"WORLD" the EXPRESSION is A\$ + "WORLD" HELLO WORLD READY. PRINT SQR(X) the EXPRESSION is SQR(X) 2.23606798 READY. PRINT MID\$(A\$,2,2) the EXPRESSION is MID\$(A\$,2,2) EL READY. ```

In the examples above, you could substitute PRINT with variable = EXAMPLE to store a value (expression result) instead of printing it.

Note that case 4 "Generic Expression" invokes OPERATOR PRECEDENCE.  In very general terms, a BASIC expression is evaluated left-to-right, but if an operator is encountered with higher priority than the current sub-expression, the later (high-priority) operator will "take precedence" which means the following sub-expression will be evaluated before the prior sub-expressions.  If you are familiar with Algebra's precedence (order-of-operations) this should seem natural.  But anyway I list them all below (by the priority used in BASIC expression evaluation) in case you aren't intimate with Algebra or maybe you've been using another computer language (with different precedence).
1. Anything enclosed in parentheses "(" and ")"... this includes functions like COS(X)
2. Power operator; for example, 2 ^ 7
3. Negation, Positivation(?) [for example -2 or +X], Multiplication, and Division [for example 2*X or 16/X.]
4. Addition and subtraction; for example 2+X or X-2.
5. Relational operators (<, =, >, <>); for example X <> 2 (X not equal two)
6. Bit-wise (logical) operator NOT
7. Bit-wise (logical) operator AND
8. Bit-wise (logical) operator OR

Note in some cases listed above, where there are two or more operators on a single line (like addition and subtraction) , BASIC will evaluate those (same-priority) operators in left-to-right ("first come, first serve") order.  Again, if you are familiar with Algebra or programming with other languages, this may seem rather obvious.  But below are some examples which I hope will de-mystify any confusion:

 ```PRINT -2^2 power (^) is evaluated before negation (-) -4 READY. PRINT (-2)^2 force negation (-) before power (^) 4 READY. PRINT 4+3*2 multiply(*) is evaluated before addition (+) 10 READY. PRINT (4+3)*2 force addition (+) before multiply(*) 14 READY. ```