Lesson 2 - Basic Program Structure

You might also like

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

Lesson 2 - Basic Program Structure

Unit 1 ELEMENTS OF BASIC STRUCTURE

Overview:

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.

Learning 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.

Course Materials:

BASIC AS A PROGRAMMING LANGUAGE


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.

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.

18
SUBJECT: CMPE 20022 – COMPUTER PROGRAMMING
PREPARED BY: VIRGILIO R. CUAJUNCO, JR., MEM
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.

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.

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
19
SUBJECT: CMPE 20022 – COMPUTER PROGRAMMING
PREPARED BY: VIRGILIO R. CUAJUNCO, JR., MEM
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.

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.

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

20
SUBJECT: CMPE 20022 – COMPUTER PROGRAMMING
PREPARED BY: VIRGILIO R. CUAJUNCO, JR., MEM
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

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.

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.

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

21
SUBJECT: CMPE 20022 – COMPUTER PROGRAMMING
PREPARED BY: VIRGILIO R. CUAJUNCO, JR., MEM
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.

22
SUBJECT: CMPE 20022 – COMPUTER PROGRAMMING
PREPARED BY: VIRGILIO R. CUAJUNCO, JR., MEM

You might also like