Professional Documents
Culture Documents
Lesson 2 - Basic Program Structure
Lesson 2 - Basic Program Structure
Lesson 2 - Basic Program 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:
Course Materials:
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.
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:
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.
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.
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