0% found this document useful (0 votes)
18 views29 pages

BASIC Is An Acr-WPS Office

Uploaded by

Victory
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views29 pages

BASIC Is An Acr-WPS Office

Uploaded by

Victory
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

BASIC is an acronym from Beginner's

All-purpose Symbolic Instruction Code. It

is a family of general-purpose, high-level

programming languages whose design

philosophy emphasizes ease of use. The

original Dartmouth BASIC was designed

in 1964 by John George Kemeny and

Thomas Eugene Kurtz at Dartmouth

College in New Hampshire, USA to

provide computer access to non-science

students.

11.3 BASIC CHARACTER

A character is any letter, digit, punctuation,

symbol or any other sign used in the

presentation of information in any

language. BASIC has the following

character set.

Alphabet A,B,C .. ,Z

Numeric 0,1,2,3,.... ,9

Carret (Exponentiation Operation) A

Asterisk (Multiplication Operation) *

Slash (Division Operation)

Minis (Subtraction)

Plus (Addition)
Left Parcnthesis

Right Parenthesis

Comma

Semi colon

Colon

Dollar Symbol (String)

Equality or Assignment Symbol

Decimal Point

Greater than

Less than

Question Mark

Quotation Mark

Back slash (integer division) o/o

Percent Symbol

Exclamation Mark

Ampersand &

Single Quotation Mark (Apostrophe)

11.4 BASIC FUNDAMENTALSs

The two fundamentals of BASIC

programming language are the constant

and variable.

CONSTANT: Constants are static value

the BASIC interpreter uses during

program execution. There are two types of


constant, which are:

() String Constant: A string constant

is a sequence of 0 - 255

alphanumeric characters enclosed

in a double quotation marks. For

example,

"TOLULOPE ODUDIMU'"

TONADPUBLICATION"

(i) Numeric Constant: Numeric

constants can be positive or

negative. For instance, if the

number 50,200 were to be entered

as a constant, it would be typed as

50200. There are five types of

numeric constants: integer, fixed-

point, floating-p0 int,

hexadecimal, and octal.

Constant Description

Integer Whole numbers that do not

contain decimal points.

Fixed-Point Positive or negative real

numbers that contain

decimal points.
Floating-Point Positive or negative

Constants numbers represented in

exponential form (similar

to scientific notation). A

floating-point constant

consists ofan optionally-

signed integer or fixed-

point number (the

mantissa), followed by the

letter E and an optionally-

signed integer (the

exponent). The allowable

range for floating-point

constants is 3.0x10 to

1.7x10. For example:

235.988E-7 = 0.0000235988

2359E6 2359000000

Hexadecimal Hexadecimal numbers with

prefix & H. For example:

&H76&H32F

Octal Octal numbers with the

prefix & O or &. For

example: &0347&1234

Single-Precision and Double-Precision

Form for Numeric Constants


Numeric constants can be integers, single-

precision, or double-precision numbers

Integer constants are stored as whole

numbers only. Single-precision numeric

constants are stored with 7 digits (although

only 6 may be accurate). Double-precision

numeric constants are stored with 17 digite

of precision, and printed with as many as

16 digits.

A single-precision constant is any

numeric constant with either:

i) Seven or fewer digits

(i) Exponential form using E

(i) Atrailing exclamation point (!

A double-precision constant is any

numeric constant with either

) Eight or more digits

(11) Exponential form using D

111) Atrailing number sign (#)

The following are examples of single.

precision and double-precision numeric

constantsS:
Single-Precision Double-Precision

Constants Constants

46.8 3456928 11

-1.09E-06 -1.09432D-06

3489.0 3490.0#

22.5! 7654321.1234

11.5 BASIC VARIABLES

Variables are the names that can change

during the execution of a program, or from

one execution of a program to the next. The

value of a variable may be assigned

specifically, or may be the result of

calculations in your program. If a variable

is assigned no value, BASIC assumes the

variable's value to be zero. There are two

types of variable, which are:

Numeric Variable: A numeric-

variable name maybe any single

letter of the English alphabet (A to

Z), or a single letter followed by

any single decimal digit (0 through

9). It can be assigned only aa

numeriC value either integer, single

precision or double precision.


Valid Invalid | Explanation_

2 must be alphabetic

9K first character must bel

alphabetic second character

Z5 A83 must be numeric

too many characters, only two

A4 AB characters are allowed

Second character must be

numeric

(ii) String Variables: These are

variables that can only be assigned

a character string value. Examples

are

K$="Happy day

Lagos$ BS, where KS and

LagosS are string variables.

Type Declaration Characters

Type declaration characters indicate what a

variable, represents. The followıng type

declaration characters are recognized:

Character Type of Variable

S String variable

Integer variable

Single-precision variable
# Double-precision variable

I1.6 DATA-NAMES

Is also known as variables or identifiers.

In BASIC language, the, minimum

requirement for the composition of data-

names is:

() For Numeric data-names: A

single letter of the alphabet (A-Z)

followed by a single digit (0 -9).

Examples are A, K, T, J0, G5, F9

etc.

(ii) For character string data-name:

A single letter of the alphabet (A-

Z) followed by a dollar symbol $.

Examples are AS, BS, CS etc.

Whenever the programmer is restricted to

using data-name that only contain several

characters, it is advisable to write a

glossary of the data-names used in the

design of the program and the

Corresponding data-name used in coding

tne computer program into BASIC. Many

modern dialects of BASIC will permit

data-names to contain enough characters


for the name to be meaningful. The two

major BASIC dialects are explained

below:

(a) Microsoft: BASIC variable names

may be of any length. If the name is longer

than 40 characters, however, only the first

40 characters are Significant. The

characters allowed in a variable name are

letters and numbers and a decimal point.

The first character must be a letter. A

variable name may not be a reserved word.

A variable name ending with one of the

following characters implies a type

declaration.

String Variable

% Numeric integer (whole number)

variable

Single precision variable (4

bytes) for storing numbers

With

decimal fraction

# Double precision variable (8

bytes) for storing real numbers

