0% found this document useful (0 votes)
20 views20 pages

CSC 113 Basic (Programming)

The document defines key concepts related to programming, including programs, programmers, and programming languages, with a focus on BASIC as a beginner-friendly language. It discusses the structure, merits, and demerits of BASIC, along with its various versions and programming rules. Additionally, it covers computer hardware components, machine language, arithmetic operations, and provides examples of BASIC programming instructions and constants.

Uploaded by

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

CSC 113 Basic (Programming)

The document defines key concepts related to programming, including programs, programmers, and programming languages, with a focus on BASIC as a beginner-friendly language. It discusses the structure, merits, and demerits of BASIC, along with its various versions and programming rules. Additionally, it covers computer hardware components, machine language, arithmetic operations, and provides examples of BASIC programming instructions and constants.

Uploaded by

abudutobi103
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DEFINITION OF CONCEPTS

PROGRAM, PROGRAMMER, AND PROGRAMMING LANGUAGE


A program is a set of instructions written in a language (such as BASIC) understandable by the
computer to perform a particular function on the computer. A well written program could be parceled
well to form an application package customized for solving specific type of problem on the computer
system.
A computer programmer is computer scientist (a professional) skilled in using constructs of
programming languages to develop executable and acceptable computer programs. A software
developer is a programmer. Programmers often work hand in hand with system analysts on large
projects.
Programming languages are artificial notational languages created or developed to be used in
preparing coded instructions on the computer for later execution by the computer. They are usually
composed of series of usage rules (syntax) that determine the meaning (semantics) of expressions
written in the language. Each programming language comes handy with its own translator i.e
interpreter or compiler as the case may be.
Programming is the art of developing computer programs with the aid of selected programming
language by a computer programmer. It is a special skill whose quality is tested by the quality of the
resulting program or software. In programming, programming stages must be properly followed, i.e
from problem definition to maintenance and review

BASIC PROGRAMMING: NATURE AND CHARACTERISTICS


BASIC is an acronym for beginners’ all-purpose symbolic instruction code. BASIC is the name of a
high level programming language developed at Dartmouth College, New Hampshire USA under the
auspices of professors John G Kemeny and Thomas E. Kutz during their academic years- 1963-64.
The language was developed. To teach beginners the basic construct s of programming theory and as
much, one of the easiest programming languages to learn.
As a high level programming language program code written in written in BASIC must be translated to
its equivalence in machine code before it can be executed on the computer. A BASIC interpreter (i.e.
translator software) is needed for this purpose. We call the program source code before it translated
by the interpreter while the translated version, ready for execution, is called the object code.
However, interpreter is not the only translator used in programming. Compilers are used by several other
high-level programming languages such as C++, Pascal, etc while assemblers are used by assembly
languages. Java has both interpreter and complier
Translates the sources code line by line and statement by statement
Does not generates an intermediates object code from the source code and
Translates and executes the sources code simultaneously

Merits of BASIC Programming Language


 BASIC is the simplest programming language
 BASIC is friendly, easy to teach and to learn compared to other HLL languages
 It is flexible and easy to code or modify existing code
 It has simplified grammar (syntax) and relatively fewer number of statements
 It has a multipurpose language suitable for interactive environments
 Its interpreter is small and portable
 It can be programmed on a terminal
 It is very common and readily available on many micro and mini computers
 It is the most widely used time sharing language in U.S.A and Canada
 It comes automatically with every new microcomputer
Demerits of BASIC Programming Language
It has slow execution pace, as translation is combined with execution.
Many versions of BASIC are unstructured because of statements such as GOTO.
Limited portability as different versions requires different interpreter for source code translation.
Different Versions of BASIC Programming Language
More than 50 versions (“dialect”) of BASIC exist today as a result of variations in hardware systems.
All these variations are expected to conform to the universally accepted standard set out by American
National Standard Institute (ANSI). Some of these versions are: ANSI BASIC, VISUAL BASIC, GW
BASIC, QUICK BASIC, TURBO BASIC, BASICA, MFBASIC, CASSETTE BASIC, APPLE
BASIC etc.
BASIC Character Set
Alphabets Letters: A-Z; a-z
Digits Numbers: 0-9
Special characters: ?, (,), +, =, *, &, ”, %, $, n, !, >, <, f, \, ;, :, @, ’, etc

Structure of Basic Program


 Each instruction in a BASIC program is written as a separate statement. A complete program is
