An expression is the combination of operators, constants and variables that is evaluated to get a result.
The simple algebraic expression like
A+5, 7+2, X+Y, P-Q
They are simple arithmetic expression where numeric variables or constant are combined using arithmetic operators like + and – .The other expressions such as
I>N, M>=A, J=M, N<25
These are known as logical or relational expressions. They compare two variables and are used for making certain decisions in program logic.
BASIC thus deals with two types of expression, they are
a) Arithmetic expression, and
b) Logical expression.
Certain arithmetic operators (or symbols) which are meaningful to the computer are to be used to evaluate such arithmetic expressions. BASIC system uses the following five arithmetic operators:
+ for addition
– for subtraction
* for multiplication
/ for division
↑ for exponentiation
NOTE:- some system use ** exponentiation
Illustrates the use of arithmetic symbols.
HIERARCHY OF OPERATION
During the evaluation of expressions, the operation are executed one after the other and, in doing so, the computer assigns an order of priority to operation. The operation of high priority are performed before those of low priority
Operation order of priority
( ) expression inside 1
↑ exponentiation 2
/ and * division and 3
+ and – addition and 4
Operations of equal priority are performed form left to right. In the expression.
RULES OF ARITHMATIC
(1) two operation should never appear together, e.g
A/-B and A↑-B are wrong
A/(-B) (or –A/B) and A↑(-B) are correct
(2) No attempt should be made to raise zero to a negative power, e.g in
X↑Y, X should not become zero while Y is negative.
(3) A negative value should not be raised to real number (fractional number )
e.g. (-5)↑2.5 is not allowed .
(4) Denominator of an expression should never be zero, e.g.
In the expressions (A+B)/(C+D), C and D should never become equal.
Compare the rule 2 with this rule.
(5) String constants or string variables are not allowed in expression, e.g.
A + ”B” * C
X * 3.5 – “4 + 2”
N$ * A ↑ D
are invalid expressions.
(6) when variables are used in an expression we should make sure that they are assigned values before attempting to evaluate the expression.
(7) Every left bracket must be matched by right bracket.
Any violation of these rules will be a syntax error and a corresponding error message will be provided by the computer.