whose decimal fraction can be stored

tO a greater accuracyy
(b) BBC/Electron: No restriction onn

the length of the data-name, however, there

must be no spaces in the middle of a data-

name. All data-name must start with a

etter, no punctuation mark must be used in

the name and data-name must not begin

with BASIC reserve words. The character

set up used in the data-name can be either

upper case, lower case or a mixture of both

cases. Integer types can be distinguished

from real numeric types by the use of the

character % after the data-name. String

data types are expressed in the

conventional manner.

11.7 GENERATION OF BASIC

LANGUAGE

(a) First Generation BASIC

language: They supported simple

data types, loop cycles and arrays.

Examples are MSX BASIC and

GW-BASIC. The program below is

an example of first generation

programn.

(b) Second generation: BASICs


introduced a number of features

into the language, primarily related

to structured and procedure-

oriented programming. Usually,

line numbering is omitted from the

language and replaced with labels

for GOTO and procedures to

encourage easier and more flexible

design. Ex a mples are

QuickBASIC and PowerBASIC

Third generation: BASIC

introduceed features to support

object-oriented and event-driven

programming paradigm. Most

built-in procedures and functions

are now represented as methods of

standard objects rather than

operators. Examples areVisual

Visual

Basic, REALbasic, StarOffice

Basic and BlitzMax

11.8 BASIC OPERATORS

Operators perform mathematical or logical

operations on values. The operators


provided by BASIC are divided into four

categories:

(i) Arithmetic

(i) Relational

(ii) Logical

(iv) Functional

(a) Arithmetic Operations:

Operators - BASIC performs addition,

subtraction, multiplication, division and

exponentiation, as well as more

complicated operations (explained in

detail later in the manual). The five

operatorS used in writing most formulas

are

|Symbol

Operator Meaning Example

Addition A+B

Subtraction A-B

Multiplication A*B

Division A/B

(or**) Exponentiation A^B or (A**B)

(Raise A to the B Power)

Priority: In any given mathematical


formula, BASIC performs the arithmetic

operations in the following order:

() Parentheses receive top priority.

Any expression within parentheses

is evaluated before an

(ii) unparenthesized expression.

In the absence of parentheses, the

order or priority is:

() xponeitiation

(b) Mulliplication and Division (of

cqual priorily)

(c) Addition and Subtraction (of equal

priority)

(i) If either equence (G) or (iiy above

docn not clearly designate the order

of priority, then 1he evaluation of

Cxpress0n% proceeds from left to

right

The cxprcsion A+ B-C is evaluated from

Iefl to right as follows:

(1) A+H step

(2) (Result of step 1)-C answer

The expression A/ B " C is also evaluated

from left to right since multiplication and


division arc of cqual priority:

. A/B step

2 (result of step I)*C answer

Parentheses Parentheses may be used to

change the order or priority because

expressions within parenthesis are always

evaluated first. Thus, by enclosing

Cxpressions appropriately, the order of

evaluation can be controlled. Parentheses

may be nested, that is, enclosed by one or

more sets of parentheses. In this case, the

Cxpression within the innermost

parentheses is evaluated first, and then the

next innermost, and so on, until all have

been evaluated. Consider the following

example:A 7*(B^2+4)/X)

The order of priority is

B2 stepl

2. (result of step 1)+4 step 2

3. (result of step 2)/X = step 3

4. (result of step 3)*7= answer

(b) Relational Operatlon: A progran

1ay 1eguire that to values be compared at

AHnE poirit tu discver their relation to one


another. fo accomplish thi, BASIC makes