thus made up of sequence of basic statements. BASIC statements are executed in the order of
appearance unless a deliberate jump to instruction (i.e. an instruction to transfer control) is given.
Below are some general rules applicable to several versions of BASIC with the exception of
more recent versions like Visual BASIC
 Every statement must appear on a separate line
 A statement must not exceed one line in length
 Each statement must be preceded by a line number (or statement number or label) which must
be an integer.
 Two statements cannot have the same line number.
 Blank spaces can be inserted wherever desired to improved readability of the program.
 Each line number must be followed by a BASIC keywords which indicates the type of
instruction that is to be carried out in the line.
 Successive statements must have increasing statement numbers.
 Although these rules are relaxed in some versions of BASIC, programs written with adherence to
these rules remain valid.

INTRODUCTION
Basic was created in 1964 by J.G. Kemeny and T.E. Kurtz at Dartmonth College USA. The language
was originally designed as a simplified version of FORTRAN for use in Teaching programming. From
the simple beginnings, the language has grown to become a very popular multi-purpose language
available on a wide variety of machines, in particular microcomputers and minicomputers. This course is
therefore devoted to BASIS because of its usefulness

COMPUTERS HARDWARE AND SOFTWARE


Computers, what are they?
Computers are automatic machines that can accept data, store vast amounts of information and perform
arithmetic at high speeds to solve complex machines
Organization of a Computer
The computer may be thought of as a system composed of five components. The components and their
functions are as follows:
1. The Input Unit: This unit feeds information from the outside world to the computer. Input units
are capable of reading information recorded on such different mediums as punched cards or
magnetic tape or from terminals. The information read from these devices is placed into
appropriate memory locations.
2. The Memory Unit: The memory unit stores information. It holds the sequence of instructions
necessary to solve particular problem and any additional data required. The memory is divided
into locations that each have an address (are addressable). Instructions are stored in these cells.
3. The Control Unit: The control unit fetches the instructions and data from memory and executes
the instructions one at a time with logical unit. All of the other components operate as directed by
the control unit.
4. The Arithmetic/Logical Unit: The arithmetic/ logical unit consists of the electronic circuitry
that performs arithmetic operations such as comparison of numbers.
5. The Output Unit: The output unit transfers or copies the contents of certain memory locations
onto some external medium such as punched cards, punched paper tape, magnetic tape, a printed
page produced by a teletype or line printer, or a cathode ray tube (CRT) screen for visual display.
The memory, control and arithmetic/ logical units are collectively called the central processing
unit (CPU).

Computers Programs
The set of instructors that control the computer is called a program. A program can be executed by the
computer only when it is stored in the computer’s memory and is in machine language code. Machine
language is the only language the computer can understand. It is a language in which arithmetic / logical
operations are represented by machine – recognizable numeric codes and in which memory locations
containing data and program instructions are represented by numeric addresses.
Machine language vary from one computer manufacturer to another and as such they are machine –
dependent other types of languages, called high – level languages, have been developed to allow the user
to formulate problems in a much more convenient and efficient manner. High-level languages are
machine-independent; programs written in such languages can be processed on any type of computer.
However, high-level languages must be ultimately translated into machine language before they can be
executed by the computer. Special programs called language translators or compilers have, therefore,
been developed to provide this translation service. BASIC (Beginner’s All – purpose Symbolic
Instruction Code) is an example of a high – level language.

