Professional Documents
Culture Documents
1
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 REM: This is my first program
20 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
2
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
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 REM : This is my first program
20 PRINT
30 PRINT "WELCOME TO NATIONAL OPEN SCHOOL"
40 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.
3
THE CHARACTER SET
BASIC has the character set consisting of the following elements:
(a) Alphabets: A, B, C, ----. Z
(b) Digits: 0,1,2, ----, 9 and
(c) Special characters: + - * / ( ) . , $ ; : = > < " ^
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
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.
4
IN-TEXT QUESTION 1
1. The following numbers (constants) are not written correctly. Indicate the errors:
(a) 4,75,163,
(b) +-7567
(c) 3.64E 1.5
(d) 6.05E
(e) 6125 -
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
5
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
Constant Constant
or or
Variable Variable
or Relational or
Expression operator 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 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
7
X NOT X
T F
F T
8
Function Name Purpose Example
SIN Sine Calculate the Sine SIN(X)
value of an SIN(44/7)
angle (in radians)
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.
IN-TEXT QUESTION 2
1. Evaluate the following BASIC expressions:
(a) 3 * 4/2 ^ 3 + 1
(b) 2 * (3+4) / (5-3) * 7
9
which is going to be your practical vehicle for learning this language however, an enhanced
version of standard BASIC, incorporating many additional features to add power to the
language. It must however, be emphasized that even if you stick to the rules of standard
BASIC, it still remains completely valid for GWBASIC or BASICA and can be run without
any problem.
1.13 TERMINAL QUESTIONS
1. Define BASIC language along with its features.
2. Describe briefly the structure of BASIC language.
3. Differentiate between
(a) Numeric and String constant
(b) Arithmetical and Relational Expression
4. Write down the hierarchy of operations in Arithmetic Expression.
5. Define history of BASIC language briefly.
1.14 FEEDBACK TO IN-TEXT QUESTIONS
IN-TEXT QUESTION 1
1. (a) Commas are not allowed
(b) Two arithmetic operators +, - for the sign cannot be used simultaneously
(c) Exponent cannot contain a decimal point
(d) E must the followed by some integer number
(e) Negative sign should not be in the last
2. (a) correct
(b) correct
(c) correct
(d) correct
(e) Incorrect because quotation marks are not allowed in strings.
3. (a) 1230000
(b) 0.000015
(c) -300
(d) 5865696
(e) - 0.012
IN-TEXT QUESTION 2
1. (a) 2.5
(b) 49
2. (a) (x+y) ^4
(b) 2*x ^ 2 + 5*x + 6
(c) (x/y) + (y/z)
(d) (x-y) ^4 / (x+y) ^4
10