Professional Documents
Culture Documents
An Introduction to
Programming
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
Input Songs
Write DollarPrice
void main(void)
2.
3.
int Songs;
4.
float DollarPrice;
5.
6.
7.
8.
9.
return;
10. }
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
Data Input
Input
opera=ons
get
data
into
the
programs
A
user
is
prompted
for
the
data
to
be
entered
This
text
uses
the
word
Write
to
indicate
a
prompt
for
input
The
word
Input
indicates
that
a
user
has
entered
a
value
Example:
Write Enter the number of songs you wish to
purchase today.
Input Songs
Other
types
of
input
can
be
from
a
le,
dragged
by
mouse,
and
more
Input Prompts
A
prompt
indicates
to
the
user
that
data
should
be
input.
To
get
the
user
to
enter
one
number:
Write Enter a number:
prompts
the
user
to
enter
a
number
Input Number
stores
the
number
in
the
variable,
Number
Naming Variables
All
variable
names
must
be
one
word
Spaces
are
never
allowed
Variables
cannot
begin
with
a
number
Names
should
be
meaningful
Long
names
are
allowed
but
names
should
be
as
short
as
possible,
yet
sAll
be
meaningful
Miles_traveled is
ne
Miles Traveled is
not
(space)
TaxRate_1
is
ne
1_TaxRate
is
not
(begins
with
a
number)
Variable1
is
ne
but
not
meaningful
Z
is
ne
but
not
meaningful
My Number
2_4_6_8_go
CowWhoJumpedOverTheMoon
#
Try It
Suppose
a
program
is
to
calculate
the
nal
(maturity)
value
of
an
investment.
You
will
be
given
the
amount
invested,
the
rate
of
interest,
and
the
length
of
Ame
that
the
money
is
invested.
a.
b.
c.
Give
Write
and
Input
statements
that
prompt
for
and
input
the
data
for
this
problem.
Write DollarPrice
This
output
statement
will
output
the
value
of
DollarPrice
to
the
screen.
+
-
AddiAon
SubtracAon
2 + 3 =
7 3 =
5
4
MulAplicaAon 5 * 4 =
20
Division
ExponenAaAon 2^3
Modulus
12/3
14 % 3 =
The
modulus
operator
has
many
uses
which
are
not
immediately
obvious.
For
example,
to
test
if
a
number
is
odd
or
even,
if
X
is
any
integer:
If
X % 2 = 1,
the
number
is
odd
If
X % 2 = 0,
the
number
is
even
Hierarchy of OperaOons
o
First:
perform
operaAons
inside
parentheses
(from
inside
out
if
more
than
one)
o
Second:
perform
exponenAaAon
o
Third:
do
mulAplicaAons,
divisions,
and
modulus
from
le\
to
right
(if
there
are
more
than
one)
o
Fourth:
do
addiAons
and
subtracAons
from
le\
to
right
(if
there
are
more
than
one)
= 3*8/12+2^2*3
^ next:
= 3*8/12+4*3
3*6+2/12+(75)^(2*3) = ?
( ) rst:
= 3*6+2/12+2^6
^ next:
= 3*6+2/12+64
Division next:
= 2+4*3
AddiAon last: = 14
Final + last:
= 82.167
Data Output
Data
that
is
sent
from
the
program
to
the
screen,
printer,
or
to
a
le
is
output.
In
pseudocode,
the
following
statement
will
display
the
value
of
the
variable
to
the
screen
and
send
the
cursor
to
the
next
line:
Write DollarPrice
If
DollarPrice
contains
the
value
9.90,
the
output
on
the
screen
will
be:
9.90
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
FormaRng Output
In
a
program
the
following
two
statements:
DollarPrice + dollars.
Note
that
the
text
inside
the
is
output
to
the
user
as
is,
and
it
is
the
value
of
the
variable
that
is
output.
FormaRng Output
Be
careful
to
format
your
output
so
there
are
appropriate
spaces.
If
Number1
is
a
variable
with
the
value
of
8,
Number2
is
a
variable
with
the
value
of
6,
and
Sum
is
a
variable
that
holds
the
value
of
Number1
+
Number2:
The
statement:
ConcatenaOon
If
a
variable
is
of
String
or
Character
type,
you
cannot
perform
any
math
operaAons
on
it.
However,
you
can
join
(concatenate)
two
Characters
or
Strings:
Given:
First,
Last,
and
Full
are
three
String
variables
Given:
First = Lizzy and
Last = Duck
Then
the
statement
Set Full = First + + Last
will
assign
the
value
Lizzy Duck to
Full
Note:
The
space
enclosed
in
quotes
is
necessary
to
put
a
space
between
the
rst
and
last
name
when
stored
in
the
variable
Full.
Integer Data
Integers
are
all
the
posiAve,
negaAve,
or
zero
whole
numbers
Examples:
8
is
an
integer
8.3
is
not
an
integer
-24,567,890 is
an
integer
0
is
an
integer
4.0
is
not
an
integer
because
it
has
a
decimal
part
Naming ConvenOons
Some
programmers
use
specic
convenAons
when
naming
variables.
These
are
almost
always
simply
a
maner
of
preference.
CamelBack
notaAon
examples:
Boolean Data
Named
for
George
Boole
who
developed
the
algebra
of
logic,
basic
to
the