0 ratings 0% found this document useful (0 votes) 119 views 286 pages Basic BASIC (1978)
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here .
Available Formats
Download as PDF or read online on Scribd
Go to previous items Go to next items
Save Basic BASIC (1978) For Later EAS (HUES
errs AN INTRODUCTION TO COMPUTER PROGRAMMING
IN BASIC LANGUAGE
FOR Y=1 Td 10
LET C=0 MC
FOR K=1 TO 50
LET F=INTC2*#RNDC1)) JAMES S. COAN
IF F=1 THEN 60
PRINT "T'"3
G@Td 100
REM C COUNTS THE NUMBER @F HEADS
{re ees |
PRINT “H's
NEXT X
PRINT
PRINT "HEADS "3C3"@UT @F SO FLIPS"
NEXT Y
130 END
seh]
ne teas
HTTTTTHTTHHT THT TTT TTTTHTHHHHHHTITTTTHTHHH THHHTITHHTT
Lalor eat OUT OF SO FLIPS
HTTHTHHTTHTTHTHHTTTHHHTTTTTTHHHHT TH TH THHHH THH T HHH
HEADS 26 QUT OF SO FLIPS
HTHTTTHTTHTTHTTTTTTTITHTTHTT IH THTHTT TH TTT TI THHH THHHT
Ey ae QUT OF SO FLIPS
ILIRSESS LILI U LIL LOL LPI L LIC Pee ee LOL ketene
Patra Yeh) eat @UT OF SO FLIPS
TTHHTTTTHTHH T THTH THHHHT THH THHHT TT TT TH THHHHTTHTHHTT
alate) oe) OUT OF SO FLIPS
HTH THHHHHHH THT TT TTT THT THHHHHH TT THT TTTHTHT THH THH THH
Pale) 26 QUT OF SO FLIPS
HTTTTTHTTTTHH FTHTTH THHHH TH THH TTHHHH THT TH TH THT TTTHT,
PTE} 22 OUT OF SO FLIPS
ARCO le
aot Ey cr QuT OF SO FLIPS
HTTHHTHH RUIEASTER ET LaST CALL un Lek at
at rare rs) ery QUT OF SO FLIPS
CLLRS LOSE e LLL eeO LCG bebe
ater ers) 26 QUT OF SO FLIPS
ersBasic BASIC
An Introduction to Computer Programming in BASIC LanguageHayden Computer Programming Series
COMPREHENSIVE STANDARD FORTRAN PROGRAMMING
James N. Haag
BASICS OF DIGITAL COMPUTER PROGRAMMING (Second Ed.)
John 8. Murphy
BASIC BASIC: An Introduction to Computer Programming in BASIC Language (Second Ed.)
James 8, Goan
ADVANCED BASIG: Applications and Problems
vames S. Coan
DISCOVERING BASIC: A Problem Solving Approach,
Robert E. Smith
ASSEMBLY LANGUAGE BASICS
Inving A. Dodes
PROGRAMMING PROVERBS
Henry F. Ledgard
PROGRAMMING PROVERBS FOR FORTRAN PROGRAMMERS
Henty F. Leogard
FORTRAN WITH STYLE: Programming Proverbs:
Henry F. Ledgard and Louis J. Chmura
COBOL WITH STYLE: Programming Proverbs
Louis J. Chmura and Henry F. Ledgard
.n Annotated Program Book
BASIC WITH STYLE: Programming Proverbs
Pau! Nagin and Henry F. Ledgard
SNOBOL: An Introduction to Programming
Poter R. Newsted
FORTRAN FUNDAMENTALS: A Short Course
Jack Steingraber
THE BASIG WORKBOOK: Creative Techniques for Beginning Programmers
Kenneth E, Schoman, Jr.
ASIC FROM THE GROUND UP
David E. Simon
APL: AN INTRODUCTION
Howard A. PeelleBasic BASIC
SECOND EDITION
An Introduction to Computer Programming in BASIC Language
JAMES S. COAN
Community Computer Corporation
Germantown Friends School
Lia
HAYDEN BOOK COMPANY, INC.
Rochelle Park, New JerseyLibrary of Congress Cataloging in Publication Data
Coan, James S
Basic BASIC: an introduction to computer
programming in BASIC language.
(Hayden computer programming series)
Includes indexes.
1, Basic (Computer program language).
2. Electronic digital computers—Programming.
1. Title.
QA76.73.B3062 1978 001.6424
ISBN 0-8104-5107-7
ISBN 0-8104-5106-9 pbk.
77-14640
Copyright © 1970, 1978 by HAYDEN BOOK COMPANY, INC. All rights re-
served. No part of this book may be reprinted, or reproduced, or utilized in
any form or by any electronic, mechanical, or other means, now known or
hereafter invented, including photocopying and recording, or in any in-
formation storage and retrieval system, without permission in writing from
the Publisher.
Printed in the United States of America
456789
PRINTING
79 80 81 82 83 84 85
YEARPreface
With the increasing availability of computer access through remote terminals
and time sharing, more and more schools and colleges are able to introduce
programming to substantial numbers of students.
This book is an attempt to incorporate computer programming, using BASIC
language, and the teaching of mathematics. I believe the two activities support
each other.
Flowcharts are used throughout the text. The general approach is to begin
with short complete programs and then simply and methodically build them
into larger programs. Each new capability or new organization of capabilities
is presented to create a desired effect in a program. Details are introduced
only as they become necessary or useful for the writing of a program, rather
than as sets of facts to be memorized in case a particular situation should
ever arise. Over 125 programs are used to achieve this.
All of the elementary BASIC language capabilities are presented in the first
five chapters and Chap. 7. Chapter 6 and Chaps. 8-13 emphasize applications.
The first seven chapters may be studied in conjunction with, or at any time fol-
lowing, a first-year algebra course. Chapters 8-13 are applications oriented,
covering many of the popular topics of precalculus mathematics, with all of the
required algorithms developed in the text. Thus, this text is suitable for use
either as a supplementary text to be incorporated into existing mathematics
courses, or as the text for a course or unit to cover programming alone.
Appendices A and B, respectively, present information for the operation
of programs on paper tape and a few comments on error diagnosis. Appendix
C introduces two formatting capabilities that are available on some time-
sharing systems. Flowchart shapes are summarized in Appendix D. A sum-
mary of BASIC statement types is provided in Appendix E and an index of
all the programs in Chaps. 2-13 is provided in Appendix F.
Many of the problems in the book are intended to enable the student to
develop interesting mathematical concepts upon seeing the printed results of
program RUNS. Possible solution programs are given in Appendix G for the
even-numbered problems to give the student an indication of the correctnessof his program without being required to run every program. However, par-
ticularly at the beginning, students derive greater benelit from seeing programs
run (or not run) than from any other programming activity.
1 wish to thank Germantown Friends School for its support in the prepara-
tion of this text. Thanks are due Mrs. Geoffrey Wilson for test teaching and
numerous students for test learning portions of the manuscript.
James S. Coan
PhiladelphiaPreface to the Second Edition
The First Edition of this book has been significantly enhanced by present-
ing character string handling and the use of data files. ce strings and files
involve language differences which depend on the computer, two versions are
presented. Demonstration programs are presented in Chap. 7 for both General
Electric Information Services BASIC and Hewlett-Packard BASIC.
The little used statement RESTORE is no longer discussed, and the INPUT
statement is now presented in Chap. 1
Thanks are due to the Community Computer Corporation for assistance in
the preparation of material for this Second
Jaates $, Coan
PhiladelphiaChapter
Chapter
Chapter
Chapter
Contents
1—Introduction to BASIC
1-1 PRINT
1-2 READ-DATA
1-3 System Commands
1-4 LET
1-5 INPUT
1-6 Sample Progiams .
1-7 Comma and Semicolon in PRINT Statements
Summary and Problems for Chap. 1
2-Writing a Program
2-1 Planning
2-2 REM
2-3 Flowcharting
2-4 IF-THEN
Summary and Problems for Chap. 2
3—Loops and Lists
3-1 Introduction to Machine-Made Loops
Summary and Problems
3-2 More on Loops
Summary and Problems
3-3 Introduction to Lists
Summary and Problems
3-4 More on Lists
Summary and Problems
4—Computer Functions
4-1 Introduction to INT( ), SQR( ), ABS( ), and SGN( )
5
Summary and Problems
4-2 Reducing Common Fractions and Dimension
Conversions
Summary and Problems
4-3 Program-Defined Functions
Summary and Problems
44 Random Number Generator
Summary and ProblemsChapter 5—Elementary Data Processing 73
5-1 introduction to Data Processing 73
and Problems 7
5-2 inays 78
Summary and Problems 84
5-3 A More Detailed Questionnaire Analysis 85
Summary and Problems 89
Chapter 6—Specific Applications 91
6-1 Euclidean Algorithm : 91
Problems 93
6-2 Change Base 93
Problems 98
6-3 Looking at Integers Digit by Digit 98
Problems 100
Chapter 7—Strings and Files 101
7-1 Introduction to Strings 101
7-2 Strings—The Substring Scheme 103
Summary and Problems 105
7-3 The String Array Scheme 106
Summary and Problems 108
7-4 Introduction to Data Files 109
7-5 Hewlett-Packard Files 110
Summary and Problems 114
7-6 General Electric Files 115,
Summary and Problems 121
Chapter 8—The Quadratic Function 122
BL Zeros 128
8-2 Axis of Symmetry and Turning Point 123
8-3 Plotting the Parabola 125
Summary and Problems for Chap. 8 131
Chapter 9—Trigonometry 132,
9-1 Introduction to SIN(X), C@S(X), and TAN(X) 132
9-2 Right Triangles and Arctangent 133
Summary and Problems 137
9-3 Law of Sines and Law of Cosines 138
Summary and Problems 142
9-4 Polar Coordinates 143
Summary and Problems 145
Chapter 10—Complex Numbers 147
10-1 Fundamental Operations 147
Summary and Problems 149
10-2 Polar Coordinates 149
Summary and Problems 151
10-3 Powers and Roots 152
Problems 153,
11-Poly 14
11-1 Fundamental Operations 154
Summary and Problems 159
11-2 Integral Zeros 159
Summary and Problems 16311-3 Real Zeros
Summary and Problems
11-4 Complex Zeros
Summary and Problems
Chapter 12—-MAT Instructions in BASIC
12-1 Introduction to MAT Instructions
Problems
12-2 Solving a Problem
Problems
12-3 Operations and Special Matrices
Summary and Problems
12-4 Solving Simultaneous Linear Equations
Summary and Problems
12-5 Transpose of a Matrix
Problems
Chapter 13-Elementary Probability
13-1 Introduction
13.2. Enumeration
Summary and Problems
13-3 Simple Probability
Summary and Problems
13-4 Random Simulation
Problems
13-5 Binomial Trials
Problems
Appendix A—Storing Programs on Paper Tape
Acl Introduction
A-2 Punching Paper Tape Off-Line
A-3 Reading Paper Tape
A-4 Getting the Computer to Punch Paper Tape
Appendix B-Error Diagnosis
B-1 Introduction
B-2 Errors that Prevent RUN
B-3 Errors that Terminate a RUN
B-4 Errors that Cause Unexpected or Incorrect Results
Summary
Appendix C—Special Formatting Functions
C-1 TAB(X)
C-2 IMAGE Statement
Appendix D-Summary of Flowchart Shapes
Appendix E—Summary of Statements in BASIC
Matrix Instructions
Functions
Files
Appendix F—Index of Programs in Text
Appendix G—Answers to Even-Numbered Problems
Index
163
197Basie BASIC
An Introduction to Computer Programming in BASIC LanguageCHAPTER 1
introduction to BASIC
In working with a computer, you, the programmer, must communicate
with the computer. In order to do that you will have tu use a language that
the computer will understand. There are many languages written for this pur-
pose. The language of this text is called BASIC. The actual physical com-
munication is rather complicated and we will ignore most of the mechanics
except for the apparatus at our end of things. The device we will be using is
called a remote terminal. It will have a specific name depending on the manu-
facturer. The remote terminal has a keyboard, which is the part we are most
concerned about.
1-1 PRINT
No matter how complicated a particular set of instructions is, you will have
to tell the computer to put the results into some form discernible to yourself.
Therefore, let us begin with a discussion of the PRINT statement. If you want
the computer to write the following statement “THIS IS A SHORT PRO-
GRAM,” you will type on the keyboard of the terminal as follows:
10 PRINT “THIS IS A SHORT PROGRAM."
20 END
The computer, on proper instruction, will do exactly what you have set out
to do.
The two lines 10 and 20 constitute a complete program. Several comments
are in order here.
1) Note that every line in a program must begin with a positive integer.
2) The statement that we want to write out is in quotes; this may be used
to good advantage, for example, for headings and labels
3) In many time-share systems, every program must have as its highest
numbered line the END statement. In some systems, the END statement
is optional.2 Basic BASIC
4) Note that all the letters are capitals. The terminal you may use or may
not be restricted in this way, Note also that the letter “O” has a slash
mark to distinguish it from the digit “0.” On some terminals the reverse
is true, the digit “O” has a slash and the letter “O” does not. On some
printers one is more nearly a circle than the other or one is nearly dia-
mond shaped, You can easily determine the method used by your equip-
ment by examining some sample output on your screen or “hard copy.”
5) It is conventional although not required to use intervals of 10 for the
numbers of adjacent lines in a program. This is because any modifica-
umbers. So you can use the
Il be ing to know
must also have li
the progra
number os
at this point that the line numbers do not have to be typed in order.
No matter what order they are typed in, the computer will follow
numerical order in executing the program.
6) Each line of a program is called a program statement.
You probably think of the computer as something that more commonly
produces numerical results and you are partly correct. Suppose you wish to
multiply 23.4 by 91, One way of doing this on the computer would be to
write a program like thi
10 PRINT 23.4891
20 END
‘Then on proper instruction the computer will type out the following and stop.
2129-4
bone
Computers vary as to the message that gets printed here. Notice the absence
of quotes. In this case you have instructed the computer to perform an opera-
tion. Had you in fact wanted 23.4 ° 91 typed out, then you would change the
program. You might write the following:
10 PRINT "23. 48912") 23. 4891
20 END
This time the result will be as follows:
236 4n912 212964
pone
You have succeeded in instructing the computer not only to perform an opera-
tion, but to print out the result in easily understandable form, which is desir-
able throughout mathematics. Notice the use of the comma here. The comma
may be used to separate the different parts of a PRINT statement. Used in
this way, a comma is called a delimiter. Notice too, that there are eight spaces
bet i and the number. A way to eliminaie all bui one of
them will be explained later. There are many fine points that we will discuss
as we progress, but for now we will take it in small quantities.
If we were limited to the PRINT and the END instructions, we would
quickly return to using pencil and paper or an ordinary desk calculator. With-
eheseaial
the equalsIntroduction to BASIC 3
out some additional capability, the computer would soon disappear. ‘This
brings us to the READ and DATA statements.
PRINT
Characters in quotes will be printed exactly as typed. Computed
results will be typed as decimal numbers or as integers.
1-2 READ-DATA
The READ statement says to look for DATA as in the following:
10 DATA 23+ 4491+83+ 19487494) 16+ 598-8 76-918.
20 READ A,B
30 PRINT Ate
35 G3T3 20
40 END
The computer ignores the DATA statement until it finds a READ, then it
takes the first number in the first DATA statement and assigns that value to
the first variable in the READ statement. Then, if there is a comma and
another variable in READ as in our example, the computer assigns the second
number in the DATA line to it; were there a third variable, the computer
would continue until it ran out of variables. In our program, the first time
through, A = 23.4 and B = 91. The next line says PRINT the product. Having
printed the product the computer looks for the next instruction, which is
GOTO 20. This is a new one that means exactly what it says. So the computer
will GOT@ line 20 and execute that instruction again. At this point the com-
puter “knows” that it has already read and used the first two numbers in
the DATA line. So it goes to the third and fourth numbers and assigns them
to A and B in order and proceeds to print the product of 83 and 19, then
goes back and assigns the fifth and sixth numbers to A and B, and so on until
it runs out of numbers in the DATA line. There may be any number of DATA
lines in a given program; all you need to realize for the time being is that a
comma must be used to separate each discrete item of data and a comma
should not be placed after the last item in a particular DATA line. Also, be
careful not to use commas to designate thousands, millions, ete. Warning: You
may not put variables or operation symbols in a DATA line. Only numbers in
decimal form are allowed so far. Here is the computer's response to the above
program:
212944
1577
8173
454. 48,
B041 68.
QUT 9F DATA IN LIVE 204 Basic BASIC
Note the explicit message at the completion of the print-out. This will vary
from computer to computer.
In our examples so far, we have used only multiplication (°). The other
arithmetic operations that you may use are addition (+), subtraction (—),
division (/), and exponentiation (raising to a power), There are two symbols
in common use for exponentiation: one is an upwards arrow (4), and the other
is a double asterisk (°°), Symbols used to instruct the computer to perform
some operation are called operators. The symbols listed here are specifically
designated as the arithmetic operators. The numbers on which the operation
is to be performed are called of
multiplication symbol mnst he present, AB in al
in algebra, the
wen
for the computer. The computer assigns the same priorities to arithmetic
operations as are assigned in algebra. If there are several operations of the
same priority to be performed on the same line, the computer does them from
left to right. Several sample programs will be given soon.
to convent
READ
The READ statement looks for numbers in a DATA statement.
READ X, Y, Z looks for numbers in groups of three.
DATA
The DATA statement supplies values for the variables designated
in the corresponding READ statement. Items of data must be sep-
arated by a comma. Numbers only are allowed.
1-3. SYSTEM COMMANDS
There are two kinds of instructions of which you should be aware. We have
already discussed an instruction given by a program that you have written. We
have not yet mentioned an equally important kind of instruction, the system
command. We must realize that the computer does nothing by itself. There-
fore, there must be what is called an executive program which will respond to
your wishes. You need not worry about the executive program; it is taken care
of by the people who maintain the computer.
The first system command required is referred to as the sign-on or log-on
The exact form of this varies from computer to computer. So we really cannot
be specific here. It simply notifies the computer that you would like to use it.
Once you are signed on, the next most important command is RUN. After
you have typed out your prugran, the compuier musi bave a way of knowing
that you want it to execute the program. So you must type RUN and then
touch the return key on the keyboard. Only then will it respond to the pro-
grammed instructions.
Possibly next in importance is the command SCR (SCRub or SCRatch) erIntroduction to BASIC 5
CLE (CLEar) followed by depressing the return key. (Which you use will
depend on the computer you are connected with.) Suppose you have run a
program and someone else would like to run his, The old program may be
erased by using the SCR command. So whenever you begin a new program
it might be wise to simply type SCR and touch the return key. The system
command must not be preceded by a number. There are several other com-
mands that we will take up as they seem appropriate.
RUN
Notifies the computer to execute the program instructions. Must
not have a number in front of it.
SCR or CLE anew
Notifies the computer that you are not going to use the current
program. The current program is erased from the working area of the
computer. Must not have a number in front of it.
1-4 LET
At this point you do have enough information to write quite a few
programs. However, another statement type that may be used to make life
easier is the LET statement. The LET statement may be used to assign any
number or any algebraic expression to any variable. Using a LET statement,
the last program would look like this:
10 DATA 23+ 4591+83+ 194879 .4s 76» 5-981 876s 918
20 READ ALB
30 LET Cease
40 PRINT C
SO GAT2 20
60 END
RUN
2129.4
1577
8173
45.4. 48
804168
QUT 3F DATA IN LINE 20
We obtain the same results as before. In this particular program, we really
did not save anything. However, in any situation where we need to write the
value of A ° B several times or the expression is more involved, we will see
that a saving may result. There are many things that you could not do
without a LET capability./ 6 Basic BASIC
LET
May be used to assign explicit values to a variable as LET X = 4.56,
or may be used to assign algebraic expressions to a variable as LET
V=X°F+/Y °G. Note: All variables on the right-hand side must
have been previously evaluated. On some computers LET is optional.
Such systems permit Z = 4.56, for example.
1-5 INPUT
‘The INPUT statement serves much the same purpose as the READ state-
ment in that it permits us to provide numbers for the computer to work with.
For example, 100 INPUT A will cause the computer to print a question mark
and stop at line 100. The question mark is a signal to whoever is operating the
terminal that he or she is to type the desired value for A on the keyboard and
press the carriage return key to resume the run of the program. Likewise, 100
INPUT A, B, C will call for three numbers separated by commas to be typed
at the keyboard. It is advisable to have the computer print a label so that the
operator can determine the nature of the numbers required. In the following
program, note that the semicolon at the end of line 100 enables us to type the
values for A and B on the same line as the printed label. The input numbers
15, 17 following the question mark were typed at the keyboard by the program
operator.
100 PRINT "INPUT TW NUMBERS? "5
110 INPUT A,B
120 PRINT THE NUMBERS ARI
130 PRINT THEIR SUM I
140 PRINT
150 END
RUN
INPUT TWO NUMBERS! 715+17
THE NUMBERS ARE: 15 17
THEIR SUM IS? 32
THEIR PRODUCT IS: 255
DONE
INPUT
Causes the computer to request data from the keyboard.
If we want the computer to obtain a decimal value for a compound frac-
tion, there may be several programs that will do the job. Here we will have
to use our knowledge of the order of operations as determined in algebra.Introduction to BASIC 7
Three programs follow that find a decimal value for
2/5 + 3/7
3/4--1/3
10 LET N=2/5¢9/7
20 LET 0=374-173
30 PRINT N7D
40 END
RUN
1.98857
Dane
10 LET Fa¢carsear 7 C9/ 4-173)
20 PRINT F
30 END
RUN
1.98857
bane
10 PRINT €2/5+3/79/¢37 4-1/3)
20 END
RUN
1.98857
bane
Parentheses serve as a powerful tool in grouping terms properly for the
desired results, Keep in mind that priorities are exactly as they are in algebra
and that if several operations of the same priority appear in the same line, they
are executed from left to right.
Carefully study the programs which follow to see how the placement of the
parentheses affects the results.
10 PRINT "3/5/3/5="13/5/3/5
20 PRINT "3/€5/3/5)="5 3/¢573/5)
30 PRINT "3/5/¢3/5)2"5 3/5/6375)
40 PRINT "€3/5)/€3/5)="1 (3/59/6375)
SO PRINT "C3/573)755"1 (3/5/3)/5
60 PRINT "€3/5)/9/5="1€3/5)73/5,
70 END
RUN
a/s/3/s= 204
O/C573/5)= 96
375737592 1
¢37597¢3/5)= 1
€375/30/5= 204
(3/5973/5= .04
Dane
10 PRINT "A=" 21361439241
20 PRINT "5213419439241
30 PRINT "Ca"5213eC143) #241
40 PRINT "Da"; 2r3¢ 143" (241)
SO PRINT “E="s2t(3¢143) 9261
60 END
RUN8 Basic BASIC
19.
26
2s
36
E= 16385
Dane
It is important to know the capacity of the computer you are working with.
Notice that according to the computer, (2/5 + 3/7)/(3/4 — 1/3) = 1.98857.
If we work that out longhand, the result would be 1.98857142. BASIC pro-
vides from 6 to 15 digits, if they are needed, depending on the computer, with
the last digit rounded off iu decimal uunibers,
If the results require more than the digit capacity of the computer, the
computer prints in scientific notation as follows:
10 LET As98 781+
20 LET A1=8976
30 LET P=AKAL
40 PRINT Al
50 END
PALS NE" P
98781. * 8976 = 8+ 866588408
The E + 08 means “times ten to the eighth power” and the decimal number is
rounded off to the sixth digit. When the computer uses this notation, it is
called E-format. Again we get large spaces using the comma to delimit the
printed results. We will discuss this before we wind up chapter one.
‘A new item Al appears in the above program in line 20. There you will find
the statement LET Al = 8976. The computer treats this as a new variable. In
BASIC you may use any letter of the alphabet and any letter of the alphabet
followed by a single digit as a variable. Some computers have additional
simple variables. Thus a large number of variables are available.
Probably the best way to learn how the computer handles scientific notation
is by experience. So, let us run a sample program to see what happens.
SPRINT "xX", ty", aM,"
10 DATA 1+31E+10,2+13E+1 1s 1+ 16132E-05,2.83E+06
20 READ XY
26 LET Q=x/Y
40 LET Pexey
50 LET S=K+Y
60 PRINT KsYsQ,PsS.
65 GTO 20
70 END
RUNIntroduction to BASIC. 9
x Y ° Pp s
1.31000E+10 2+ 13000E+11 6 15023F-02 2. 790OEH21 2+ 2ELODEFII
14161328-05 24830008406 44 10360E-12 32.8654 2+830008+06
OUT OF DATA IN LINE 20
Notice the use of Q for quotient, P for product, ete. This is a technique
that is useful not only on the computer, but throughout mathematics.
Suppose you wish to write a program to find the total cost of a purchase
in which there are different numbers of items at various prices, say 2 @ $.35,
3 @ $2.65, 11 @ $.25, 1 @ $9.49, and 35 @ $1.59. We could have many
more, but for a sample this should suffice. This program could of course be
written in several ways, but here is one possibility:
10 PRINT “ITEM
20 DATA 2+ +35+ 3p 2+ 6521 Be «25s 1594 As 35e 16 59
25 LET
30 READ NsP
40 LET T=TesP
4S PRINT NoPsNePsT
50 G3T@ 30
70 END
RUN
1 TEMS. UNIT PRICE cast SUBTATAL,
2 +35 “7 “7
3 2.65 7695
"1 +25 2.78
1 9.49 9.09 20.39
35 1659 55.65 76.54
OUT OF DATA IV L
NE 30
‘The single figure we set out to obtain is in the lower right-hand corner. The
result is $76.54; however, the other information is bound to be useful in at
least some situations. Besides, even if we only print the right-hand column,
we do not yet know how to dispose of the first four figures in that column. If
you only want to print the right-hand column, then lines 10 and 45 may be
altered thus:
10 PRINT “SUBT@TAL”
45 PRINT T
and only that column will be printed. Notice that line 10 is executed only once
and line 45 is executed five times. The G@T@ statement in line 50 only
returns the computer back to line 30, So the computer only prints the headings
once and only lets T = 0 once.
Still, in the last program, the combination of lines 25 and 40 may seem
strange, but it will not as soon as you gain a little more insight into how the
computer works, Line 25 is said to initialize the value of T at 0, i.e., give it an
initial value of 0. When the computer executes the initializing statement, line10 Basic BASIC
25 LET T = 0, it “says” that there is a location in the computer storage area
ich thi mi will call T and that thi
call T and that this pro;
number zero be stored in that location for now. If we then say 26 LET T = 5,
then the computer wili put the number 5 in that jocation designated as T and
zero will no longer be there. If we write a program that says 25 LET T = 0
followed by 26 LET T = T + 1, then the computer goes to the location where
it is storing the value for T, “sees” 0, adds 1 to it, and returns the result to the
location from which it just got 0, thereby replacing 0 (the old value) with 1
(the new value). So we see that in BASIC (as in other computer languages) =
does not mean “two names for the same thing.” Jt means, insiead, that the
number on the right is to be placed in a location whose name is specified on
the left. Thus we see that the equals sign as used here really specifies an opera-
tion for the computer to perform. So the equals sign is called an assignment
operator or a replacement operator, and the LET statement is called the
assignment statement or replacement statement.
Let us go through the program line by line. The lowest numbered line is a
PRINT statement. So, right off, the computer prints the headings. Then it
recognizes that the next statement is a DATA statement and ignores it. Line
25 assigns the value 0 to T. Then in line 30 the computer reads the first two
numbers in the DATA line. Line 40 says that the previous value of T is to be
taken out of storage and added to N times P. So, the first time through line
40, the value of T on the left will be 0 (from storage) plus the cost of two
items at $.35, or $.70, and the computer returns the value .70 to the location
in storage called T. Line 50 sends the computer back to read the next two
numbers in the DATA line and to add their product (7.95) to .70 to get 8.65.
It should be clear that we are printing the values of N, P, N times P, and T
each time we read two new numbers. This process continues until the com-
puter finds no more data. This causes the computer to terminate the RUN.
al hat the
1-7 COMMA AND SEMICOLON IN PRINT STATEMENTS
Let us look at one more capability. In two of the programs of this chapter,
the results were printed out with unnecessary great spaces. You may have
noticed that we did not have these spaces in the two programs where semi-
colons were used in the PRINT statements. We have two delimiters, i.e., we
have two signals that tell the computer how closely we want the results
printed. The rules are a little complicated, but in general, the semicolon
specifies closer spacing than the comma. The comma sets up zones across the
page. The number of characters in the zones does vary from computer to
computer, but 15 characters per zone is common. This zone width does
not change with the number of digits in the numbers being printed. The
semicolon sets up different sized zones depending on the number of digits
in the number and whether it is in scientific notation, Here is the pro-
gram from p. 8 again. First we run it. Then we insert a line which
replaces the comma print delimiters with semicolon delimiters. And we call
for another RUN.Introduction to BASIC 11
10 LET A=98781-
20 LET Al=89 76
30 LET PsAKAL
40 PRINT AS MEMS ALS Sty P
50 END
RUN
98 7816 * 8976 a 8. 866SHE+08
DONE
aL PRINT As "ws 15"
RUY
9a 781. * 8976 = 9.866588+08
98781. * 8976 == 8. 86658E+08
DONE
The output of this program is much more closely spaced. Notice that in the last
line of the printing there is a space between the ° and 8976. The computer
leaves a space there for a + sign but does not print it, If the number printed
were negative, then there would be a minus sign printed in that space. The
same holds true for the space between the = and 8.86658E + 08. Also notice
that in all program runs there is a space before the first number printed in any
line if the number is positive. However, if we write 10 PRINT “3” in a pro-
gram, then when we run the program, 3 will be printed in the very first
space. This is because the computer treats things in quotes differently from
the values of variables for printing purposes.
SUMMARY OF CHAP. 1
1) We now have the PRINT statement which puts results in readable form.
It may be used for titles, headings, and labels.
2) Everything in quotes will be printed just as you type it (except more
quotes).
3) Commas or semicolons may be used between the different items to be
printed to control spacing.
4) The READ statement is used to read data. Several variables may be
read with a single READ statement by separating them with commas, or they
may be read with different READ statements. Just be sure the data is in
proper order to match the READ variables.
5) The DATA statement supplies data for the READ statements. Discrete
items of data must be separated with commas
6) The LET statement may be used to a
expression to any variable.
7) The INPUT statement allows the operator to enter data from the key-
board in response to a question mark.
ign any value or any algebraic12 Basic BASIC
8) The GOT statement is used to alter the progress of the computer
during the execution of a program.
9) The END statement may or may not be required. If required, it must
carry the highest line number in the program.
10) The system commands to date are RUN and SCR or CLE. System com-
mands must not be preceded by line numbers.
PROBLEMS FOR CHAP. 1°
1) Define the following items: BASIC, PRINT, END, READ-DATA, LET, RUN,
GOT, statement, system command, program, remote terminal, comma delimiter,
semicolon delimiter, scientific notation, imtialize, and print zone,
2) What is the greatest number of variables permissible in a single BASIC pro-
gram thus far?
3) Which of the following are valid BASIC variables? A, XI, 1B, XA, Y12.
4) The statement was made in Chap. 1 that you cannot have the computer print
quotes by putting quotes inside quotes. Why not?
5) Write a program to add 2081, 682, 1161, and 72.03,
6) Write a program to add 1E6 and 1E — 3. Comment on the result.
7) Have the computer multiply 2E3 by IE — 1
8) Have the computer print a decimal value fo
9) Modify the purchase program on p. 9 to total the number of items
10) Write a program that will print the sum of the first 10 counting numbers.
Put the numbers in as data.
11) Write a program that will print the product of the first 10 counting numbers
J 12) Write a program that will multiply two binomials. In other words, for
(Ax + BY(Cx + D), you will put in data in groups of four numbers (A, B, C, D), and
you want the computer to write out the three numbers that are cocflici
product.
¥ 13) Have the computer print products of fractions by putting the numerators
and denominators in as data and printing the numerator and denominator of the
product as two numbers.
¥ 14) Do the same for adding fractions as in problem 13).
15) Have the computer print all possible atrangements of three digits using each
once. Assign the digits in a DATA line and use semicolon spacing.
16) Write programs to print decimal values for the following:
@ aes
1/3—1/4
(b) 2/3 + 3/4
3/6 273
« 12 +.3/T7 ) (ae
(da)
* Check marks (/) in front of problem numbers indicate the more dif
it problems:CHAPTER 2
Writing a Program
2-1 PLANNING
In Chap. 1 we looked at some programs and tried to analyze them, but
we did not really go into the development of the programs themselves. Pro-
grams do not just happen, they do not appear whole. They are planned and
developed with some considerable care. There are two important tools that we
will be using to help us write programs. One is a new BASIC statement type,
the REM statement. The other is fowcharting.
2-2 REM
XXX REM (REMark), where XXX is a line number in a BASIC program,
notifies the computer that what follows is to be ignored during the RUN of
the program. This means that you may write any message you like following
REM. None of what you type has any effect on the execution of the pro-
gram, but you may comment or remark upon the function of a particular
line or a group of lines or the entire program.
REM
Permits the programmer to remark or comment in the body of his
program.
EXAMPLE
118 REM THE NEXT THREE LINES PRINT THE FIRST SUM.
9 REM THIS PROGRAM ADDS PAIRS OF NUMBERS.
2.3. FLOWCHARTING
Flowcharting, or block diagramming as it is sometimes called, is useful in
planning programs in any computer language or for that matter in planning
the solving of any problem, whether or not you are using a computer. We
1314 Basic BASIC
intsoduce flowcharting by an example. Suppose we want to add the counting
numbers from 1 to 50
for counting and the other to keep track of the sum. We want to start the
counting variable at 1 and the summing variable at 0. Then for every value
of the counting variable we want to add the counting variable to the old
value of the summing variable to get a new value of the summing variable.
Figure 2-1 represents a rough flowchart for such a process.
Figure 2-1 attempts to break the problem into its most fundamental steps.
By using a diagram of this kind, we are able to show the direction we must
follow to do the problem. We would like to have each step smal! enough for
the computer to handle with one BASIC statement. However, this will not
always be practical. In our example, though, it will be both practical and
reasonable to have each step be a BASIC statement. With that in mind we
redraw the diagram using statements more nearly like those in BASIC lan-
guage. At the same time we will introduce the more standard practice of
having different shapes for boxes that indicate different kinds of functions.
‘The shapes used for this example are listed in Fig. 2-2 and the new flow-
chart is Fig. 2-3(A).
‘This time we are very close to being able to write the program directly
from the flowchart. Of the statements in Fig. 2-3(A), the only one for which
we do not yet have a corresponding BASIC language statement is decision-
making. BASIC has a statement type that allows us to alter the path of the
computer through a program depending on whether an algebraic sentence is
true or false.
Start counting variable
Catt
=
Start summing variable
Sato
‘Add summing variable
and counting variable k Paaaeecerereenaa ace)
Have we done no Add 1 to
all 50? counting
variable
Le
Printsum
Fig. 2-1. Diagram for adding counting numbers from 1 to 50.Writing a Program 15
ae Used for beginning and
ending of programs.
Indicates that a computation
isto be performed.
Operation
Shows that a question is being asked
anda decision is being made.
Indicates that results are
PRINT tobe printed and will also
READ be used to indicate the
READ statement.
Small circle shows that we are
going to transfer to another
statement in the program. N
will match another small circle
in the same chart.
Fig. 2-2. First five shapes
Arrowheads will indicate direction in all cases. used for flowcharting
2-4 IF-THEN
XXX IF Z = Q THEN 230 means that if Z does equal Q, then the next
line to be executed is line number 230. If Z does not equal Q, then the com-
puter is directed to simply execute the line with the next number after XXX.
The equals sign appears in the IF~THEN statement. Used here the equals
sign is clearly not the assignment operator we defined earlier. In the IF-THEN
statement the equals sign specifies a required relation (mathematical equality)
to exist between two numbers. Therefore, the equals sign is now designated as
a relational operator.
With the IF-THEN statement added to our growing list of BASIC state-
ments, we should be able to write the program directly from the Sowchart. See
Fig. 2-3(B). If we simply copy the program in Fig. 2-3(B) and run it, it looks
like the program below.
10
20
30
40
50 LET cece
60 GaT2 30
70 PRINT S
30 £ND
Ruy
127516 Basic BASIC
leTC=1 10 ETC =1
LETS=0 20 LETS=0
30 LETS=S+C
40 IF C = 50 THEN 70
LETC =C+1 50 LETC=C+1
60 GATS 30
70 PRINTS
80 END
“A (B)
Fig. 2-3. (A) Flowchart for adding counting numbers 1 to 50. (B) Pro-
gram written from flowchart.
BASIC allows us to give programs names. This requires a system com-
mand and will vary with the system tied in with your terminal. Some systems
use the command NAME-, while others use NAME without the hyphen. After
the system command, you type the name to be used. Being able to name pro-
grams will be helpful to us here as we will be able to refer to programs by
name from now on.
We will give the last program a name, insert some REM statements to
explain the function of certain lines, and add a label to make the printed
result clearer. It is always recommended that you write programs with the
thought that someone else will be reading them and you may not be there to
do the explaining. You may even find that you cannot understand your ownWriting a Program 17
sum
3 REM THE EXECUTIVE PRAGRAM ALLOWS US TO GIVE OUR PROGRAM A
NAME,
9 REM THE RESTRICTIONS ON NAMES VARY FRIM SYSTEM To SYSTEM
B OREM teeee
9 REM WE ARE ADDING INTEGERS ANE THROUGH SO IN THIS PROGRAM
10 Let
20 Ler
30 LET S=s*c
38 REM HAVE WE ADDED SO (THE LAST NUMBER T@ BE ADDED) YET?
40 IF C=SO THEN 70
48 EM WE HAVEN'T ADDED 50 YET ¢#* S9 WE ADD ONE
SO LET C=cel
60 Gata 30
68 REM WHEN C=50 WE PRINT S (THE SUM) IN LINE 70
70 PRINT S
BO END
RUN
SuMI
1275
Done
70 PRINT “THE SUM QF THE INTEGERS FROM ONE To FIFTY 18'S
RUN
Sum
THE SUM OF THE INTEGERS FROM QNE T@ FIFTY IS 1275
Done
programs several weeks after you write them, uuless they have good REM
statements. See SUM1.
Let us do another program, similar to SUMI1, where we will add all the
odd integers from 5 through 1191. This time instead of starting the counting
variable at 1, we will have to start it at 5. Since we are only interested in
odd numbers, we will have to add 2 instead of 1 each time we add the new
number to the summing variable. We will test N (the number added) each
time through the summing step to decide whether we have reached the
desired number, in this case 1191. First we draw the flowchart in Fig, 2-4.
This flowchart is very much like the one in Fig. 2-3(A). See SUM2. Again, of
10 LET N=S
20 LET S=0
28 REM LINE 30 ADDS THE NEW NUMBER T@ THE SUMMING VARIARLF.
30 LET S=S+n
40 IF N=1191 THEN 70
48 REM ADD 2 IN LINE SO FOR @DD NUMBERS
50 LET N=Nee
60 GoTo 30
70 PRINT “SUM OF @0D NUMBERS FROM 5 TO 1191 1S"7S
80 END
RUN
‘SUM2
SUM OF ODD NUMBERS FROM S TO 1191 1S 355212-
pone18 Basic BASIC
~
LETN=5.
LETS =0
=
LETS=S+N HC)
PRINT S
no
LET N=N42 END
Fig. 2-4. Flowchart for adding
odd integers from 5 to 1191
course, we use the IF~THEN statement, because we have to decide each time
we add 2 whether or not we have reached 1191.
The IF-THEN instruction is called a conditional transfer. Unless instructed
otherwise, the computer executes the statements of the program in numerical
order. The IF-THEN statement allows us to tell the computer to alter that
order of execution on the condition that an algebraic sentence is true. If the
algebraic sentence is false, then the computer passes to the next line in
sequence, On the other hand, the G@TP statement is an unconditional transfer.
IF-THEN
XXX IF YYYYYY THEN 22Z. If YYYYYY is true, transfer to line
ZZZ. If YYYYYY is false, pass to the next line after XXX.Writing a Program 19
You may have more than one conditional transfer in the same place in a
program. This would be necessary if you wanted to test for several conditions.
Suppose in SUM2 you want to see the sum several times during the RUN.
Let us look at the sum for the first two, for N = 731, and the last two.
First we should draw a new flowchart. It is clear from the flowchart that we
have to decide each time we print the sum whether or not we have finished
or have to add 2 and take the sum again. See Fig. 2-5 and SUMS.
Note that we test N for three relations: 1) “less than 9,” 2) “equals 731,”
and 3) “greater than 1188.” We have already seen the equals sign used as a
relational operator. The two new relational operators “less than” (<) and
“greater than” (>) are introduced here.
LETS=0
+
LETS=S+N HG)
tervan+24{2)
és) Fig, 2-5. Flowchart for changing program SUM2 so that
the sum is printed several times during the program20 Basic BASIC
sum3
5 PRINT "SUM aF aD0"
6 PRINT “NUMBERS FRO
7 PRINT “FIVE To", "I
10 LET
20 LET
28 REM LINE 30 ADDS THE NEW NUMBER TO THE SUMMING VARIABLE.
30 LET S=S#Nn
40 IF N<9 THEN 90
50 IF N=731 THEN 90
60 IF N>1188 THEN 90
68 REM ADD 2 IN LINE 70 FOR ODD NUMBERS
Bu Ube Ss
90 PRINT Nos.
100 IF Ne1191 THEN 70
110 END
RUN
sum3
SUM OF ODD
NUMBERS FROM
FIVE 13 Is
s 5
7 12
731 133952.
1189 354021.
119th 355212.
Dane
Other relational operators are “less than or equal to” (< =), “greater than
or equal to” (>=), and “not equal to” (<>). Some time-sharing systems
require a set of alphabetic relational operators (such as EQ for =) instead of
the symbols listed above.
Some facts about flowcharts should be becoming clearer. Arrowheads along
connecting lines show the direction the computer is to follow. Rectangles and
parallelograms have only one exit arrow, but they may have more than one
entrance arrow. Diamonds have two exit arrows. Can diamonds have more
than one entrance arrow?
We said previously that we did not know how to eliminate some of the print-
ing in the SUBT@TAL column. Look at the purchase program on p. 9 again.
We had no way of preventing the computer from running out of data. Now we
can simply tack on some artificial data at the end of the DATA line, which
could not possibly be data in the problem, and use the conditional transfer to
test each time data is read to see if the computer has read the artificial data. If
the computer has read the artificial data, then we do not want to use it; but we
have a signal for the computer that it is time to print the total and terminate
the run without reading any more data. Artificial data used in this way is
called dummy data. If we are talking about prices and numbers of items, we
can use 0 or negative numbers for dummy data. Let us use 0 for the number
of items and 0 for the price and name the program TOTAL. See the flowchart
Fig. 2-6.Writing a Program 21
LETT END
=T+NeP
Fig. 2-6. Flowchart for using dummy data in program TOTAL,
TOTAL
5 KEM THIS PROGRAM IS A MODIFICATION OF A PROGRAM THAT WE DID
BEFORE.
10 PRINT “TOTAL COST =s"s
20 DATA 25 435s Bs2+ 65s 1 1p 25s 149+ 494 35 14594040
21 REM THE DUMMY DATA IN THIS DATA LINE IS tot
25 LET Tso
30 READ NsP
34 IF Ne0 THEN 45
40 LET T=T+#P,
42 Gate 30
4S PRINT T
70 END
RUN
TOTAL
ToTAL COST =
16.34
owe
Look at lines 10 and 45 and then look at the printed result. These two
PRINT instructions are executed on the same printed line. This is accomplished.
by using the semicolon at the end of the PRINT instruction in line 10. The
semicolon there tells the computer to wait after printing the $ until it executes
the next PRINT instruction in the program and to print that on the same line
right after the $. Again there is a single space for the plus sign which is not
printed. If the number were negative, there would be a minus sign there.22 Basic BASIC
SUMMARY OF CHAP. 2
1) We are now able to remark about a program right in the program
itself by using REM. You should use REM statements so that whoever reads
your program can determine what they are intended to do. It will also help
you to remember your own programs weeks or months later when you your-
self have forgotten what they will do.
2) Flowcharting will prove a very valuable process that we will use to
develop programs to solve problems.
3) The ability to have the computer m
e decisions, using IF
ions greatly increases the complexity
act according ta the ontcome of the de
of the problems we may solve by computer
4) We now distinguish between conditional and unconditional transfer
statements.
5) Dummy data may be used to gain a greater control over what we can
ask the computer to do after it has read the last item of data.
PROBLEMS FOR CHAP. 2
Unless instructed otherwise, draw a flowchart for every problem that calls for
a computer program to be written. Also use REM liberally,
1) Write a short description of each of the following terms: flowchart, dummy
data, IF-THEN, REM, conditional transfer, unconditional transfer.
2) In the program TOTAL, why did we use two O's for dummy data? Why
couldn't we have used just one 0 since line 34 only tests to see if N — 0?
3) Bill took four tests. His marks were 100, 86, 71, and 92. What was his average
score?
4) Modify the program SUM2 to count the number of odd numbers from 5 to
1191 by first modifying the flowchart.
5) Three pairs of numbers follow in which the first number is the base and the
second number is the altitude of a triangle: 10, 21; 12.5, 8; 289, 114. Write a
program to print in good form the base, the altitude, and the area for the three
triangles.
6) Find the number of and the sum of all positive integers greater than 1000
and less than 2213 divisible by 11
7) Aman is paid 1g the first day on the job, 2¢ the second day, 4¢ the third day,
and so on, doubling each day on the job for 30 days. You are to calculate his wages
‘on the 30th day and his total for the 30 days.
8) Write a program to print the integers from 1 to 25 paired with their reciprocals.
9) Write a program to print the integers from 75 to 100 paired with their
reciprocals
10) Rewrite the program TOTAL to count the number of different items in the
order and print the total.
11) A customer put in an order for four books which retail at $5.95 and carry a
25% discount, three records at $3.98 with a 15% discount, and one record player for
$39.95 on which there is no discount. In addition, thae is a 2% discount allowed
on the total order for prompt payment. Write a program to compute the amount
of the order.
12) Write a program to balance a checkbook that inchides the following transac:
tions: Sept. 2, deposit $9.00; Sept. 5, write a check for $3.24; Sept. 10, write aWriting a Program 23
check for $1.98; and Sept. 17, write a check for $3.85. Assume that the balance was
$14.23 on Sept. 1. Have the computer print the balance after each transaction.
13) Write a program to find the amount of $100.00 deposited for one year in a
savings account at 4% per year compounded four times yearly.
J 14) In the song “The 12 Days of Christmas,” gifts are bestowed upon the singer
in the following pattern: the first day she received a partridge in a pear tree; the
second day two turtle doves and a partridge in a pear tree; the third day three
french hens, two turtle doves, and a partridge in a pear tree. This continues for 12
days. On the 12th day she received 12 + H+.» + 2+ 1 gifts, How many gifts
were there all together?
¥ 15) For problem 14) have the computer print the number of gifts on each of the
12 days and the total up to that day.
V 16) George had test scores of 83, 91, 97, 100, and 89. Write a program to com-
pute his average, Have the computer count how many tests George took,
¥ 17) Write a program that will take more than one set of test scores, find the
average for cach set, and print the result before going back to read the next set of
scores.CHAPTER 3
Loops and Lists
3-1 INTRODUCTION TO MACHINE-MADE LOOPS
A computer loop may be defined as a self-repeating sequence of program
statements. This being true, loops are not new to us. Most of the programs
we wrote in Chap. 2 used a loop. In those programs we initialized a variable
with the idea that we would be adding a fixed number repeatedly and doing
something each time we added the fixed number. Let us draw a flowchart and
write a program to simply print the integers 1 through 6. See LOOP and
Fig. 3-1.
LETX =
PRINT X.
LETX=X 44
24
20
30
40
50
99
LETX=1
PRINT X;
IF X = 6 THEN 99
LETX=X+1
GOT 20
END
3-1. Flowchart for LOGP1 for printing six integers.Loops and Lists 25
Laopt
10 LET x=I
20 PRINT Xs
30 IF X=6 THEN 99
40 LET X=K+1
so GeTe 20
99 END
RUN
Laat
1 2 3 4 5 6
ONE
In LO@P1 we first print the number and then test to see if we have printed
the last number in the sequence. If we have, then we stop. If we have not
printed the last number, then we add 1 and print the new number. The
results we obtain are entirely equivalent to the results we would get when we
test to see if the number we are about to print is too great before we print it.
If it is not too great, then we print it. If it is too great, then we stop. Consider
the flowchart of Fig. 3-2 and LO@P2.
LeT xX
PRINT X
UETX=X41
Fig. 3-2. Flowchart for LO@P2 for testing X before it is printed
Loope
10 LET x=1
20 IF X>6 THEN 99
30 PRINT x3
40 LET X=xe1
50 GaTe 20
99 END
RUN
Leora
1 2 3 a 8 6
DONE26 Basic BASIC
FQOR-NEXT
Loops are used so routinely in programming that BASIC provides a
machine-made loop. Program L@@P3 is the machine equivalent of our pro
gram LGOP2.
Notice that the two statements 10 FOR X = 1 TY 6 and 50 NEXT X in
L@GP3 do the work of the four statements 10, 20, 40, and 50 in LOBP2.
FOR X = 1 T@ 6 indicates doing everything between this statement and
NEXT X, beginning with X = 1 and ending with X = 6. NEXT X tells the
computer to add 1 to the ol begi
again, When X = 6, L@@P3 prints 6. After it ‘prints 6, line 50 says NEXT x.
This means, add 1 and go to the beginning of the loop. At this point in the
RUN the value of X is 7, not 6 as you might think. Since 7 is greater than 6,
the F@R-NEXT combination instructs the computer to execute the next
instruction after NEXT X, which in program LO@P3 is END.
ne of X and go to th
of the loop
Laora
10 FOR X=1 Ta 6
30 PRINT xs
50 NEXT x
99 END
Lara
1 2 3 4 8 ‘
DONE
A machine loop does not have to begin with 1. It may begin wherever you
require. The variable that is incremented in the machine loop may be treated
in the same way as other variables in the program. However, you are warned
against changing the value of that variable. LG@P3+, which is a modification
of LO@P3, prints the values of X,2 ° X, X — 10, X °° 3, and X/(—3).
Loars+
3 PRINT "x BAK X10 X43 Ks E-3)"
10 FOR x=1 13 6
30 PRINT Xs 24K3K-103K132K7 0-39
50 NEXT x
99 END
RUN
Leara+
x 2eK K-10 X13 K/E=3)
1 2 +9 1.333333
2 4-8 8 ++ 666667
3 6 -7 27-1
a a -6 64 =1.33333
5 100 +5 125 1466667
6 120 =a 216-2
DONE
Notice lines 80, 100, 150, 220, 240, and 310 in program LUPDEM. ‘They
are all of the form XXX PRINT. This statement is sometimes called the
blank PRINT. It has the effect of directing the computer to tum up a new