èArithmetic Operations
In BASIC programs, arithmetic operations are indicated by the symbols:
+ addition
- subtraction
x multiplication
** or ↑exponentiation
Types of Instruction
We consider a simple model computer that operates conceptually as its real-life counter- parts. The input
is a terminal where numbers have been entered. (typed). The memory unit consists of a group of
sequentially numbered “pigeonholes”. Locations can be referred to either by name or by address. The
arithmetic/logical unit is represented by a desk calculator capable of performing arithmetic and logical
operations. The role of the control unit played by a human operator who can fetch instructions from the
memory one at a time and execute them. The type of instructions the operator is capable of executing
includes:
1. Input: Read a value from the input medium and store that value in a specified memory location
e.g. INPUT F will cause one value on the input medium to be read and stored in a memory
location called F.
2. Conditional branding: Perform a comparison test between two values using the logical unit and
brands to a specified memory location if the test condition is met. If the condition is not met, no
transfer occurs, and the following instruction is taken from the next memory location. For
example, the instruction, IF F < 50
Go To 20
Cause the control to fist fetch F from memory and then request the logical unit to compare the
value with 50. If the logical unit reports that the value F is less than 50, the control unit fetched
the next instruction is taken from the next sequentially numbered location. If F is not less than
50, it processes the next instruction.
3. Calculations: Perform calculations using the arithmetic unit and place results in desired memory
location. For example, the instruction Let M = 5 * F will cause the control unit to fetch the value
contained in the memory location F and activate the arithmetic unit to multiply that value by 5.
The final result is then stored in memory location P (product).
4. Unconditional branding: Take the next instruction from a specified memory location. For
example, the instruction, Go to 700 cause the control units to fetch the next instruction from
location 70 rather than from the next sequentially numbered location. Unconditional branding,
then simply means transfer directly to a particular instruction in memory.
5. Output: Copy a value fro a memory location onto the output medium. For example, the
instruction PRINT F, P will cause the contents of F and P to be written out on the output pad.
6. Termination: Cease execution of instructions for this program and wait for a new program to be
loaded into memory. For example, the instruction, END causes the program to terminate. No
more instructions in this program are executed.
Sample Program
Here we wish to calculate and write out an amount of pay owed an employee who is paid =N=50 per
hour with time and =N=100 for all hours in excess of 40.
1. INPUT F
2. IF F > 40 GO TO 5
3. Let P = 50 * F
4. GO TO 6
5. LET P = 2000 + 100 * (F – 40)
6. PRINT P,F
7. END

èBASIC STATEMENTS
LET, PRINT, TERMINATION, SYSTEM COMMANDS
Elements of BASIC Language
BASIC is a set of instructions telling the computer what actions (operations) to carry out to solve a
particular problem. There are three types of BASIC operations that the computer can carry out. These
are:
1. Arithmetic operations (add, subtract, multiply, divide, raise to the power)
2. Input/ Output operations (entering data and printing results).
3. Comparison (determining whether one value is less than, equal to, or greater than another value).
BASIC instruction is made up of different elements (BASIC keywords, constants, variables, expressions,
arithmetic operators, relational operators etc) which, when combined together under a proper
grammatical structure, yielded a valid BASIC instruction.
We consider the following problem; the investment T is given by the formula
T = P (1 + T)N
Where I is the interest rate, N is the number of years, and P is the Principal. Mr. Ojo wants to deposit
=N=1,500 at a savings institution. He considers a bank and a credit union. The credit union requires a
non-refundable membership fee of =N=15. Deposits earn 12.25 percent at the credit union and 11.75
percent in the bank. Mr. Ojo will need the money in 2 ½ years. We wish to determine which institution
that Mr. Ojo should use.
The program is as follows:
10 LET p = 1500
15 LET T1 = .1225 20
20 LET T2 = .11 75
25 LET N = 2.5
30 LET T1 = P * (1 + T1) 1N – 15
35 LET T2 = p * (1 + T2) 1 N
40 PRINT “CREDIT AMOUNT”, T1
50 PRINT “BANK AMOUNT”, T2
55 END
The output
CREDIT AMOUNT 1987.43 BANK AMOUNT 1980.20
So Mr. Ojo will be advised to use credit union
Constants
A constant is a quantity whose value is fixed and explicitly stated constants may either be numeric or
alphanumeric.
1. Numeric Constants
Numeric constants are positive or negative numbers; they can be added, subtracted, multiplied, divided
and raised to a pointer by a computer. A constant is made up of any of the digits through a and may be
preceded by the + or – symbol.
Examples of valid constants
300 - 2.31 62504. + 0.3 -14
Imbedded blanks (blanks between first and last digit) do not affect the value of the constant.
Example 6 32 = 632 = 6 3 2
Examples of invalid constant
1, 634, 123 No comma is allowed
23.24. Only one decimal point is allowed
$40.42 Character $ is invalid
127 – 423 Character – is invalid
2. Alphanumeric Constants
Alphanumeric constants are generally words, messages, captions, column headings titles, names,
addresses etc. that are endorsed within quotation marks.
Examples of valid alphanumeric constants
“1425 ADEYI ST”
“1425 ADEYI ST”
“IT IS HER’S”
Note: The maximum number of characters allowed in an alphanumeric string varies from one system to
another.
Constants may be used in BASIC as follows:
 LET T1 = P + (1 + T1) ↑ N – 15
 PRINT -10.36, + 4.6 + 5.93, .005
 LET N$ = “CHARLES”
 PRINT “$”, A