use of the following relatíonal operators

equal

less than

less than or equal to

greater than

greater than or equal to

>or not equal to

Depending upon the result of the

Comparison, control of program execution

may be directed to another part of the

program. Relational operators are used in

conjunctíon with the IF-THEN statement.

When arithmetic and relational operators

are combined in one expression, the

arithmetic operator is always performed

first.

Logical Operation: Logical

operators performn tests on multiple

relations, bit manipulations, or Boolean

operations, and return a true (not zero) or

false (zero) value to be used in making

decision. In an expression, logical


operations are performed after arithmetic

and relational operations. When a selection

is based upon one or more conditions being

true, it is possible to combine the condition

together using logical operators and the

resultant compound condition will either

be true or false. The following truth tables

illustrate the outcome of combining two

conditions using the logical operators.

There are sis lopical operatom which re

isted in er olpIcdence below

Operator Operation

NOT Lopieal complemen

AND Conjunctivn

OR Disjunction (inclusive OR)|

TOR Taclusive (or Nam

preccdenee is OR)

EQV Tquivalence

TMP Tmplication

() Functional Operators: A function

is used in an expression to calla


predetermined operation to be performed

on an operand. Function calls consist of a

three letter (all capitals) name followed by

an argument in parenthesis. The argument

may be a number, variable, expression, or

another function. The functional operators

and their meaning are given below:

FUNCTIONS T MEANING

TAB(Y) Start at position Y +

SPCY) Leave Y spaces

ABS(Y) Absolute value of Y

SQR(Y) Square root ofY

LOG(Y) Natural logarithm ofY

EXP(Y) Value ofe

ATN(Y) Arctangent of Y

TAN(Y) Tangent ofY

COS(Y) Cosine of Y

SIN(Y) Sin ofY

I1.9 ASIC ANGUA ,E

NTATEMENT

ASIC taloments are the principa

componciuls ol 1/ASIC programs. The

general format of BASIC statements i


XXX Reserved Word Paramcters

whero

XXX iN the line number; RCserved word is

the statcment (inutruction) ype; and

Paramclcrs are the variables uscd in

Conjunction with the slatemcnt type.

ach statement starts with a line number

lollowed by the reserved word statcment

ypo. Spaces have no significancc in

BASIC Janguage statements cxcept in

messagcsor literal strings which are

displayed or printed out.

Statement Line Numbers (Sequencing)

Failure to assign a line number results in

the message:

WHAT

Each linc of the program begins with a line

number of I to 5 digits that serves to

identify the line as a statement, The largest

allowable line number is 99999. A

common programming practice is to

number lines by fives or tens, so that

additional lines may be inserted in a

program without the necessity of

renumbering lines already present.


11.10 PR0GRAM STATEMENTSs

A BASIC program is made up of a number

of BASIC program statements. Each

BASIC program statement begins with a

line number. Line numbers must be whole

numbers in the range of I to 32767. It is

common practice to

Separate line numbers by increments of 10,

SO that additional program statements can

be added with ease later.

Classification of BASIC PROGRAM

STATEMENTS

BASIC Program statement can be

categorized as follows:

(a) Remark statement e.g REM.

6 Information Entry Statement e.g.

INPUT. READ-DATA.

(c) Data manipulation Statement e.g

LET

(d) Output Statement e.g. PRINT

(e) Loop Statement e.gFOR

NEXT, WHILE..... NEXT.

Control Statement e.g. GOTO; IF-


THEN, GOSUB.

(e) Program Termination Statement

e.g. END, STOP.

11.10.1 The REMark Statement

The REM statement is a non executable

statement used to insert comments into the

source program. A, programmer may

include comments in his/her program with

the REM statement. BASIC compey

ignores REM lines,, but they are intended to

assist a person trying to understand how

our program works. They are also helpful

for improving the visual appeal of your

program. The exclamation mark (!) may

also be used to add comments. The

exclamation mark (!) can be used in the

same way as a REM. The exclamation

mark (!) can also be used to place a

comment on the same line as a BASIC

statement.

REMARK Statement Formn

Line Number Statement Parameters

XXX REM comments

11.10.2 Information Entry Statements


(a) INPUT statement: The INPUT

statement allows a value, numeric or

character string, to be typed at the

keyboard of a computer and stored in the

computer's memory at the data-name

specified.

Format: INPUTdata-name

Example: INPUTX will enable a

numeric value for X to be entered at the

keyboard and stored in the memory under

the data-name X

INPUTAS will enable a string

value for AS to be entered at a keyboard

and stored in the memory under the data.

name A$

When the computer executes an INPUT

statement, it will output on the screen of a

terminal a prompt, either ? or! depending

upon the system used, and wait for the user

to enter a value at the keyboard.

For example: INPUT "WHAT IS YOUR

