Class Crib Notes for CPSC 110 Brent Dingle
Friday, February 15, 2002
Reminders:
Start working on Lab Assign 4, Next quiz should be
shorter – really =)
Review:
So
far have seen while-do, repeat-until and for loops using numbers. And succ()
and pred()
We
will see loops based on characters.
We
will see that loops can iterate on any type that has order and can be counted
with integers.
We
may see some nested loops.
Structure
of each loop type
Loops
using chars as control
Note
while and repeat can use complex Boolean expressions
Simple
Example of each type – not overlapping, but illustrate when to use each type.
For
loops usage:
I
know I have N items and I want to do the same thing to all of them.
Say
I have 10 baseball cards I want to sell. They each already are priced, but no
one will buy them, because they are too expensive. So I want to decrease the
price of all of them by 10%.
For
i := 1 to 10 do
Begin
reduce baseball card number i’s price by
10%.
End
A
while loop can be used more diversely – notice it might not always need to
execute.
Consider
that someone gives you a list of people’s names (which might have zero to an
unknown number of names on it) and asks you to find out if Johnny Bravo is on
the list.
cur_name
:= ‘nothing’;
While
( (not at end of list) and (cur_name <> ’Johnny Bravo’) do
Begin
set cur_name = next name on list
End
A
repeat loop always gets done at least once and can also use complex Boolean
expressions.
Let’s
consider a guy’s mentality on getting to go out on a date:
Say
he likes a girl name Betty, he will try to ask her out at least once no matter
what.
repeat
ask Betty out
until
(Betty gets a restraining order)
OR (Betty says yes)
Some
definitions:
Assertions
– pre and postconditions
Invariants
– stuff that does not change (value) when a loop is executing
Variants
– stuff that does change when a loop is executing
Beginning
of top-down design