3. Variables
Unlike a constant, a variable may assume different values. They are two types of variable.
1. Variables that contain numeric data can be processed arithmetically (added, subtracted etc)
2. Variables that contain alphanumeric data cannot be processed arithmetically.
The specifications for standard BASIC variable names are as follows:
Numeric Variables
A single letter of the alphabet such as A, B, Z, P or a single letter followed by one digit (0 - 9) such as
B4, Z2, D0.
Alphanumeric Variables
Same as numeric variables except that a $ is appended to the variable name,
Example: A$, B$, Z5$
The following are invalid variables as indicated I the notes
2B Does not start with letter
KRB Too many letters
B% Invalid character %
C + 2 Invalid character + BA$ B2$ would be valid
“HELLO” Is a constant

Expressions
An expression may be a constant, a variable or any combination of constants and/or variables linked by
the five arithmetic operators shown below. Note that no two arithmetic operators may be typed side by
side. Parenthesis may be included to denote the order of computations. The allowable arithmetic
operators are:
+ Addition
- Subtraction
* Multiplication
/ Division
↑or ** or ^ Exponentiation (raising to a power)
Valid Expressions
Algebraic Expressions BASIC Expressions
+ 3.5 3.5
-c -c
a.b -30 A*B -30
a/b .c (A/B) * c
Ax2 + bx + c A*X↑2+B*x+c
(a.b)2 (A*B) ↑ 2
(-c + 1.4)d (-c + 1.4) * D
ᵆ X ↑ .5
4
(ᵄ − ᵄ )3 ( (A –B) ↑ 3) ^ .25
ᵃᵅᵆᵆ − ᵆᵄᵅᵆᵄᵅᵅ (c – s) / Y
ᵄᵅᵄᵅᵆ
Bonus + hours x rate B+H*R
1/r1 + 1/r2 + 1/r3 1/R1 + 1/R2 + 1/R3
Precedence in Expressions
Operations within expression are performed according to the following rules of precedence.
Operation Precedence
( ) high precedence
↑ or **
* or /
+ or - low precedence
Operations with high precedence are performed before operations with lower precedence. The operations
addition/subtraction, multiplication/division are performed in order from left to right according to the
rule of precedence. Exponentiation is performed in order from right to left. If parenthesis is nested, the
operation in the innermost set of parenthesis is performed first.
Consider the expression
3 + (2 * 2 ↑ 3 / 4 + 1) – 2 / 3
8
16 4
5 .6666 …
8
7.3333…
1. A – B + C B is subtracted from A, and the result is added to C
3 – 2 + 5 (3-2) + 5 = 1 + 5 = 6
2. A + B * C Since multiplication has priority, B * C is computed; the result is then added to A
giving A = (B * C).
3 + 2 * 3 3 + (2 * 3) = 3 + 6 = 9
3. A/B * C Since multiplication and division have the same priority B is first divided into (A/B),
and the result of the division is multiplied by C
9./ 4. * 2 (9./ 4.) * 2 = 2.25 * 2 = 4.50
4. A/B/C First A/B is performed, and the result is then divided by C 8/4/2 8/4 ÷ 2 = 2 ÷ 2 = 1
5. (A + B) / C * D = ( (A + B) / C ) * D
So (3 + 6) / 3 * 6 = ( (3 + 6) / 3 ) * 6 = 3 * 6 = 18
6. A + B * C ↑ 2 = A + (B * (C ↑ 2) )
3 + 3 * 2 ↑ 2 = 3 + (3 * 22) = 3 + (3 * 4) = 3 + 12 = 15
7. A ↑B ↑C = (A ↑ B) ↑ C 3 ↑ 2 ↑ 3 = (3 ↑ 2) ↑ 3 = (32)3 = 93 = 729
The Replacement Statement LET
A replacement statement evaluates an expression and stores the value of the expression in a memory
location identified by a variable name. The general form of a replacement statement is

