: BASIC Programming language
The BASIC programming language was developed in 1964 by John G. Kemeny and Thomas
Kurtz at Dartmouth College. BASIC is an acronym that stands for “Beginner‟s All-Purpose
Symbolic Instruction Code”
Basic Character Set
A character set is simply a list of letters, numbers and symbols that provide one with the
characters used in a particular language. BASIC as a language has its own character set, they
include the following:
Alphabets characters (A to Z or a to z)
Numeric character (0, 1 to 9) including hexadecimal characters.
Special characters (such as $,#,!, ^ etc.) that perform special functions in BASIC
Arithmetic operators
CHARACTER NAME USE
* Asterisk For multiplication, e.g. A*B or (3*5)
- Minus For subtraction, e.g. M-N or (4-1)
+ Plus For addition, e.g. K+N or (1+6)
/ Forward slash For real division, e.g. A/B or (7/3)=2.33
\ Bask slash For integer division, e.g. P\G or (7\3)=2
^ Caret For exponentiation, e.g. A^B or (7^3)
Relational (Comparison) Operator
CHARACTER NAME USE
= Equal to A=B
> Greater than A>B
< Less than A<B
>= Greater than or Equal to A >=B
<= Less than or Equal to A <= B
Data types
Data type is a description of the set of values and the basic set of operation that can be applied to
values of the type.
i) Integers: a positive or negative number without decimal. It has a range of values from -32,768
to 32,767. Each value is stored using 2 bytes of memory (storage) space.
ii) Real numbers: numbers with fractional parts or with a decimal point. It is stored using 4 bytes
of memory space.
iii) Boolean: consist of only two values; “YES and NO” or “True or False” or 1 or 0.
iv) String: a sequence of characters in double quote. For example, “Computer Studies” is a string
value with 16 characters. Each character is stored using 8 bits (one byte) in the ASCII character
set and two byte in the UNICODE character set. Alphabet is represented in ASCII.
Keyword
Keywords are words that have special meaning and function in BASIC. Such words must be
used strictly according to their functions, otherwise the computer will respond with error
message.
Here are some of the BASIC keywords and their uses
KEYWORDS USE EXAMPLE
REM Make comment about an instruction or about the 10 REM Program to add
whole program two numbers
INPUT Used to ask the user to supply the data to be 5 INPUT A,B,C
processed while the program is executing
PRINT Used to display the output of operation on the PRINT “The values”,
screen A,B
LET Used to assign a value to a variable 3 LET A=5
READ Used to tell the computer that the data to be 10 READ A,B
processed is supplied within the program
statements. Used together with DATA keyword
DATA Used to show the computer the data it is asked to 10 DATA 4,7
read in the READ statement. Used along with
READ keyword.
END To end the program 5 END
Variables and Constant
A variable is an identifier or a name of a memory location where data (values) can be placed or
stored. Because the value placed in a memory location can be changed at any time, we call such
memory location “a variable”. However, when the value of a memory location is not to be
changed, we refer to such memory location as “constant”.
Declaration of Variables and Constants
When you are to supply data to the computer for processing, you are required to state the data
type. This help the computer to interpret it and an appropriate storage space is reserved for the
data. To achieve this, the keyword DIMENSION (or DIM) is used to specify the data type. For
example, variable Name and Pie are to hold “character and real” data values respectively. This
can be written in BASIC as follows:
1. DIM Char Name, INT Age, REAL Height
2. DIM Name AS Char, Age AS INT, Height AS Real
3. DIM Name$, Age%, Height!
Rules for naming Variable
1. Every variable must begin with an English alphabet (A to Z or a to z).
2. The name must not be more than 40 characters in length.
3. Names can be alphanumeric (combinations of alphabet and numbers
4. Name must not be any keyword
5. Do not include a blank space in the name.
Basic Expression
A BASIC expression is formed when two or more characters, strings, variables or constants are
combined with arithmetic, relational or logical operators
Examples include:
1. F2 + 7 (add 7 to the value in the variable F2)
2. A+ B (add the value in A to the value in B)
Arithmetic expression and their BASIC expression
Algebraic expression BASIC expression
A÷B A/B
2
2b +5c 2*(b^2)+5*c
(-b+sqr(b^2 – 4*a*c))/2*a
2
-b±√b -4ac/(2a)
Arithmetic operation precedence
The rule that guides the order in which operation must be performed in an expression is known
as “precedence” rule.
THE ORDER SYMBOL NAME
1 () Bracket
2 ^ Exponentiation
3 - Negation
4 *or / Multiplication
5 \ Integer
6 MOD Modulo (remainder)
7 + or - Addition or subtraction
BASIC statements
A BASIC statement is any valid instruction given to the computer for processing of data. BASIC
statement may be an expression, an assignment, a reserved word (keyword) with or without
arguments or their logical combinations
1. Declaration statement: Every variable in a program must be declared before it can be used.
A variable must be declared with appropriate data type. Example of declaration statements are
a. DIM Age AS Integer
b. DIM Name AS Character
2. Assignment statement: This statement causes a computer to store a value in a variable. The
statement has two sides separated by the equality sign (=).
a. Name = “Computer Studies”
b, Age = 16
3. INPUT statement: This statement is used to ask the user to supply the data to be processed
while the program is executing.
a. INPUT Name$, Age% (This statement is not interactive)
b. INPUT “Enter the Name and Age”, Name, Age (This statement is interactive)
4. READ and DATA statement: The READ statement is used to supply data into the
program, but unlike the INPUT statement, the programmer supplies the data inside the program
codes using the DATA keyword.
a. READ “Enter Name and Age”, Name$, Age%
b. DATA “Jide Babs”, 20
5. PRINT statement: This statement is used to show the result of data processed.
A simple QBASIC program
1 REM This program calculates the area of any rectangle and print the length, width, and area.
2 „To calculate the Area of a Rectangle we must know its length and width
3 Length = 10
4 Width = 6
5 Area = length * breadth
6 PRINT “Area is “, Area
7 END
Another program solving Quadratic equation is shown below.
1. REM program to solve a quadratic equation
2. INPUT “Enter coefficients a, b, c “; a,b,c
3. Discrim = b^2 – 4*a*c
4. If Discrim > 0 then
5. Dummy = sqr(Discrim)
6. PRINT “There are 2 distinct roots”
7. PRINT “first root (X1) = “; (-b + dummy)/(2*a)
8. PRINT “Second root (X2) = ”; (-b –dummy)/ (2*a)
9. ELSEIF Discrim = 0 THEN
10. PRINT “There is only 1 root, the 2 roots coincide”
11. PRINT “double root = ”; - b / (2*a)
12. ELSE
13. PRINT “The roots are imaginary”
14. Dummy = sqr (- discrim)
15. PRINT “Real part = “; - b /(2*a)
16. PRINT “Imaginary part = ”; dummy/(2*a)
17. END IF
18. END