Class Crib Notes for CPSC 110 Brent Dingle
Friday, February 8, 2002
Reminders:
Quiz #2 in class, February 13th
Review:
Understand
Multiway branching à if – else if – else
Understand
CASE statement
Nested
ifs, EXAMPLE 1:
Say we want to price cars made
before 1980 as follows
Red car price =
$1500
Blue car price =
$1000
other color
price = $500
Pseudo
Code (Pascal and English mixed together)
IF (car built
before 1980) THEN
Begin
IF
(Car is red) THEN
Begin
set
price = 1500
End
ELSE
IF (Car is blue THEN
Begin
set
price = 1000
End
ELSE
Begin
set
price = 500
End
END
Notice
the above only works for 1 car, we could modify it so it checks all cars by
putting a while loop around the above pseudo code, e.g:
WHILE (not
all cars checked) DO
Begin
do
the nested if above
set
car = next car
End
Nested
ifs, EXAMPLE 2:
Calculate
change example of nested-nested ifs (slight variation from what is written in
ppt files)
Problem
Stmt:
Assume a user entered an amount of cents and we have
calculated how many quarters, nickels, dimes and pennies are needed to arrive
at the amount.
Assume we want the output to be in the format:
If input was 25 cents then output would be:
1 quarter(s)
If input was 37 cents then
output would be
1 quarter(s), 1 dime(s), and 2 pennies
If input was 35 cents then
output would be
1 quarter(s), and 1 dime(s)
And
so on for other amounts
Our concentration will be on getting the commas and
the “and” placed correctly.
We want to do this task in such a way that it will
illustrate the use of nested ifs
Here
is the code we came up with in class:
PROGRAM
whatever;
VAR
quarters, nickels, dimes,
pennies : integer;
BEGIN
{ ask user for number of
cents = 1 to 99 }
some code goes here
{ calculate and set quarters and
dimes and nickels and pennies appropriately }
some code goes here
{ output the results as
described above – getting the commas and “and” placed correctly }
IF
(quarter <> 0) THEN
Begin
Write(quarters,
‘quarter(s)’);
End;
IF
(dimes <> 0 ) THEN
Begin
IF
(quarters <> 0) THEN
Begin
Write(‘,
’); { write a comma and a space }
IF
((nickels = 0) AND (pennies = 0))
Begin
Write(‘and
’); { write an “and” and a space
}
End;
End;
Write(dimes,
‘dimes(s)’);
End;
IF (nickels
<> 0) THEN
Begin
IF
((quarters <> 0) OR (dimes <> 0)) THEN
Begin
Write(‘,
’);
IF
(pennies = 0)
Begin
Write(‘and
’);
End;
End;
Write(nickels,
‘nickel(s)’);
End;
IF (pennies
<> 0) THEN
Begin
IF
((quarters <> 0) OR (dimes <> 0) OR (nickels <>0)) THEN
Begin
Write(‘,
and ’); { since nothing can come
after pennies and is automatic }
End;
Write(pennies,
‘pennies’);
End;
Writeln; { finish off the line with a CRLF }
END.
The
green IF’s are outer-ifs.
The
red and blue IF’s are inner-ifs of the green (they are inside the green ifs)
The
blue IF’s are inner-ifs of the red (the blue ifs are inside the red ifs)
The
green and red ifs make up a nested if statement.
The
green, red and blue ifs make up a nested if statement.
You
may nest while loops in a similar fashion.
You will see something related to the above when you get to
assignment number 5.
We
will begin talking about the types of loops: