CSC 113 Basic (Programming)
CSC 113 Basic (Programming)
INTRODUCTION
Basic was created in 1964 by J.G. Kemeny and T.E. Kurtz at Dartmonth College USA. The language
was originally designed as a simplified version of FORTRAN for use in Teaching programming. From
the simple beginnings, the language has grown to become a very popular multi-purpose language
available on a wide variety of machines, in particular microcomputers and minicomputers. This course is
therefore devoted to BASIS because of its usefulness
Computers Programs
The set of instructors that control the computer is called a program. A program can be executed by the
computer only when it is stored in the computer’s memory and is in machine language code. Machine
language is the only language the computer can understand. It is a language in which arithmetic / logical
operations are represented by machine – recognizable numeric codes and in which memory locations
containing data and program instructions are represented by numeric addresses.
Machine language vary from one computer manufacturer to another and as such they are machine –
dependent other types of languages, called high – level languages, have been developed to allow the user
to formulate problems in a much more convenient and efficient manner. High-level languages are
machine-independent; programs written in such languages can be processed on any type of computer.
However, high-level languages must be ultimately translated into machine language before they can be
executed by the computer. Special programs called language translators or compilers have, therefore,
been developed to provide this translation service. BASIC (Beginner’s All – purpose Symbolic
Instruction Code) is an example of a high – level language.
èArithmetic Operations
In BASIC programs, arithmetic operations are indicated by the symbols:
+ addition
- subtraction
x multiplication
** or ↑exponentiation
Types of Instruction
We consider a simple model computer that operates conceptually as its real-life counter- parts. The input
is a terminal where numbers have been entered. (typed). The memory unit consists of a group of
sequentially numbered “pigeonholes”. Locations can be referred to either by name or by address. The
arithmetic/logical unit is represented by a desk calculator capable of performing arithmetic and logical
operations. The role of the control unit played by a human operator who can fetch instructions from the
memory one at a time and execute them. The type of instructions the operator is capable of executing
includes:
1. Input: Read a value from the input medium and store that value in a specified memory location
e.g. INPUT F will cause one value on the input medium to be read and stored in a memory
location called F.
2. Conditional branding: Perform a comparison test between two values using the logical unit and
brands to a specified memory location if the test condition is met. If the condition is not met, no
transfer occurs, and the following instruction is taken from the next memory location. For
example, the instruction, IF F < 50
Go To 20
Cause the control to fist fetch F from memory and then request the logical unit to compare the
value with 50. If the logical unit reports that the value F is less than 50, the control unit fetched
the next instruction is taken from the next sequentially numbered location. If F is not less than
50, it processes the next instruction.
3. Calculations: Perform calculations using the arithmetic unit and place results in desired memory
location. For example, the instruction Let M = 5 * F will cause the control unit to fetch the value
contained in the memory location F and activate the arithmetic unit to multiply that value by 5.
The final result is then stored in memory location P (product).
4. Unconditional branding: Take the next instruction from a specified memory location. For
example, the instruction, Go to 700 cause the control units to fetch the next instruction from
location 70 rather than from the next sequentially numbered location. Unconditional branding,
then simply means transfer directly to a particular instruction in memory.
5. Output: Copy a value fro a memory location onto the output medium. For example, the
instruction PRINT F, P will cause the contents of F and P to be written out on the output pad.
6. Termination: Cease execution of instructions for this program and wait for a new program to be
loaded into memory. For example, the instruction, END causes the program to terminate. No
more instructions in this program are executed.
Sample Program
Here we wish to calculate and write out an amount of pay owed an employee who is paid =N=50 per
hour with time and =N=100 for all hours in excess of 40.
1. INPUT F
2. IF F > 40 GO TO 5
3. Let P = 50 * F
4. GO TO 6
5. LET P = 2000 + 100 * (F – 40)
6. PRINT P,F
7. END
èBASIC STATEMENTS
LET, PRINT, TERMINATION, SYSTEM COMMANDS
Elements of BASIC Language
BASIC is a set of instructions telling the computer what actions (operations) to carry out to solve a
particular problem. There are three types of BASIC operations that the computer can carry out. These
are:
1. Arithmetic operations (add, subtract, multiply, divide, raise to the power)
2. Input/ Output operations (entering data and printing results).
3. Comparison (determining whether one value is less than, equal to, or greater than another value).
BASIC instruction is made up of different elements (BASIC keywords, constants, variables, expressions,
arithmetic operators, relational operators etc) which, when combined together under a proper
grammatical structure, yielded a valid BASIC instruction.
We consider the following problem; the investment T is given by the formula
T = P (1 + T)N
Where I is the interest rate, N is the number of years, and P is the Principal. Mr. Ojo wants to deposit
=N=1,500 at a savings institution. He considers a bank and a credit union. The credit union requires a
non-refundable membership fee of =N=15. Deposits earn 12.25 percent at the credit union and 11.75
percent in the bank. Mr. Ojo will need the money in 2 ½ years. We wish to determine which institution
that Mr. Ojo should use.
The program is as follows:
10 LET p = 1500
15 LET T1 = .1225 20
20 LET T2 = .11 75
25 LET N = 2.5
30 LET T1 = P * (1 + T1) 1N – 15
35 LET T2 = p * (1 + T2) 1 N
40 PRINT “CREDIT AMOUNT”, T1
50 PRINT “BANK AMOUNT”, T2
55 END
The output
CREDIT AMOUNT 1987.43 BANK AMOUNT 1980.20
So Mr. Ojo will be advised to use credit union
Constants
A constant is a quantity whose value is fixed and explicitly stated constants may either be numeric or
alphanumeric.
1. Numeric Constants
Numeric constants are positive or negative numbers; they can be added, subtracted, multiplied, divided
and raised to a pointer by a computer. A constant is made up of any of the digits through a and may be
preceded by the + or – symbol.
Examples of valid constants
300 - 2.31 62504. + 0.3 -14
Imbedded blanks (blanks between first and last digit) do not affect the value of the constant.
Example 6 32 = 632 = 6 3 2
Examples of invalid constant
1, 634, 123 No comma is allowed
23.24. Only one decimal point is allowed
$40.42 Character $ is invalid
127 – 423 Character – is invalid
2. Alphanumeric Constants
Alphanumeric constants are generally words, messages, captions, column headings titles, names,
addresses etc. that are endorsed within quotation marks.
Examples of valid alphanumeric constants
“1425 ADEYI ST”
“1425 ADEYI ST”
“IT IS HER’S”
Note: The maximum number of characters allowed in an alphanumeric string varies from one system to
another.
Constants may be used in BASIC as follows:
LET T1 = P + (1 + T1) ↑ N – 15
PRINT -10.36, + 4.6 + 5.93, .005
LET N$ = “CHARLES”
PRINT “$”, A
3. Variables
Unlike a constant, a variable may assume different values. They are two types of variable.
1. Variables that contain numeric data can be processed arithmetically (added, subtracted etc)
2. Variables that contain alphanumeric data cannot be processed arithmetically.
The specifications for standard BASIC variable names are as follows:
Numeric Variables
A single letter of the alphabet such as A, B, Z, P or a single letter followed by one digit (0 - 9) such as
B4, Z2, D0.
Alphanumeric Variables
Same as numeric variables except that a $ is appended to the variable name,
Example: A$, B$, Z5$
The following are invalid variables as indicated I the notes
2B Does not start with letter
KRB Too many letters
B% Invalid character %
C + 2 Invalid character + BA$ B2$ would be valid
“HELLO” Is a constant
Expressions
An expression may be a constant, a variable or any combination of constants and/or variables linked by
the five arithmetic operators shown below. Note that no two arithmetic operators may be typed side by
side. Parenthesis may be included to denote the order of computations. The allowable arithmetic
operators are:
+ Addition
- Subtraction
* Multiplication
/ Division
↑or ** or ^ Exponentiation (raising to a power)
Valid Expressions
Algebraic Expressions BASIC Expressions
+ 3.5 3.5
-c -c
a.b -30 A*B -30
a/b .c (A/B) * c
Ax2 + bx + c A*X↑2+B*x+c
(a.b)2 (A*B) ↑ 2
(-c + 1.4)d (-c + 1.4) * D
ᵆ X ↑ .5
4
(ᵄ − ᵄ )3 ( (A –B) ↑ 3) ^ .25
ᵃᵅᵆᵆ − ᵆᵄᵅᵆᵄᵅᵅ (c – s) / Y
ᵄᵅᵄᵅᵆ
Bonus + hours x rate B+H*R
1/r1 + 1/r2 + 1/r3 1/R1 + 1/R2 + 1/R3
Precedence in Expressions
Operations within expression are performed according to the following rules of precedence.
Operation Precedence
( ) high precedence
↑ or **
* or /
+ or - low precedence
Operations with high precedence are performed before operations with lower precedence. The operations
addition/subtraction, multiplication/division are performed in order from left to right according to the
rule of precedence. Exponentiation is performed in order from right to left. If parenthesis is nested, the
operation in the innermost set of parenthesis is performed first.
Consider the expression
3 + (2 * 2 ↑ 3 / 4 + 1) – 2 / 3
8
16 4
5 .6666 …
8
7.3333…
1. A – B + C B is subtracted from A, and the result is added to C
3 – 2 + 5 (3-2) + 5 = 1 + 5 = 6
2. A + B * C Since multiplication has priority, B * C is computed; the result is then added to A
giving A = (B * C).
3 + 2 * 3 3 + (2 * 3) = 3 + 6 = 9
3. A/B * C Since multiplication and division have the same priority B is first divided into (A/B),
and the result of the division is multiplied by C
9./ 4. * 2 (9./ 4.) * 2 = 2.25 * 2 = 4.50
4. A/B/C First A/B is performed, and the result is then divided by C 8/4/2 8/4 ÷ 2 = 2 ÷ 2 = 1
5. (A + B) / C * D = ( (A + B) / C ) * D
So (3 + 6) / 3 * 6 = ( (3 + 6) / 3 ) * 6 = 3 * 6 = 18
6. A + B * C ↑ 2 = A + (B * (C ↑ 2) )
3 + 3 * 2 ↑ 2 = 3 + (3 * 22) = 3 + (3 * 4) = 3 + 12 = 15
7. A ↑B ↑C = (A ↑ B) ↑ C 3 ↑ 2 ↑ 3 = (3 ↑ 2) ↑ 3 = (32)3 = 93 = 729
The Replacement Statement LET
A replacement statement evaluates an expression and stores the value of the expression in a memory
location identified by a variable name. The general form of a replacement statement is
In most BASIC systems, the key word LET is optional and need not be specified by the user. When the
particular statement is carried out (RUN) the value of the expression is first computed and the result is
placed (stored) in the variable (memory location0 specified on the left – hand side of the equal sign.
When we write 10 X = 2 * X + 1
We are looking at the expression and saying “find out what is in X, multiply it by 2, add 1 and store the
result in memory location X. the equal sign must be understood as a replacement sign. Thus, if the
memory location contains 4, then 2 * X + 1 = 9 and the value 9 is stored in X. i.e. the value X is replaced
by 9.
Examples of valid replacement statements are:
10 LET X = -0.07 Store value - .07 in X
12 X1= A + B / C -1 Evaluate the expression and store the result in X1
14 LET C$ = “GROSS PROFIT” Store the string “GROSS PROFIT” in C$
15 I = I + 1 Computer I + 1 and store result in I
Line Numbers
Each BASIC line must be numbered. Line numbers must be unsigned integers not exceeding five digits.
In most systems, statements or lines may be entered in any desired order; the system will sort the
statements in order when the program is list or executed. Statements will be processed by the computer
in ascending order under a decision statement (IF) or unconditional transfer (Go To) is encountered.
Example
10 LET X = 3
20 PRINT X, Y
15 LET Y = X ↑ 3
BASIC would store these statements in its memory in the following orders
10 LET X = 3
15 LET Y = X ↑ 3
20 PRINT X, Y
Number Representation
Constants in BASIC can be expressed in two different notations
1. Decimal notation, with or without decimal points.
Examples: 45.67, -12.3, 11240, -48216.
2. Exponent notation, where the character E is used to represent the decimal base 10, followed by a
two digit (with or without sign) to represent the exponent (power).
Examples:
10 LET X = 3.456 E + 2 where 3.456 E + 2 = 3.456 x 102
12 LET Y = -3. E -3where -3.E-3 = -3 x 10-3 = -.003 14 LET Z = 1.23 EO where 1.23 EO =
1.23 x 100 = 1.23
On output, as a result of the PRINT statement the BASIC will use E notation to represent a constant if
the value of the constant is outside a certain range. The range will vary from one computer to another.
Example:
10 LET X = 123456
20 LET Y = 123456 789012345
30 LET T = . 0003245
35 LET Z = 22.1 E 3
45 LET V = -.001
48 LET W = .01
49 PRINT X; Y; T; Z; V; W
RUN
123456 1.2345679E + 14 3.245 E - 4 22100 -1E-03 .01
Programming Example
Income Calculation
Mr. V is a widower with three children aged 12, 10 and 19. His monthly salary is $1,523.36. His
monthly contribution to a retirement plan is 6.6 percent of his first 9 month’s salary; retirement
deductions are spread over a 12 month period for each child support for social security. His monthly
social security deduction is 6.7 percent of his monthly income, and his federal income tax is 15.6 percent
of his yearly gross (deducted on monthly basis). Monthly payments for life insurance equal 9.6 percent
of his monthly salary after social security and federal tax deductions. Write a program to compute his
monthly spendable income.
Remark: Note the GO TO 10 statements at the line program repeatedly. The process of repeating a
sequence of instructions is called a Loop. In this case the loop is infinite and the only way the user can
escape from it is by pressing a special key on the keyboard (see your user’s manual
The unconditional Transfer Statement GO TO
The General form of the Go to Statement is
line number Go TO transfer line number
A BASIC program consists of a sequence of BASIC STATEMENTS. Basic will process these
statements one after another in sequential order. When BASIC encounters a GO TO statement it will
transfer control to the statement specified, that is, processing will continue at the transfer number. It also
allows the program to branch back to repeat (reprocess) certain instructions or certain procedures; this is
called looping
Examples 1
GOT TO 50
20 - This code is by passed
50 LET Y = 3 * Z
Example 2
20 INPUT H, R, B
This block of code is processed repeatedly.
50 GO TO 20 variables) is used. The contents (value) of each location are to be printed on some output
device. For example, the Block
Indicates that values in locations P and H are to be displayed (written) on some device
PRINT P, H
PRINT V
PRINT P2
P=H*R
PRINT N$, P
STOP
a. Flowchart Definition
Because of the requirement for logical organization and requiring of program instructions, it is usually
difficulty to write a computer program without first expressing the program in some preliminary form.
For a complex problem the tool is the flowchart although for a simple problem a verbal outline of steps
required may suffice. A flowchart is a pictorial representation of the logic (method) used to solve a
particular problem. A flowchart is particularly useful for visualizing paths through the logic of a
program
b. Flowchart symbols
In program flowcharts, the symbols shown in the figure below is normally used. Each symbol or block
represents a different type of operation written within the blocks are instructions to indicate (in general
terms) what operation is to be performed. It is not necessary to express the instructions used in a
flowchart block in any particular computer language. The emphasis in the less than or equal to (≤),
greater than (>), greater than or equal to (≥) or not equal to (>). The decision blow is the only block from
which two different logical paths may be selected follows indicate the path to be taken depending on the
decision block.
IS
H > 40 Yes
No
If the value of H is greater than 40, the path marked YES is taken, otherwise the NO path is taken.
c. Flowlines
The sequence of instructions to be executed in a flowcharted algorithm is denoted by straight
lines with an arrowhead such as → or ↓. The direction of flow is always in the direction
pointed by the arrowhead.
d. Connector Blocks
When it is convenient to draw flowlines to connect one area of the flowchart to another,
connectors are often used. Connectors serve two purpose:
1) To identify a block by a label for reference purposes
2) To indicate transfer to another labelled block.
The symbol used for a connector is a circle .
A label is placed in the connector block when the flow line points away from the connector, such as
→ , the connector is being used to denote an entry point, that is, a block to which transfer will be
made from some point in the flowchat when the flow- line points toward the connector as in → , the
connector is being used to indicate a transfer.
If / Then
Then general form of the If statement is
line number if condition {GO TO THEN} transfer line number
Where the condition consists of two arithmetic expressions or character string variables linked together
by one of the relational operations shown in below:
Where the statement can be an input / output instruction, a replacement statement or a STOP. If the
condition is met, the statement (and only one) is executed and the control is transferred to the condition
is not met control is transferred to the statement following the If.
Example 1
10 If A < B THEN K = K + 1 If A > B the statement K = K + 1
will executed after which statement
15 If A = 4 GO TO 67 15 will be executed. If A ≥ B 15
will be processed next.
Example 2
10 If C < > 0 THEN PRINT A IF C ≠ 0 print the value
A and stop otherwise ( C
15 STOP ≠ 0) stop
Since only one statement is permissible after the THEN keyword the following code would be needed to
account for the following flowchart
K=5
The REM Statement
The general form of REM statement is
line number REM literal characters
Where REM (remark) is a key word, and the literal characters are supplied by the programmer.
Although the REM statement requires a line number, it is not an executable statement.
èLOGICAL EXPRESSIONS AND THE AND / OR LOGICAL OPERATORS
A logical expression can be thought of as a proposition. A proposition is a statement that is either true or
false.
And
Examples
1. To determine whether A lies between 7 and 14, the following statement could be used:
If A > > AND A < 14 THEIR PRINT “YES” Note that the statement If A > > ABD < 14 is
invalided. No logical operator may be side by side with a relational operator.
2. Parentheses may be used to devote the order in which the expressions are to be evaluated
If ( S = 1 AND M 1 < > 0) OR (S = 3 AND M1 > 4) THEN PRINT S, M1
On / Go To Statement
The ON/GO TO statement is a useful and convenient statement that allows transfer to many different
points in a program
Line number ON expression GO TO line number line
Another form of the If statement is
line number on expression GO TO line number 1, - - line number n
The rule is the following
If the value of the expression is 1 control is transferred to line number 1
If the value of the expression is 2, control is transferred to the line number 2 Examples
20 ON N GO TO 3, 57, 100, 4. IF N = 3 control is transferred to 100
15 ON J – 2 * K GO TO 30, 10 IF J – 2 * K = 1 go to statement 30
30 ON Y ↑1 GO TO 10, 20, 60 IF Y ↑ I = 1, 2, 3, go to 10, 20 and 60 respectively.
èData Statement
The general form of the DATA Statement is
Where DAT is the keyword. The constant – list consists of numeric or character constants. Items in the
list must be separated by commas.
DATA is a non executable statement, unlike all other BASIC statements, except REM – encountered so
far.
The DATA statement inform the BASIC system that the numbers specified in its constant
– list are to be stored in memory until the user decides to process these numbers during program
execution.
Example
1 DATA 1, 3.1
5 READ X, Y, Z, W$ 10 PRINT X, Y, Z, W$
15 DATA 3, HI
20 END
RUN
1 3.1 3 HI
Example 2
1 DATA 1, 3.1, 3, HI
5 READ X
10 READ Y
15 READ Z, W$
20 PRINT X, Y, Z W$
25 END
RUN
1 3.1 3 HI
Example 3
1 DATA 1
5 DATA 3.1
10 READ X, Y, Z, W$
15 DATA 3, HI
20 PRINT X, Y, Z, W$
25 END
RUN
1 3.1 3 HI
2. Parentheses may be used to devote the order in which the expressions are to be evaluated
If ( S = 1 AND M 1 < > 0) OR (S = 3 AND M1 > 4) THEN PRINT S, M1
Where READ is a key word. The variable list consists of variables separated from one another by
commas
The READ statement causes as many values to be fetched from the DATA block as there are variables
in the READ list.
5 READ X, Y
10 DATA 1, 3
15 READ 2$
20 DATA WE. 4. 6
25 READ W