Professional Documents
Culture Documents
L2U2 - Operators Expressions
L2U2 - Operators Expressions
Overview:
Learning Objectives:
Course Materials:
ARITHMETIC EXPRESSIONS
A BASIC system can handle arithmetic expressions involving the five arithmetic operators +
(addition), - (subtraction), *(multiplication), /(division) and ^ (exponentiation). The hierarchy of
operations is as follows:
(i) Exponentiation
(ii) Multiplication and division
(iii) Addition and subtraction
Thus, in a particular arithmetic expression, the order of execution is as per this hierarchy, i.e. all
exponentiation operations are performed first, then multiplication/division and the
addition/subtraction operations are the last to be carried out. Note that within a particular
hierarchical group, the operations are executed form left to right. Normal hierarchy of operations
can be altered by use of parentheses. The operations within the innermost parentheses are
performed first and then the second innermost and so on.
In addition to this hierarchy of operations, the following rules must be kept in mind in arithmetic
expression:
• Two operations must not appear together. For example, C+-D, A/-C, etc are not
permitted.
• String constants and string variables should not be used in arithmetic expressions.
For example, P+P$ is wrong.
• When brackets are used, they must be used in pairs, i.e., every left bracket must be
matched with a right bracket.
• Denominator of an expression should not be zero.
• Within a given pair of parentheses, the natural hierarchy of operations will apply.
24
SUBJECT: CMPE 20022 – COMPUTER PROGRAMMING
PREPARED BY: VIRGILIO R. CUAJUNCO, JR., MEM
Let us take an example where we give BASIC equivalents of a few algebraic expressions
In the execution of programs, it is sometimes desired to compare two numerical quantities (or
sometimes string quantities) and take decisions on achieving certain conditions. For example, we
may be interested to check the number of repetitive calculations performed or to find out whether
the denominator of an arithmetic expression has become zero or if a particular quantity is
negative, and so on. Expressions written to compare two quantities using certain relational
operators are known as relational expressions. These expressions take only one of the two
values, namely, TRUE or FALSE, For instance, the relational expression A > B will be true if A is
greater than B, otherwise FALSE. This test result is used to change the sequence of execution of
statements in a program. The general form of a relational expression is as follows:
Constant Constant
or Relational
or
operator
Variable Variable
When expressions are used on either side of the relational operators, the expressions will be
evaluated first and then the results of expressions compared. This means that relational
operators come last in the hierarchy of operators.
Logical expressions are used in IF---THEN Statements to determine the course of action of a
running program.
25
SUBJECT: CMPE 20022 – COMPUTER PROGRAMMING
PREPARED BY: VIRGILIO R. CUAJUNCO, JR., MEM
LOGICAL OPERATORS
Like relational operators, BASIC, also supports logical operators to perform logical operation on
numerical values. Logical operators are used to connect two or more relations and return a
TRUE or FALSE value to be used in a decision.
For example, the expression A > 50 AND B > 150 is TRUE when A is more than 50 and at the
same time B is more than 150.
Logical operators return results as indicated in the following tables. T indicates a TRUE and F
indicates a FALSE. X and Y are relational expressions.
AND Operator
X Y X AND Y
T T T
T F F
F T F
F F F
OR Operator
X Y X OR Y
T T T
T F T
F T T
F F F
NOT Operator
X NOT X
T F
F T
The word `library' stands for collection. In the context of computer languages, a library is
essentially a collection of useful programs. These programs are used by the programmers to
simplify their task of program development. These programs are often referred as subroutines.
The programmer does not have to know the details of the sub-routine. Most of the programming
languages are offered with a number of sub-routines called sub-routine library or library
functions. These built-in library functions are used to simplify some useful common functions like
calculation of square root, log of a number or cosine of an angle. BASIC's library is rich with
mathematical functions. For example, suppose you want to calculate the square root of a
26
SUBJECT: CMPE 20022 – COMPUTER PROGRAMMING
PREPARED BY: VIRGILIO R. CUAJUNCO, JR., MEM
numeric variable A. In simple mathematics, square root of A, i.e., can be written as A½. Thus in
BASIC it can be written as:
10 LET B = A^0.5
The value of B will be the square root of A. Using a library function the operation can be
performed as
10 LET B = SQR(A)
Of course, from the example, it appears that there is hardly any benefit in using the library
function. But imagine, if problem is little more complicated like calculation of the sine value of an
angle or logarithm of a number. The programming algorithm to calculate these is not so simple.
From this example, we may deduce the rules, governing the use of a function.
(a) Each function is assessed by the function name (LOG, SQR, etc.) followed by the function
argument placed within the parenthesis.
(b) The function argument is the information you supply to the function to act upon it. For
mathematical factions, it has to be a numeric constant or variable. Listed below are some
examples of mathematical functions in BASIC.
(d) You cannot have a blank space between function name (SIN, LOG etc.) and the beginning of
the opening parenthesis enclosing the argument.
It should be noted that a library function program would produce the result faster than a BASIC
program that has been written to perform the same task. For example, calculation of square root
by the SQR function will be faster than writing in the form of a program. This is due to the fact
that library functions are optimised for the particular BASIC interpreter provided by the supplier.
27
SUBJECT: CMPE 20022 – COMPUTER PROGRAMMING
PREPARED BY: VIRGILIO R. CUAJUNCO, JR., MEM