NAME", N$
Will output to the terminal the prompt

WHAT IS YOUR NAME? and enable a

string value for NS to be entered at the

keyboard and stored in the memory under

the data-name N$.

(b) DATA, READ Statements: The

DATA and READ statements are always

used together. The DATA statement is used

to set up a list of numeric or string values.

These values are accessed by the READ

statement which assigns those values to

variables in a program.

DATA Statement Format: The DATA

statement sets up a list of values to be used

by the READ statement.

DATAStatement F'ormat

Line Number Parametern Stale ment

XXX DATA Values

where values

where values are numcrie and/or string

entries separatca by commas, The DATA

statement serves as a source of input


ariables for the program. "The variables

are accessed for proccssing by the READ

statement.

There may be any number of DATA

statements in a program. They are not

executed and may be placed anywhere

within the program. However, BASIC

treats them all together as a single list.

Both string data and numeric data may bc

intermixcd in a single DATA statement.

String data in a DATA list must always be

enclosed by quotation marks.

For example, the three DATA statements

10 DATAA

20 DATAB

30 DATAC

áre equivalent to the following statement:

50 DATA 2, 3,4

READ Statement Format: The READ

statement accesses variables defined by the

DATA statement and assigns those values

to variables in a program.
READ Statement Format

Line Number Statement Parameters

XXX READ variables

where variables are names corresponding

to values contained in DATA statements

A READ statement may contain numeric

and string variable names intermixed, to

Correspond to numeric and alphanumeric

Values in the DAIA list. Iowever, sinee

Valucs are takcn from the DATA Ist in

NCquential order, you must ensure that the

valucs in the li:t are in the coTect eunce

to correspond to thc viariable namer of the

ame format (numeric or sirng)

A READ statement may have more or

fewer variables than valucs in any one

DATA statement. The READ statement

Causes BASIC o scarch all available

DATA statements in the order of their linc

numbers until values arc found for each

Variable in the READ. A second READ

statement will begin reading values whdCre

the first one stopped.

The READ statement is always used in

combination with the DATA statcment.


Example 11

10 READ A, B,C

20 DATA 5,8,3

Example 2

10 READ NAMEIS, AGEI

20 READ NAME2S, AGE2

30 READNAME3$

40 READAGE3

50 PRINT NAME1S; AGE1,

NAME2S; AGE2, NAME3S;

AGE3

60 PRINT "Total age is"; AGE1 +

AGE2+AGE3

70 DATA "BOB", 21, "MARY"

80 DATA 18

90 DATA BILL

100 DATA45

110 END

RUN [Return]

Example 3: The following program uses

DATA statements to supply both a variable

number of scores and variable score values

to an average calculation routine.

10 PRINT 'NUMBER
20 PRIN T OF

SCORES','AVERAGE'

30 PRINT

40 READNI

50 FOR 1-1 TONI

60 READN

70 IFN=OGOTO 210

80 LETS=00

90 FOR K=1 TON

100 READT

110 LETS=S+T

120 NEXTK

130 PRINTN, S, T

140 NEXTI

150 DATA5

160 DATA3, 82, 88,97

170 DATA5,66, 78,71,82,75

180 DATA4,82, 86, 100,91

190 DATA4,72, 82, 73, 82

200 DATA61, 73,67, 80, 84,79

210 END

11.10.3 Output Statement

PRINT statement

The PRINT statement is used to perform

calculations and display results. It is also


used to display alphanumeric (string)

messages.

PRINT Statement Format

Line number Statement Parameter

XXX PRINT expressions

Where expressions may be numbers,

variables, strings or .arithmetic

expressions, separated by commas or

semicolons.

When used without an expression, a blank

line will be output on the terminal. In

BASIC, a line is formatted into five fixed

zones (called print zones) of 14 columns

each. If the expressions in a PRINT

PRINT

statement are separated by commas, each

will begin at a 14-column interval. That is

the first expression will be displayed

starting at the first position, the second at

the fifteenth position, and so forth. If more

than five variables are involved, the

display will automatically continue at the

beginning of the next line. If this format is


not desired, you may separate expressions

with the semicolon G), causing the text or

data to be output immediately after the last

character printed.

If the last expression in a PRINT statement

is followed by a comma or semicolon, the

next display called for by the program will

begin on the same line (if there is room).

This also applies to the question mark

displayed by the INPUT statement

For example:

PRINT "GOD BLESS YOU", NS

Will output to a screen:

GOD BLESS YOUTOLU

Ifthe value of NS is TOLU

PRINT without a literal and/or data-name

will output a blank line.

11.10.4 Assignment Statement

LETSTATEMENT

Values are assigned to numeric and string

variables with the LET statement. The

general form of the LET statement is:

Line Number Statement Parameter


XXX LET Vexpression

You might also like