Branching Within a Unit: -branch- and -doto-
All of the branching or sequencing commands discussed so far referred to -unit-s (or -entry-s). It is often convenient to be able to branch within a unit, which is possible with the -branch- command:
The tag of the -branch- command is like the tag of a -goto-, except that unit names are replaced by “statement labels.” These labels appear at the beginning of statements and must start with a number (0 through 9) to distinguish them from commands, which start with letters. A statement beginning with a label need not have any tag (as in the line above labeled “5”), but it can have a tag like that of a -calc-, as in the last statement above (“8after count 15”). In fact, a labeled statement is essentially a -calc- statement. As with -goto-, “x” in a -branch- means “fall through” to the next statement.
It is not permissible in a unit to label two statements with the same label (nor can you have two units with the same name in a lesson). On the other hand, since -branch- operates only within a unit and cannot refer to labels in other units, it is all right to use the same label in different units. (Similarly, you can use the same unit name in different lessons.) Note that -entry- is similar to -unit-, so -branch- cannot be used to branch to a label if an -entry- command intervenes.
It is often convenient to use -branch- rather than -goto-. In addition, -branch- requires less computer processing than -goto-, so that heavily computational iterations are better done with -branch- where possible. Generally speaking, about the only time you must consider the computational efficiency of one TUTOR technique compared with another is when you do a large number of iterations of some process. Unless you are making many passes through the same statements, merely write your TUTOR statements in what seems to be the simplest and most readable manner. It is a mistake to spend time worrying about questions of efficiency if the student will make only one pass through the statements.
Just as -branch- is a fast -goto- within a unit, there is a fast -doto- (analogous to the iterative -do-) for use within a unit:
The tag of the -doto- is similar to an iterative -do-, but instead of naming a unit to be done repeatedly you name a statement label. For each iteration TUTOR executes statements from the -doto- down to the named statement label. After the last iteration is performed, TUTOR proceeds to the statement which follows the -doto- label (-circle- in the above example).
Just as it is possible to have nested -do- iterations, it is also possible to have nested -doto-s. Here is a comparison of -do- and -doto- for displaying a two-dimensional array:
This nested -doto- example has the structure:
Other possible structures include the following:
Note that in each case the “inner” -doto-s are nested within the “outer” -doto-s. Here is a counter-example of a structure which is not permissible:
When do you use -doto- instead of an iterative -do-? Use -dotowhenever the contents of the loop are very short, because the “overhead” associated with each -doto- iteration is much less than the “overhead” associated with each -do- iteration. This is due to the extra manipulation involved in getting to the “done” unit. If the contents of the loop are long, the overhead becomes insignificant, and either -do- or -doto- can be used, whichever you prefer or whichever is more readable.