Anda di halaman 1dari 11

ELEMENTS OF BASIC STRUCTURE 24.

1 INTRODUCTION

In this lesson, you will be introduced to a programming language called BASIC. It is the most popular conversational programming language. Various versions of BASIC have been developed by computer manufacturers for their computers. This language is quite simple to understand and has been adopted by most of the microcomputers. It is suitable for both mathematical and business problems. It has been specially designed for use in `time-sharing' environment but can also be used as a standard language in a `batch-processing' environment. 24.2 OBJECTIVES

At the end of this lesson, you should be able to know how to construct constants, variables in BASIC language and expression using mathematical, relational and logical operators. learn the structure and essential elements of BASIC. use some elementary BASIC commands in small programs. BASIC AS A PROGRAMMING LANGUAGE

24.3

Basic is quite simple language and easy to understand. It is becoming more popular as an interactive language. Now we shall discuss various features of this language. 24.3.1 Why BASIC? B in BASIC stands for Beginners. Obviously, it is meant for those who are about to start learning programming. To qualify as a beginner's language, it has to be simple, easy and readily available. BASIC has all the qualities. Although, it is the beginner's language, its use by no means is restricted to basic computer education or elementary applications. The second alphabet A stands for all-purpose. It means BASIC can be used for a variety of applications involving business, science, mathematics and graphics. In fact, with new enhancements, BASIC has some of the powers of more advanced languages. The all purposeness is also derived from the fact that very large segments of games and computer courseware are written in BASIC. The other three letters i.e. SIC stand for Symbolic Instruction Code -- means the instructions may not be exactly in English but expressed in symbols.

