Intro. to Problem Solving With Basic 2
Intro. to Problem Solving With Basic 2
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
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.
i. In BASIC combining alphabets, numbers and the decimal point (a maximum length of 40
characters) may form a variable.
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.
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.
v. No blank space, special characters or any other letter is allowed in the number.
1. Arithmetic expression
2. Relational Expression
3. Logical expression
1. Arithmetic Expression
Arithmetic Operators
/ 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
= 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
Example
LET X = 12
LET B$ = “Clementina”
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.
Example
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
PRINT SUM
END
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
e. PRINT statement
This statement is used to transmit data from the computer memory to the output device.
Examples
PRINT A
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
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
NEXT [variable][,variable...]
Example 1: Write a program using FOR-NEXT state to print any statement five times Solution
FOR I = 1 TO 5
NEXT I
END
Example 2: Write programming using FOR-NEXT statement to display odd numbers from 1 to
20
Solution
PRINT ODD
NEXT ODD
END
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
80 PRINT “================”
50 LET PERI = 2 * (L + B)
80 END