Please send questions to
st10@humboldt.edu .
4 basic [control] structures of programming:
1. sequence
2. branching
3. procedure
4. repetition
...and TODAY we are introducing repetition!
* in the 1970's, two Italian mathematicians proved that
we can specify any problem solution statement using
only these control structures
* when you are at the body step of the design recipe,
and you see multiple steps --- figure out LOGICALLY
what these are before trying to code them;
... you might draw them in a flowchart (graphically),
you might write them out in PSEUDOCODE
* for OUR purposes, pseudocode is WHAT we want
to do, in the ORDER we want to do it, written in
whatever combo of English/code makes sense to us
* for diff_profits1: here is pseudocode!
print out a header
print out all the profits for ticket prices from $4 to $5
in dime increments
* HANDY PICO TIP: ctrl-w then ctrl-t then line number then enter
... takes you STRAIGHT to that line number
* now, to redo diff_profits1 from $1 to $7? UGH!
* so, REPETITION.
* one of SEVERAL statements in Python for repetition
is the CLASSIC while-loop
* while bool_expr:
action1
action2
...
* DRAW FLOW CHART from BOARD in your NOTES!
* 1. when the while is reached,
2. evaluate bool_expr
3. if it is true, do action1, action2, ...
4. go to 2
* SO --- diff_profits2.py will now use a loop!
* NOTE: almost every loop needs at least one
LOOP CONTROL VARIABLE --- its contents affect
when the loop ends...
* NOTE: 4 most common loop errors:
* go one time too many
* go one time too few
* never enter the loop
* never leave the loop (infinite loop!)
* WHAT IF... you get an infinite loop?
1. type ctrl-c to try to send a keyboard interrupt to
TERMINATE the loop
2. open another cs-server session.
at the cs-server prompt, type:
cs-server> ps -ef | grep your_user_name
LOOK for the python process (name is on the right)
GRAB its process id from the 2nd column
AT the cs-server prompt, kill it as follows:
cs-server> kill that_id
* some loops are unique --- others follow a few common
styles
* count-controlled loop
* event-controlled loop
* sentinel-controlled loop
* count-controlled loop
* you want to do something a specified number of times
* 1st: you set up a counter variable to 0
* 2nd: you loop while the counter is less than its limit
* IN the loop, you increment the counter
* spam1.py