Sequencing, Selection, and Repetition
This lesson reads algorithms written as numbered steps in English and as flowcharts. There's no Java in it yet. One algorithm runs through every section: a printer working through a tray of paper, twelve sheets per job.
Sequencing
The first version of the algorithm is a plain sequence.
Step 1: Set sheets to 50.
Step 2: Subtract 12 from sheets.
Step 3: Display sheets.
Trace it with one column for the variable. After Step 1 it holds 50, and after Step 2 it holds 38, so Step 3 displays 38. Step 2 doesn't add a second value beside the first. It replaces 50 with 38, since a variable holds one value at a time.
Selection
Now the printer warns when the tray runs low. The decision is written with If, and Otherwise introduces what happens when the answer is false.
Step 1: Set sheets to 50.
Step 2: Subtract 12 from sheets.
Step 3: If sheets is less than 40, display "Low paper".
Otherwise, display "Ready".
Step 3 reads the value sheets holds when that step is reached. That value is 38, not the 50 the algorithm started with. Since 38 is less than 40, "Low paper" is displayed and "Ready" is not. Starting the tray at 60 instead leaves 48 after Step 2, the answer is false, and only "Ready" is displayed. Exactly one of the two actions runs.
Repetition
To keep printing while a full job's worth of paper remains, the algorithm needs repetition. In numbered steps it's written as a step that sends the algorithm back to an earlier one.
Step 1: Set sheets to 50.
Step 2: Subtract 12 from sheets.
Step 3: Go back to Step 2 if sheets is 12 or more.
Step 4: Display sheets.
Step 2 brings sheets to 38. Since 38 is 12 or more, the algorithm goes back to Step 2, and sheets becomes 26. The same thing happens at 26 and then at 14, which leaves 2. Now the question is false, so the algorithm continues to Step 4 and displays 2, after printing four jobs.
Step 3 asks whether enough paper is left for another job, so the condition has to say 12 or more. With "if sheets is greater than 0" ending Step 3 instead, the answer at 2 is still true. The algorithm goes back, subtracts 12 from a tray holding 2 sheets, and displays -10.
Flowcharts
An algorithm can also be drawn.
The shapes used in this course are these.
(START) (END) where the algorithm begins and ends
[ action ] one step
[ question ? ] a decision; two arrows leave it, Yes and No
| v the box that comes next
back to A a return to the box labeled A
Here is the printer drawn as a flowchart.
(START)
|
v
[ set sheets to 50 ]
|
v
[ A: is sheets at least 12 ? ]---- No ----> [ display sheets ] ----> (END)
|
Yes
|
v
[ subtract 12 from sheets ]
|
v
back to A
At 50 the answer is Yes, so 12 sheets are subtracted and the arrow returns to A. The answer is Yes again at 38, 26, and 14. At 2 it's No, and 2 is displayed. The numbered steps and the flowchart are written in different ways, and for a tray of 50 sheets they accomplish the same task.
The order of the steps
The two versions don't contain quite the same algorithm. The numbered steps subtract and then ask, while the flowchart asks and then subtracts. A tray of 50 sheets hides the difference, because both versions print four jobs and display 2.
A tray of 5 sheets shows it. The numbered steps subtract 12 before any question is asked, so sheets becomes -7. Since -7 is less than 12, the algorithm displays -7 after printing a job it had no paper for. The flowchart asks first, gets No at 5, and displays 5 without printing anything.
Both versions use the same operations in a different arrangement. Where the decision sits relative to the subtraction is part of what an algorithm does.