You are on page 1of 6

IN-TEXT QUESTION 1 1. The following numbers (constants) are not written correctly.

Indicate the errors: (a) (b) (c) (d) (e) 2. 4,75,163, +-7567 3.64E 1.5 6.05E 6125 -

Which of the following BASIC strings are correct? (a) (b) (c) (d) (e) January 26, 1997 Sixty-two 5121942 100 - 40 =60 SUM "10"

3.

Write the following as ordinary numbers: (a) (b) (c) (d) (e) 1.23E6 1.5E - 5 - 3E + 2 5.865696 E + 6 - 1.2 E -2

24.8

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) (ii) (iii) Exponentiation Multiplication and division 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.

Let us take an example where we give BASIC equivalents of a few algebraic expressions Algebraic Expression 2A+B A(B+C) BASIC Equivalent 2*A+B A*(B+C)

A +B ..................................( A + B ) /(C + D ) C +D

B2-4AC 24.9

B^2-4*A*C

RELATIONAL OR LOGICAL EXPRESSIONS Relational Operator = > <


2

A relational expression is formed by using any of the following relational operators: 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 or Variable or Expression

Relational operator

Constant or Variable or Expression

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. 24.10 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 NOT

Disjunction 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 T T F F Y T F T F OR Operator X T T F F Y T F T F NOT Operator X T F 24.11 LIBRARY FUNCTIONS IN BASIC NOT X F T X OR Y T T T F X AND Y T F F F

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 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 SIN

Name Sine

Purpose Calculate the Sine

Example SIN(X)

value of an angle (in radians) LOG SQR LOG Square root Calculate the natural logarithm of a number Calculate the square root of a number

SIN(44/7) LOG(X) LOG(100) SQR(A) 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.

IN-TEXT QUESTION 2 1. Evaluate the following BASIC expressions: (a) (b) 3 * 4/2 ^ 3 + 1 2 * (3+4) / (5-3) * 7

2. Write the BASIC expression corresponding to each of the following algebraic expression: (a) (b) (c) (d) (x+y) 4 2x2 + 5x + 6 x/y + y/z (x-y)4 / (x+y)4