In most BASIC systems, the key word LET is optional and need not be specified by the user. When the
particular statement is carried out (RUN) the value of the expression is first computed and the result is
placed (stored) in the variable (memory location0 specified on the left – hand side of the equal sign.
When we write 10 X = 2 * X + 1
We are looking at the expression and saying “find out what is in X, multiply it by 2, add 1 and store the
result in memory location X. the equal sign must be understood as a replacement sign. Thus, if the
memory location contains 4, then 2 * X + 1 = 9 and the value 9 is stored in X. i.e. the value X is replaced
by 9.
Examples of valid replacement statements are:
10 LET X = -0.07 Store value - .07 in X
12 X1= A + B / C -1 Evaluate the expression and store the result in X1
14 LET C$ = “GROSS PROFIT” Store the string “GROSS PROFIT” in C$
15 I = I + 1 Computer I + 1 and store result in I

Line Numbers
Each BASIC line must be numbered. Line numbers must be unsigned integers not exceeding five digits.
In most systems, statements or lines may be entered in any desired order; the system will sort the
statements in order when the program is list or executed. Statements will be processed by the computer
in ascending order under a decision statement (IF) or unconditional transfer (Go To) is encountered.
Example
10 LET X = 3
20 PRINT X, Y
15 LET Y = X ↑ 3
BASIC would store these statements in its memory in the following orders
10 LET X = 3
15 LET Y = X ↑ 3
20 PRINT X, Y

The Print Statement


The general form of the PRINT statement is

Line number PRINT expression List

Where the expression list can be, alternatively,


A constant PRINT 7.6
A variable PRINT X
An expression PRINT (H – 50) * R * 2.5 + H * R
A character string PRINT “PAY HOURS RATE”
A combination of the above separated by commas or semi-colons
PRINT “PAY = “; P; “HOURS*= “; H

The Termination Statements STOP and END


The last BASIC statement in a BASIC program should always be the END statement. For this reason,
the END statement should always have the highest line number in your program. If you type BASIC
statements after the END statement, these will not be processed by BASIC.
The STOP statement tells BASIC where in your program you want to stop processing instructions. The
general form of termination statements are;

Line number STOP


Line number END

Any program can be written without a STOP statement.

è LISTING AND DEBUGGING PROGRAMS


NUMBER REPRESENTATION AND PROGRAMMING EXAMPLES
By now you may have realized how easily one can make errors on a computer. Errors commonly called
bugs, come in essentially four varieties: typographical, syntactical, logical, and system related.
Bugs
1) Typographical Errors: These errors are due to mistypes and they are interpreted by the computer
as syntax errors. E.g. letter O is typed instead of digit 0.
2) Sy-ntactical Errors: Syntax error reflects the programmer’s inability to observe correctly the
grammatical rules of the BASIC language.
Examples
10 LET P + 1 Typographical error + should be =
20 LET X = A (B + C)
20 LET X = A * (B + C) 30 PRINT “HELLO” S1 S2 Punctuation missing between items 40
LET Z = (3 + X * (4 + T) Missing right parenthesis
3) Logical Error: Logical errors are the most difficult errors to detect because they are very well
camouflaged.
Example:
LET Y = T /N
Although, it is a perfect instruction. Yet it could be a time bomb during the execution if N = O.
Also a programmer may wish to print the result stored in memory location R, but instead write
the instruction PRINTS.
4) System Errors: Systems errors relate t the computer system itself, for example, a printer may not
be turned on when the CPU needs it, thereby causing a special error message to appear.
Listing and Debugging Programs
To debug a program means to take the bugs out of a program i.e. to correct it consider the following
program
10 LET H = 40 The user types letter O instead of zero.
15 LEM R = 5.50 LEM is typed instead of LET
20 LET P = H + R Also there is an error due to the omission of PRINT instruction.
25 END
RUN

Number Representation
Constants in BASIC can be expressed in two different notations
1. Decimal notation, with or without decimal points.
Examples: 45.67, -12.3, 11240, -48216.
2. Exponent notation, where the character E is used to represent the decimal base 10, followed by a
two digit (with or without sign) to represent the exponent (power).
Examples:
10 LET X = 3.456 E + 2 where 3.456 E + 2 = 3.456 x 102
12 LET Y = -3. E -3where -3.E-3 = -3 x 10-3 = -.003 14 LET Z = 1.23 EO where 1.23 EO =
1.23 x 100 = 1.23
On output, as a result of the PRINT statement the BASIC will use E notation to represent a constant if
the value of the constant is outside a certain range. The range will vary from one computer to another.
Example:
10 LET X = 123456
20 LET Y = 123456 789012345
30 LET T = . 0003245
35 LET Z = 22.1 E 3
45 LET V = -.001
48 LET W = .01
49 PRINT X; Y; T; Z; V; W
RUN
123456 1.2345679E + 14 3.245 E - 4 22100 -1E-03 .01