The advantages of BASIC can be summed up as follows: BASIC is easy to learn and fun to practice. It may be called a `People's language'. It is available almost in every computer from micro to mainframe. Therefore, a program developed in a micro can run on bigger system with minor modifications. Program development cycle is quick, debugging is simple, and Modification of programs is quite easy.

24.3.2 History of BASIC Conceived by John Kemeny and Thomas Kurtz, BASIC was born at Dartmouth College, USA in 1964. It had a rather humble beginning. The classical languages like COBOL or FORTRAN were already developed. COBOL, in fact, was already established as the major data processing language. FORTRAN on the other hand, became the principal programming language for scientific applications. Both these languages, however, were not very simple to learn. There were extensive rules on syntax and structure of the programs. Because of its simplicity, BASIC gained immediate popularity. It became a widely used language for small commercial and scientific applications. BASIC had the same impact on the world of programming languages as the PC had on the larger computer systems. BASIC's popularity and acceptance by the user community compelled all the major computer manufacturers to come out with their individual version of BASIC. When the microcomputer or PCs were introduced, BASIC found a most convenient vehicle for its proliferation. The ease and simplicity of BASIC made it the standard programming language for the microcomputer world. Even your MS-DOS operating system comes with a BASIC interpreter called BASICA. (MS-DOS 3.2 onwards, a more enhanced BASIC version, i.e., GWBASIC forms the part of the operating system.) American National Standards Institute (ANSI) in 1978 came out with a standardised version of BASIC language. Most of the versions of the currently available BASIC include the features of the ANSI standard. 24.4 STRUCTURE OF BASIC

The general structure of standard BASIC language is best understood by a practical program. The following small program prints the words WELCOME TO NATIONAL OPEN SCHOOL on the screen: 10 20 REM: This is my first program PRINT "WELCOME TO NATIONAL OPEN SCHOOL"

30

END

Keeping this program in view, let us go over the rules on the structure of the language. These are: Each statement of instruction must appear on a separate line. A statement cannot exceed 80 characters i.e. one line on the terminal. A statement must start with a statement number. It is a positive integer. No two statements can have the same numbers. The statement must be in the ascending order. Each statement number is followed by a BASIC command or keyword. You may use blank space to increase the readability of the program.

There is no restriction on blank lines within a program. These are the rules, as applicable to standard or "Dartmouth BASIC" as it is called. The Microsoft BASIC i.e. BASICA or GWBASIC, however, has provision for more than one statement in the same line and the use of keywords also need not follow a line number. But if you strictly follow the rules of standard BASIC, the program will still remain valid in Microsoft or any other BASIC. Now let us learn about some of the keywords, you came across in the program. 24.5 ELEMENTARY BASIC STATEMENTS

In the program, you came across their important BASIC commands or keywords. These are REM, PRINT and END. We will briefly discuss REM and END here. The PRINT will be discussed in greater details in subsequent lessons. Presently for the purpose of understanding, you may take PRINT, as an output instruction. The output may be meant for the terminal (as in this case) or it may be directed to a disk file. What is important, is to remember, that in BASIC, print does not necessarily mean a print out on a paper; it may mean display on the terminal or write on a disk file also. 24.5.1 REM The REM is the short form for REMARKS. At the time of execution, when the interpreter comes across the first three alphabets, as the keyword, it knows that the content of the line following REM is not meant for execution and is to be ignored. REM is used purely to enhance the documentation aspect of a program. It is meant for the reader of the program to understand the contents, purpose, logic and other such aspects. Whatever, information on the
3

program, you want to convey to the reader of the program, may be placed after REM. There is no limit on the number of REM statements in a program. In fact, it is recommended that you get used to judicious and copious use of REM statements. It may not only help the reader, but it may also help you in understanding your own programs, particularly if those are old or lengthy. At times to enhance the presence of a REM statement you may place a blank line above or below a statement. This makes it more visible in lengthy programs. You can place a blank line by a PRINT statement. If we want to insert a blank line after the REM statement of the program, we may write the program as: 10 20 30 40 REM : This is my first program PRINT PRINT "WELCOME TO NATIONAL OPEN SCHOOL" END

24.5.2 END The END statement was mandatory in the older BASIC version to indicate the end of a program. In other BASIC like Microsoft BASIC, it is not compulsory. In such cases, the program terminates at the last or highest line number. Whenever END is used, it must be the last line. Any statement after the END is irrelevant to the interpreter. Having been familiarised with a small BASIC program, let us now see the elements other than the keywords, comprising this language. 24.6 (a) (b) (c) THE CHARACTER SET Alphabets: A, B, C, ----. Z Digits: 0,1,2, ----, 9 and Special characters: + - * / ( ) . , $ ; : = > < " ^

BASIC has the character set consisting of the following elements:

The symbol ^ (caret) is used to denote exponentiation operator, the symbol * (asterisk) is used to denote multiplication and the other symbols have their usual meanings. In addition these, blank space denoted by the symbol b is also regarded as a character.

24.7

CONSTANTS AND VARIABLES


4

A quantity in a computer program which does not change its value during the execution of the program is called a constant and the quantity which may change its values during the execution of the program is called a variable. BASIC allows the following constants: Numeric constant String constant

Numeric constant is one that is formed by a sequence of digits 0,1,2, ---, 9 and may include a decimal point. A numeric constant known as number may be as integer or a real number. 383, + 57, 0, - 6.2 and 6.15E4 are valid numeric constants. The number 6.15E4, in fact, represent 6.15 104. The notation E is used to represent exponential form. The number after E is the exponent which can be positive or negative. However, its length cannot exceed two digits. It is also important to keep in mind that BASIC does not distinguish between an integer and a fraction. Commands are not allowed in a numeric constant The limit on the number of digits that can be used varies from computer to computer. Normally, a numeric constant can have up to a maximum of eight digits. A string constant consists of a sequence of characters. It must be enclosed by a quotation mark. This may contain blank space as a character but it should not include the quotation mark. String constants are used to represent non-numeric quantities such as names, addresses, etc. For example, "PANKAJ GOEL", "SUM = Rs 75", "162" are a few valid string constants. In BASIC, variables are also of two types. They are Numeric variable String variable

Numeric variable can assume numeric value and is represented by an alphabet or an alphabet followed by another alphabet or a digit. For example A, C, A2, ABC, A6 etc, represent numeric variables. A string variable is represented by an alphabet followed by dollar ($) sign. It should be kept in mind that while constructing the string variable, dollar ($) should be the last character. For example, A1$, PKG$, BOOK1$, etc., are valid string variables.

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


7

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)

10

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

11

Anda mungkin juga menyukai