When circumstances allow, I'll be typing up bits of my lecture notes and posting them online. These may or may not bear any resemblance to the actual lectures.
One tool commonly used by programmers is called a flowchart. Using a flowchart, you can diagram what choices the computer has at each decision point (if or ifelse statement). A flowchart can include as much or as little detail as you need.
Below is an example of a simple flowchart describing the process of waking up in the morning:Notice that actions are described in rectangular boxes, decisions appear in diamond shaped boxes, and the start and end are in circles. Arrows indicate the "flow of control" in the "program". (I don't know what the green bullet shape is for.) In the second flowchart presented in this Wikipedia article, you can see that events in which the computer displays something are indicated by parallelogram shaped boxes. You can think of this as a rough correspondence between rectangles and operations, parallelograms and commands, and diamonds and predicates.
Here is a flow chart describing one possible way to write a maze game. See if you can write a maze game that prints the responses shown in the parallelograms of the flow chart and makes decisions as described in the damonds.
There is more than one way to write this maze game. Did you use two procedures or one? Did you use if or ifelse? One way to program according to a flow chart is to use an ifelse statement for every diamond in the flow chart. In this case each branch out of the diamond becomes one input to ifelse. Each of these inputs to ifelse contain all the Logo commands described in the tree up to the point where the branches merge. (In our example, this is right before the end of the program.) Notice that this leads to "nested" ifelse statements.
to maze1
print [You are lost in a dark cave. Will you go left or right?]
ifelse (equalp readword "right)~
[print [You fell in a pit!]]~
[print [You see a light ahead. Will you go toward or away?]
ifelse (equalp readword "toward)~
[print [You are saved!]]~
[print [You are lost!]]
]
end
Another way to program this maze game would be use an if
statement and stop the procedure when the game ends:
to maze2
print [You are lost in a dark cave. Will you go left or right?]
if (equalp readword "right)
[print [You fell in a pit!] stop]
print [You see a light ahead. Will you go toward or away?]
if (equalp readword "toward)~
[print [You are saved!] stop]
print [You are lost!]
end
The second method looks easier than the first, but procedures that
use stop make big programs a little harder to read. The real
drawback of the first procedure is that putting one ifelse
inside the other makes it hard to read. We can get around this by
using a helper procedure:
to maze3
print [You are lost in a dark cave. Will you go left or right?]
ifelse (equalp readword "right)~
[print [You fell in a pit!]]~
[seelight]
end
to seelight
print [You see a light ahead. Will you go toward or away?]
ifelse (equalp readword "toward)~
[print [You are saved!]]~
[print [You are lost!]]
end
Procedure maze3 is very easy to read -- if you go right, you
fall in a pit. If you do anything else, the program follows the
insructions given in seelight. By moving the second input
to ifelse into its own procedure, we've made the parent
procedure much easier to read.Of course we could also write a separate procedure that prints You fell in a pit! In this example that seems unnecessary, but if the game offered multiple opportunities to fall in a pit that might be a useful helper procedure to write.
See if you can write a procedure that behaves as described in the flow chart.