Programming Example
Income Calculation
Mr. V is a widower with three children aged 12, 10 and 19. His monthly salary is $1,523.36. His
monthly contribution to a retirement plan is 6.6 percent of his first 9 month’s salary; retirement
deductions are spread over a 12 month period for each child support for social security. His monthly
social security deduction is 6.7 percent of his monthly income, and his federal income tax is 15.6 percent
of his yearly gross (deducted on monthly basis). Monthly payments for life insurance equal 9.6 percent
of his monthly salary after social security and federal tax deductions. Write a program to compute his
monthly spendable income.

10 LET C = 119.25 Support per child


15 LET I = 1523.36 Monthly salary
20 LET R = (9*J. *. 066)/12 Monthly retirement plan deduction
25 LET S = .067 * I Monthly social security deduction
30 LET F = .156 * I Monthly federal income tax
35 LET T = I – (R + S + F) Net after deductions
40 LET L = .096 * (I – F – S) Monthly life insurance payments
45 LET T1 = T – L Net minus life insurance payments
50 LET T1 = T1 + 2 * C Plus child support (2 less than 18 years)
55 PRINT “SPENDABLE INCOME IS”; T1
60 END
SPENDABLE INCOME IS 1233.11

Errors are called bugs. Debugging means correcting errors

è FLOWCHARTING AND BASIC STATEMENTS


Flowchart is a set of symbols linked by directed lines which represent a sequence of operations for
flowchart is essentially a pictorials representation ( as sort of visual outline) of the sequence of steps that
must be taken in order to solve a particular problem.
Input Statement
Below is the program to compute each employee of a company’s gross pay.
05 REM PAYROLL PROGRAM The REM instruction is to document the
10 PRINT “ENTER NAME” program when the program is Ron, the
INPUT instruction line 15 will cause the
20 PRINT “ENTER HOURS” computer to stop and display a question
25 INPUT H mark (?) on the screen. The user enters a
name and presses the RETURN key This
30 PRINT “ENTER RATE’ causes the name entered on the screen to
35 INPUT R be stored in memory location N $
40 REM
45 IF H> 40 GO TO 65
If condition is true, transfer to 65 to
50 LET P = H * R compute overtime, otherwise (H < 40)
the computer automatically goes to the
55 GO TO 95 next instruction 50
60 REM
65 LET P1 = 40 * R
70 LET V = H – 40 The transfer to line 65 is made
only if the condition H> at line 45 is true
75 LET P2 = V * R * 1.5
80 LET P = P1 + P2 P1 is the first 40 hours at regular rate
85 PRINT “OVERTIME HOURS = ”; V V is the overtime pay
90 PRINT ‘OVERTIME PAY = ” ; P2 D is the final pay
95 PRINT “PAY TO”, N $; P Line 90 or as
a result of line 55
98 GO TO 10

When the input instruction is executed (run), the following happens


1. The computer types a question mark on the terminal
2. The computer stops the user at this pint should enter a value for each of the variables specified by
the INPUT statement. These values are then stored in the computer memory in the specified
memory locations when the ENTER / RETURN key is pressed.
10 PRINT “ENTER AGE PLEASE” Note that when the input Instruction is typed and the user presses
ENTER key to get to the next instruction (20), the computer does not display a? and it does not stop.
This only happens when RUN is typed
15 INPUT X
20 PRINT “YOU FIBBER, YOUR ARE NOT”
25 PRINT X; “YEARS OLD” RUN
ENTER AGE PLEASE
? 41 (entered by user) Output produced by instruction above You FIBBER, YOU ARE NOT 41
YEARS OLD

Remark: Note the GO TO 10 statements at the line program repeatedly. The process of repeating a
sequence of instructions is called a Loop. In this case the loop is infinite and the only way the user can
escape from it is by pressing a special key on the keyboard (see your user’s manual
The unconditional Transfer Statement GO TO
The General form of the Go to Statement is
line number Go TO transfer line number

A BASIC program consists of a sequence of BASIC STATEMENTS. Basic will process these
statements one after another in sequential order. When BASIC encounters a GO TO statement it will
transfer control to the statement specified, that is, processing will continue at the transfer number. It also
allows the program to branch back to repeat (reprocess) certain instructions or certain procedures; this is
called looping
Examples 1
GOT TO 50
20 - This code is by passed
50 LET Y = 3 * Z
Example 2
20 INPUT H, R, B
This block of code is processed repeatedly.

50 GO TO 20 variables) is used. The contents (value) of each location are to be printed on some output
device. For example, the Block
Indicates that values in locations P and H are to be displayed (written) on some device
PRINT P, H

