Introduction To Computer Programming (Basic) : October 2016
Introduction To Computer Programming (Basic) : October 2016
Introduction To Computer Programming (Basic) : October 2016
net/publication/317182495
CITATION READS
1 54,944
4 authors, including:
Gbenga Ogunsanwo
Tai Solarin University of Education
2 PUBLICATIONS 1 CITATION
SEE PROFILE
Some of the authors of this publication are also working on these related projects:
All content following this page was uploaded by Usman Opeyemi Lateef on 28 May 2017.
Variables in BASIC
A variable is a name or identifier that represents number or a string of characters whose value can
change during or after the execution of a BASIC program.
There are two types of variable valid in BASIC, they are:
Numeric Variables
String Variables
Each of the following variables represents a numerical quantity:
A SCORE1 AGE% PRICE2# TOTCOST! K3 RMNUMX
The following variables represent non-numeric (i.e. string, also called alphanumeric) values each
A$ ADDRESS AGE2$ COURSES$
TITLE$ X45$ DEPT$ REMARKS RULE0$ NAMES. The rule is that every
alphanumeric, that is string variable must end with the data-type symbol $. When a variable name has
no $ symbol after it, it is taken to be a numeric variable. The type of numeric variable it is can further
(optionally) be indicated by attaching % (for integers),! ‘(for single precision numeric variables) or #
(for double precision numeric variables) to the right end side of the variable name.
Constants in BASIC
A constant is an identifier, a number, a string of characters whose value cannot change during or after
the execution of a BASIC program.
The Two types of constants we have are:
Numeric constants e.g. 0, epilson, TOT etc
Alphanumeric or string constants e.g. “The bride’s age is”, “Final Score is” etc.
The rule is that a string constant must be quoted with double question marks.
Operations in BASIC
BASIC recognizes the following different types of operations in a BASIC program.
Arithmetic Operations
Arithmetic operators are used to carry out arithmetic operations in BASIC. These operators are
executed according to their hierarchy or what is called precedence rule. Table 16.1 below shows these
operators according to their hierarchy.
Table 6.1: Hierarchy of arithmetic Operators in BASIC
Hierarchy Operation Operators used Examples
3 E – Exponentiation A M = a + (b 4)
Or
M. = a + (bA4)
2 M – Multiplication * a*b=b*a
1 A - Addition + a + b=b + a
S - Subtraction - 9 – 6 =3
1
The hierarchy determines the sequence Basic follows in executing a group of arithmetic operators
found in an arithmetic expression. The operation with the highest hierarchy is executed first and the
one with operators; this can be altered during execution by introducing parenthesis (i.e. a pair of
brackets) in the appropriate place(s) in expression. The content of the bracket will be evaluated first.
This overriding rule is applicable to all other arithmetic operators as well.
Multiplication and division come next to exponentiation in hierarchy with multiplication not
necessarily preceding division.
Addition and subtraction comes last in the hierarchy of arithmetic operators with addition not
necessarily preceding subtraction in execution.
As shown in the table above, multiplication and addition operators are commutative while others are
not.
PEMDAS summarizes both the hierarchy of operators and the order followed in execution.
Example
Write a BASIC expression that will evaluate this mathematical expression.
[(3(x + y)2 + (4Z)2)m/(n-3)]/5
A corresponding BASIC formula for the above algebraic expression is:
(3 *(X + Y) ^2 + (4 *Z) ^ (M I{N – 3)) 15
Please note the difference between the symbols used in mathematics and those used in BASIC for
relational operation.
Logical Operations in BASIC
Logical operations in BASIC involve use of logical operators NOT. AND OR which must yield
logical values TRUE (T) or FALSE (F). The use of operators discussed below.
NOT – this logical operator has the highest precedence or priority over others and affects only one
operand or variable at a time. There could only be two possible combinations and outcomes as shown
in the truth table below:
INPUT OUTPUT FALSE (F) TRUE (T)
TRUE (T) FALSE (F)
NOT operator is an inverter, that is, it gives the reverse of the input upon which it is applied.
AND – this is another logical operator that affects two variables at a time thus providing four possible
combinations as shown below.
T X F F
T X T F
T X F F
As shown in the table above, the result is TRUE only when both input variables are TRUE, else it is
FALSE.
OR – this logical operator with the lowest precedence affects two operators at a time giving rise to
four possible combinations as shown in the table below.
T + F T
F + T T
F + F F
Thus OR operators yields FALSE output only if both input variables are FALSE else it yields TRUE
Examples
Simplify the followings:
NOT T OR NOT F AND T OR T
T OR T AND (F OR T) AND NOT F
NOT T AND NOT F AND NOT AND T OR F
T OR T AND NOT F AND T OR NOT T
T AND F OR NOT T OR F OR (NOT F OR F )
NOT T AND T AND (F OR F ) AND T OR F
Solutions
T NOT T OR NOT F AND T OR T
F OR NOT F AND T OR T
F OR T AND T OR T
F OR T OR T
T OR T
T
T OR T AND (F OR T) AND NOT F
T OR T AND T AND NOT F
T OR T AND T AND T
T OR T AND T
T OR T
T
Do the rest as exercises, bearing in mind that content or bracket supersedes even the NOT operator in
precedence.
String operations (Concatenation)
Numerical operations cannot be performed cannot be performed on strings or string variables,
However, concatenation (i.e. literal combination, one behind the other) of strings and string variables
is allowed on several versions of BASIC.
Example
10 LET AS = “FIVE HUNDRED” 20 LET BS = “THOUSAND”
30 LET C$ = A$ + “ ” + B$ + “ NAIRA” 40 PRINTC$
50 END
The above program will cause FIVE HUNDRED THOUSAND NAITA to be displayed on the screen
as a single string as output.
Functional Operations
Functional operators are inbuilt operators that the programmer can call or invoke from BASIC
functions library to solve some programming related problems instead of writing the code that will
solve such problems himself. Examples of such functions include: COS, SIN, ABS, SORT, TAN,
SUM, LEN, etc for cosine, sine, absolute, square root, tangent, summation, and word- length
functions respectively.
Example:
Given that x = 45o, Develop a BASIC program that generate and display the sine, cosine and Tan of x.
Solution
10 LET X = 45
20 LET P = SINE (X)
30 LET Q = COS (X)
40 LET R = TAN (X)
50 PRINT P, Q, R
60 END
REVIEW QUESTIONS
1. Express the following in accordance with BASIC syntax:
References
Abass, O.A. (2007), Computer and BASIC Programming Concepts. Over to God Press, No.22,
Bonojo street, Ijebu-Ode, Ogun State, Nigeria. ISBN: 978-40296-5-3-3.
French, C.S. (2002), Computer Science. BookPower/ELST Edition first published 2002.
Reprinted 2004, 2005, and 2006. ISBN-13: 978-0-8264-6761-4.
Holmes, B.J. (1989), BASIC Programming- A Complete Course Text. DP Publications Ltd.,
London.