Radio Shack TRS-80 User Manual page 28

Micro computer system
Hide thumbs Also See for TRS-80:
Table of Contents

Advertisement

Sample Run:
25
DEGREES CELSIUS
=
77
DEGREES FAHRENHEIT.
First
notice that
line
40
consists
of a
PRINT
statement-
followed
by
four
separate expres-
sions
two
variables
and
two
groups
of
words
in
quotes
called "literals"
or
"strings".
Next,
note
how
the parentheses
are
placed
in line
30.
With
the 9/5 secure
inside,
we
can
multiply
its
quotient times C, then
add
32,
Now,
remove
the parentheses
in line
30 and
RUN
again.
The
answer
comes
out
the
same.
Why?
1.
On
the
first
pass,
the
Computer
started
by
solving
all
problems
within parentheses,
in
this
case just
one
(9/5).
It
came up
with
(but did
not
print) 1.8.
It
then
multiplied the
1.8 times the value
of
C
and added
32.
2.
On
our next
try,
without
the parentheses, the
Computer
simply
moved
from
left
to
right
performing
first
the
division
problem
(9
divided
by
5),
then
the multiplication
problem
(1.8
times
C),
then the
addition
problem
(adding
32).
The
parentheses
really
made
no
difference
in
our
first
example.
Next, change
+32
to
32+ and
move
it
to
the front of the
equation
in line
30.
Run
it
again,
without
parentheses.
Did
it
make
a
difference
in
the
answer?
Why
not?
Answer: Execution proceeds from
left
to
right,
multiplication
and
division
first,
then
returns
and performs
addition
and
subtraction.
This
is
why
the
32
was
not
added
to
the 9
before heing divided
by
5.
Very important!
If
they
had been
added,
we
would
of course
have
gotten the
wrong
answer.
EXERCISE
5-1
:
Write
and
run
a
program which
converts 65°
Fahrenheit
to
Celsius.
The
rule
tells
us that "Celsius
temperature
is
equal
to five-ninths
times what's
left
after
32°
is
sub-
tracted
from
the
Fahrenheit temperature."
C° =
(F°-32)X
5.
9
Remember
what
the semi-eol«ns
are for?
26

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents