INTRODUCTION TO PROBLEM SOLVING WITH BASIC CONT'D
BASIC PROGRAMMING LANGUAGE
1. Origin of and Features of BASIC
BASIC stands for Beginner’s All-purpose Symbolic Instruction Code. It was developed in 1960
by John Kemeny and Thomas Kurtz to teach students at Dartmouth College. It has undergone a
series of historical development, which has resulted in several forms of the language.
BASIC is now in form of VB.NET (Visual Basic.Net). The majority of BASIC languages use
program translators called interpreters to allow the computer to understand and obey the BASIC
statements in the computer program. Examples of such interpreters are:
BASICA
GwBASIC
Turbo BASIC
Quick BASIC
2. BASIC Character Set
The characters used in BASIC language include:
i. Alphabetic Characters: BASIC alphabetic characters consist of letters (A to Z).
ii. Numeric Characters: Numbers from 0 to 9 are used in BASIC language
iii. Special Characters: Special characters are characters that are not letters or numbers. They
include punctuation marks, accent marks, ASCII control characters, formatting characters.
Examples + % ^ # = ( ) etc
3. BASIC variable
A variable is a quantity that changes during the execution of a program. It can also be defined as
a name that is used to represent some storage location.
Types of Variables
1. Numeric Variables: These are used to store numeric values such as 23, 98, 1.44 etc.
Examples of numeric variables are; N, Y, P, SUM, AVERAGE, etc
2. String Variables: These are used to store alphabetic and alpha-numeric values. A string
variable name is always written with a dollar sign ($) as the last character. E.g. Name$, AVG$,
X$, etc.
Rules for coding variable
i. In BASIC combining alphabets, numbers and the decimal point (a maximum length of 40
characters) may form a variable.
ii. No reserve word can be used as a variable name.
iii. Special characters cannot be used for naming a variable. iv. A string variable corresponds to
string data whereas a numeric variable corresponds to numeric data,
iv. In a program, each variable is referred to throughout the program by its name.
4. Constants
A constant is data that remains the same as the program runs (executes). Constants are values
stored or assigned to variables.
Types of Constants in BASIC
BASIC allows two constants which are;
i. Numeric constant: Numeric constant in BASIC is any signed or unsigned number.
ii. Alpha-Numeric or string constant: It consists of the combination of letters, digits, and other
symbols that are treated in a manner completely analogous to a numeric constant. They are
enclosed within inverted commas.
Rules for numeric constants
i. A number can have a maximum of 8 digits
ii. No comma is allowed
iii. A decimal point can appear anywhere
iv. If the value is quite larger it is expressed in exponent form
v. No blank space, special characters or any other letter is allowed in the number.
5. BASIC Expressions and Operators
In programming, an expression can be defined as the combination of operands and operators.
Operands are the data items involved in an expression. Operators determine the action to be
carried out on the operand in the expression. For instance in the statement: LET C = A + B, A
and B are the operands while “+” is the operator.
There are three major types of expression in BASIC. They are:
1. Arithmetic expression
2. Relational Expression
3. Logical expression
1. Arithmetic Expression
BASIC arithmetic expression is used to represent mathematical formulae in BASIC
programming. Below is a list of BASIC arithmetic operators:
Arithmetic Operators
Symbol Name Function
^ Upper caret Exponentiation
/ Slash Division
* Asterisk Multiplication
+ Plus Addition
- Minus Subtraction
Every arithmetic expression must appear on a single line. There is no superscript in BASIC as
we find in algebra.
2. Relational Expression
Relational Expression is used for the comparison of two or more data items. BASIC relational
operators are listed below:
Symbol Name
< Less than
> Greater than
= Equal to
<> Not Equal to
<= Less than or equal to
>= Greater than or equal to
3. Logical Expression
Logical expression involve is an expression involving two or more relational repression joined
by a logical expression. BASIC logical operators are:
AND
NOT
OR
6. BASIC Statements
a. LET Statement
The let statement is used to assign a numeric or string value to a variable.
Syntax
LET [variable] = [constant] for numeric value
LET [variable]$ = [“value”] for string value
Example
LET X = 12
LET B$ = “Clementina”
LET AREA = L*B
b. INPUT Statement
The INPUT statement is used to enter data into the computer with a user prompt or a group of
variables during program execution.
Syntax for numeric value
INPUT “[prompt]”; [variable]
Syntax for string value
INPUT “[prompt]”; [variable$]
Example
INPUT “type in the number”; A
INPUT “Type in your name”; N$
c. READ-DATA statement
READ and Data are two statements concerned with each other which are used to put data in a
line of the program and to read the data when it is needed.
Example:
READ A, B, C
DATA 5, 6, 7
LET SUM = A+B+C
PRINT SUM
END
d. REM (Remark) Statement
The REM statement is used to insert comments or remarks into a BASIC program. The use of
remark statements improves the readability of the program. REM is a non-executable statement.
Syntax
REM [remark]
Example
REM program to add six numbers
e. PRINT statement
This statement is used to transmit data from the computer memory to the output device.
Examples
PRINT A
PRINT “I Like Writing Program”
f. Program Terminators (END and STOP Statements)
The STOP statement is used to terminate the execution of a program at any point in the program.
The END statement indicates the actual end of a program. The STOP statement may appear
many times and anywhere, whereas an END statement can only appear at the end of a program
and only once.
Example
REM END statement
PRINT “Good morning”
END
g. FOR – NEXT
Looping is used to have the computer do repetitive tasks in a fraction of the time that would
otherwise be required. The most common type of loop used in QBASIC programming is the
FOR...NEXT and WHILE WEND loop that repeats a series of instructions a specified number of
times.
Syntax
FOR variable=x TO y [STEP z]
NEXT [variable][,variable...]
x,y, and z are numeric expressions.
STEP z specifies the counter increment for each loop.
Example 1: Write a program using FOR-NEXT state to print any statement five times Solution
FOR I = 1 TO 5
PRINT “the dullest pencil is better than the sharpest memory”
NEXT I
END
Example 2: Write programming using FOR-NEXT statement to display odd numbers from 1 to
20
Solution
REM program to print odd numbers from 1 to 20
PRINT “odd numbers from 1 to 20 are”
FOR ODD =1 TO 20 STEP 2
PRINT ODD
NEXT ODD
END
7. Simple Basic Programs
Example 3: Program to find the sum and difference between two number
10 REM this program accepts two numbers and finds their sum and difference
20 INPUT “Type the first number and press ENTER”; NUM1
30 INPUT “Type the second number and press ENTER”; NUM2
40 LET SUM = NUM1 + NUM2
50 LET DIFF = NUM1 – NUM2
60 PRINT “first number is “; NUM1
70 PRINT “second number is “; NUM2
80 PRINT “================”
90 PRINT NUM1; “+”; NUM1 “=” ; SUM
100 PRINT NUM1; “-“ ; NUM2 “=” DIFF
110 END
Example 4: a program to calculate the area and perimeter of a rectangle
10 REM program to find the area and perimeter of a rectangle
20 INPUT “Type the length of the rectangle”; L
30 INPUT “Type the in the breadth of the rectangle”; B
40 LET AREA = L*B
50 LET PERI = 2 * (L + B)
60 PRINT “The area of the rectangle is “ ; AREA
70 PRINT “The perimeter of the rectangle is” ; PERI
80 END