User Tools

Site Tools


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:

unit somethin branch count-4,5,x,8after at 1215 write "count" is equal to 4 5 do countit 8after count=15

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:

doto 8end,i⇐first,last,incr calc a⇐b×sin(5i°) at 100,200+2a-i write T Bend circle 100

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:

-do- do column,i⇐1,3 unit column do rows,j⇐1,20 unit rows at 820+10i+100j show scores(i,j) -doto- doto 4,i⇐1,3 doto 4,j⇐1,20 at 820+10i+100j show scores(i,j) 4

This nested -doto- example has the structure:

doto 4 ----+ | doto 4 --+ | | | 4 ---------+-+

Other possible structures include the following:

doto 8 ----+ | doto 5 --+ | | | 5 ---------+ | | 8 -----------+

doto 8 ------+ | doto 5 ----+ | | | doto 3 --+ | | | | | 3 ---------+ | | | | 5 -----------+ | | 8 -------------+

doto 8 ----+ | doto 3 --+ | | | 3 ---------+ | | doto 5 --+ | | | 5 ---------+ | | 8 -----------+

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:

doto 5 --+ | doto 8 --|-+ | | THIS IS ILLEGAL! 5 ---------+ | | 8 -----------+

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.

Array Operations

plato/tutor/branching_within_unit.txt · Last modified: 2023/08/05 18:55 by Site Administrator