1. The Processing Block


A rectangular symbol is used for processing instructions. The most common form for
expressing these instructions is the replacement statement. A replacement statement specifies the
arithmetic operations to be performed on constants / or variables and the location (variable) into which
the value computed is to be placed. For example, the block P = 2* H specifies that the
contents (value) of H is to be multiplied by 2 and the result is to be placed into P.
2. The Decision Block
The diamond – shaped symbol is used to denote decisions. A common means of expressing a
decision is in terms of a 46 question that can be answered Yes or No. The question must involve only
mathematical relations such as equality (=) , less than (<)
Flowchart is on the logic required for solving a problem rather than on the mechanics or specifics of a
programming language.
3. The Terminal Block
An oval – shaped symbol is used to mark the point at which execution of instructions is to
begin and end. The instruction START may be used to mark the beginning point; the instruction END or
STOP may be used to mark the ending point. A flowchart may have only one starting point (entry point)
but may have many ending points.
4. The Input / Output Block
A parallelogram – shaped symbol is used for input and output operation, the instruction
(command) READ OR INPUT, followed by a list of names (variables) separated by commas, is used.
These names or variables can be thought of as symbolic names given to memory locations in which the
data read is to be stored.
Variables names should be chosen to covey the nature of the data to be processed
INPUT N$,
For an output operation, the instruction 47 PRINT, followed by a list of memory locations

START Enter the name into memory cell


N$
INPUT N$ Enter the hours into memory
location H
INPUT H Enter the rate into R If the number
of hours > 40 Do the following:
INPUT R
Computer pay for first 40 hours compute
over time hours compute overtime pay
compute final pay Print overtime hours
H > 40 Print overtime pay otherwide do
the following: compute regular
pay and Print the name and pay in
P1 = 40 * R either case. And then go back to
V = H – 40 process another record
P2 = V * R * 1
P = PI + P2

PRINT V

PRINT P2

P=H*R

PRINT N$, P

STOP

This is for REM PAYROLL PROGRAM.


FLOWCHARTING
A flowchart is essentially a pictorial ( a sort of visual outline) of the sequence of steps that must be taken
in order to solve a particular problem

a. Flowchart Definition
Because of the requirement for logical organization and requiring of program instructions, it is usually
difficulty to write a computer program without first expressing the program in some preliminary form.
For a complex problem the tool is the flowchart although for a simple problem a verbal outline of steps
required may suffice. A flowchart is a pictorial representation of the logic (method) used to solve a
particular problem. A flowchart is particularly useful for visualizing paths through the logic of a
program
b. Flowchart symbols
In program flowcharts, the symbols shown in the figure below is normally used. Each symbol or block
represents a different type of operation written within the blocks are instructions to indicate (in general
terms) what operation is to be performed. It is not necessary to express the instructions used in a
flowchart block in any particular computer language. The emphasis in the less than or equal to (≤),
greater than (>), greater than or equal to (≥) or not equal to (>). The decision blow is the only block from
which two different logical paths may be selected follows indicate the path to be taken depending on the
decision block.

IS

H > 40 Yes

No
If the value of H is greater than 40, the path marked YES is taken, otherwise the NO path is taken.
c. Flowlines
The sequence of instructions to be executed in a flowcharted algorithm is denoted by straight
lines with an arrowhead such as → or ↓. The direction of flow is always in the direction
pointed by the arrowhead.
d. Connector Blocks
When it is convenient to draw flowlines to connect one area of the flowchart to another,
connectors are often used. Connectors serve two purpose:
1) To identify a block by a label for reference purposes
2) To indicate transfer to another labelled block.
The symbol used for a connector is a circle .
A label is placed in the connector block when the flow line points away from the connector, such as
→ , the connector is being used to denote an entry point, that is, a block to which transfer will be
made from some point in the flowchat when the flow- line points toward the connector as in → , the
connector is being used to indicate a transfer.

BASIC STATEMENTS: THE DECISION STATEMENT


INTRODUCTION
The central processing unit (CPO) has a logical unit which allows the computer to compare one number
with another as well as one alphanumeric string with another. This decision-making capability is
available to BASIC through the If statement which allows the computer to transfer to a non sequential
instruction.

