Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Unit 2 Operators Expressions

Overview:

An operator in a programming language is a symbol that tells the compiler or interpreter to


perform specific mathematical, relational or logical operation and produce final result. This
chapter will explain the concept of operators and it will take you through the important arithmetic
and relational operators.

Learning Objectives:

After successful completion of this lesson, you should be able:

1. to write in computer expression an algebraic expression


2. to write Boolean expression and truth table

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

Algebraic Expression BASIC Equivalent


2A+B 2*A+B
A(B+C) A*(B+C)
A+ B
..................................( A + B) /(C + D)
C+D
B2-4AC B^2-4*A*C

RELATIONAL OR LOGICAL EXPRESSIONS

A relational expression is formed by using any of the following relational operators:

Relational Operator Meaning


= Equal to
> Greater than
< Less than
<= Less than or equal to
>= Greater than or equal to
<> Not equal to

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.

The common logical operators are:


• AND Conjunction
• OR Disjunction
• NOT Logical Negation

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

LIBRARY FUNCTIONS IN BASIC

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.

To find the logarithm of Χ by using LOG function you write

10 LET Y = LOG (X),

The variable Y will store the LOG value of X.

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.

Function Name Purpose Example


SIN Sine Calculate the Sine value SIN(X)
of an angle (in radians) SIN(44/7)
LOG LOG Calculate the natural LOG(X)
logarithm of a number LOG(100)
SQR Square root Calculate the square SQR(A)
root of a number SQR(100)

(c) The function name is to be written exactly as given. No deviation is permitted.

(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

You might also like