QTERM-R55 User's Manual
11
QSI Corporation Fax 801-466-8792 Web www.qsicorp.com Phone 801-466-8770
label
sorry
is chosen whenever the program cannot con-
vert the given number.
Note that the
on
construct can also be used as
on goto
.
2.5.6
End of Program
The
end
statement ends your program immediately (see
Example 9 below). Note that this will cause the QTERM-
R55 to enter download mode and wait for a new application
program to be sent via the serial port.
2.6
Loops
Example 7:
input "Please enter a word" a$
for a=len(a$) to 1 step -1
print mid$(a$,a,1);
next a print " is ",a$," reversed !"
If you try this program, you will get this output:
Please enter a word: hello
olleh is hello reversed !
In the above program, everything from
for
to
next
is
repeated, while the variable (
a
) goes from its initial value
len(a$)
to its final value
1
.
Note the
step
clause. The number after
step
(here:
-1
)
is added to
a
after every repetition. The
step
clause
makes
a
go down with every iteration. If the step clause 3
is omitted,
step 1
is assumed.
Within the
for
-
next
loop above the string functions,
len()
and
mid$()
are applied (see section 2.4).
2.7
Data and Arrays
Occassionally the need arises to supply a program with ini-
tial data. Example 8 program converts numbers to strings.
Example 8:
restore names
read maxnum
dim names$(maxnum)
for a=1 to maxnum:read names$(a):next a
label loop
input "Please enter a number: " num
num=int(num)
if (num>=1 and num<=maxnum) then
print num,"=",names$(num)
goto loop
endif
print "Sorry, can't convert ",num
label names
data 9,"one","two","three"
data "four", "five","six"
data "seven","eight","nine"
The following program (Example 9) produces similar out-
put but is written using totally different language con-
structs.
Example 9:
label loop
input "Please enter a number: " number
number=int(number)
on 1 gosub sorry,one,two,three,
four,five,sorry
goto loop
label sorry
print "Sorry, can't convert ",number:end
label one:print "1=one":return
label two:print "2=two":return
label three:print "3=three":return
label four:print "4=four":return
label five:print "5=five":return
These programs produces the following output:
Please enter a number: 2
2=two
Please enter a number: 3
3=three
Please enter a number: 12
Sorry, can't convert 12
2.7.1
Reading Data
Example 8 program converts numbers to their textual repre-
sentation. For this purpose it needs to know the numbers
from 1 to 9 as text. This information is stored with the
data
lines at the bottom of the program.
The program uses the
read
command to get one piece of
data after the other.
If you want to deviate from the linear ordering while read-
ing the
data
statements, you may use the
restore
state-
ment. In the example above,
restore names
makes
sure that the next
read
statement reads its data after the
label
names
.
2.7.2
Arrays
In Example 8, the words
"one" ... "nine"
are stored
within the string array
names$()
.