If / Then
Then general form of the If statement is
line number if condition {GO TO THEN} transfer line number
Where the condition consists of two arithmetic expressions or character string variables linked together
by one of the relational operations shown in below:

BASIC Relational Operators Mathematical symbol Meaning


= = Equal to
< < Less than
<= or =< ≤ Less than or equal to
> > Greater than
>= or => ≥ Greater than or equal to
< > or >< ≠ Nor equal to

The If Statement Revisited


Another form of the If statement is
line number if condition THEN statement

Where the statement can be an input / output instruction, a replacement statement or a STOP. If the
condition is met, the statement (and only one) is executed and the control is transferred to the condition
is not met control is transferred to the statement following the If.

Example 1
10 If A < B THEN K = K + 1 If A > B the statement K = K + 1
will executed after which statement
15 If A = 4 GO TO 67 15 will be executed. If A ≥ B 15
will be processed next.

Example 2
10 If C < > 0 THEN PRINT A IF C ≠ 0 print the value
A and stop otherwise ( C
15 STOP ≠ 0) stop
Since only one statement is permissible after the THEN keyword the following code would be needed to
account for the following flowchart

A>B C=C+1 PRINT A 10 IF A > = B GO TO 25


15 C = C + 1
20 PRINT A
25 K = 5

K=5
The REM Statement
The general form of REM statement is
line number REM literal characters

Where REM (remark) is a key word, and the literal characters are supplied by the programmer.
Although the REM statement requires a line number, it is not an executable statement.
èLOGICAL EXPRESSIONS AND THE AND / OR LOGICAL OPERATORS
A logical expression can be thought of as a proposition. A proposition is a statement that is either true or
false.
And
Examples
1. To determine whether A lies between 7 and 14, the following statement could be used:
If A > > AND A < 14 THEIR PRINT “YES” Note that the statement If A > > ABD < 14 is
invalided. No logical operator may be side by side with a relational operator.
2. Parentheses may be used to devote the order in which the expressions are to be evaluated
If ( S = 1 AND M 1 < > 0) OR (S = 3 AND M1 > 4) THEN PRINT S, M1

On / Go To Statement
The ON/GO TO statement is a useful and convenient statement that allows transfer to many different
points in a program
Line number ON expression GO TO line number line
Another form of the If statement is
line number on expression GO TO line number 1, - - line number n
The rule is the following
If the value of the expression is 1 control is transferred to line number 1
If the value of the expression is 2, control is transferred to the line number 2 Examples
20 ON N GO TO 3, 57, 100, 4. IF N = 3 control is transferred to 100
15 ON J – 2 * K GO TO 30, 10 IF J – 2 * K = 1 go to statement 30
30 ON Y ↑1 GO TO 10, 20, 60 IF Y ↑ I = 1, 2, 3, go to 10, 20 and 60 respectively.

èData Statement
The general form of the DATA Statement is

line number DATA constant - list

Where DAT is the keyword. The constant – list consists of numeric or character constants. Items in the
list must be separated by commas.
DATA is a non executable statement, unlike all other BASIC statements, except REM – encountered so
far.
The DATA statement inform the BASIC system that the numbers specified in its constant
– list are to be stored in memory until the user decides to process these numbers during program
execution.
Example
1 DATA 1, 3.1
5 READ X, Y, Z, W$ 10 PRINT X, Y, Z, W$
15 DATA 3, HI
20 END
RUN
1 3.1 3 HI
Example 2
1 DATA 1, 3.1, 3, HI
5 READ X
10 READ Y
15 READ Z, W$
20 PRINT X, Y, Z W$
25 END
RUN
1 3.1 3 HI

Example 3
1 DATA 1
5 DATA 3.1
10 READ X, Y, Z, W$
15 DATA 3, HI
20 PRINT X, Y, Z, W$
25 END
RUN
1 3.1 3 HI
2. Parentheses may be used to devote the order in which the expressions are to be evaluated
If ( S = 1 AND M 1 < > 0) OR (S = 3 AND M1 > 4) THEN PRINT S, M1

3.2 Read Statement


The general form of the READ statement is

Line number READ variable list

Where READ is a key word. The variable list consists of variables separated from one another by
commas
The READ statement causes as many values to be fetched from the DATA block as there are variables
in the READ list.
5 READ X, Y
10 DATA 1, 3
15 READ 2$
20 DATA WE. 4. 6
25 READ W

You might also like