# CDC Community

🔹 Historical Conservation 🔹

### Site Tools

tutor:conditional_commands

# Conditional Commands

It is important to be able to specify the sequencing of a lesson conditionally. We might like to jump past some material on the condition that the student has demonstrated mastery of the concept and needs no further practice. Or we might like to take the student to a remedial sequence conditionally (the condition being poor performance on the present topic). Or, which help sequence we offer might be conditional on the number of times help has been requested. All of these examples imply a need for conditional sequencing or branching statements, where the condition may be specified by calculations involving the status of the student.

The usefulness of conditional branching is not limited to the sequencing of major lesson segments, but extends to many calculational or display situations. For example, we might need to -do- conditionally one of several possible subroutines in the course of presenting a complex display to the student. This chapter will show you how to perform these and similar conditional operations.

Here is an example involving a conditional -do- statement:

unit setup calc N⇐-1 jump home * unit home next home at 2010 do N,neg,uzero,One,utwo at 1215 write N equals ⊀s,N⊁. calc N⇐N+1 * unit neg write Unit "neg". * unit uzero draw 210,260;2060;2010 * unit One circleb 50,0,270 * unit utwo write Unit "two".

The new element is the conditional -do- statement in unit “home”. If N is negative, that statement is equivalent to “do neg”. If N is zero, the statement is equivalent to “do uzero”, and so on. The statement:

do N,neg,uzero,One,utwo

is equivalent to:

do neg \$\$ if N is negative do uzero \$\$ if N is zero do One \$\$ if N is 1 do utwo \$\$ if N is 2 or greater

Note that unit “utwo” will come up repeatedly because it is the last unit named in the conditional -do- statement. The list of unit names can be up to 100 long:

do N,neg,uzero,One,utwo,dispone,zon,zip,figure,ultima

If N is 7 or greater, this statement is equivalent to “do ultima”.

The “conditional expression” (N in this case) can be anything. It can be as complicated as “3x — 5 sqrt(N)” and can even involve assignments as in “N⇐35—x”. The value of the expression is rounded to the nearest integer before choosing a unit from the list of units. If the rounded value is negative, the first unit in the list is chosen. For example, if the expression is -.4, it rounds to zero, in which case the second unit in the list is chosen.

In a conditional -do- each unit named may involve the passing of arguments:

do 3N-4,circ(25,75),box(45),x,flag,circ(10,30) ⇑exp ⇑ neg012≥3

So far we have encountered the following sequencing commands: -do-, -jump-, -next-, -nextl-, -back-, -backl-, -help-, -help I-, -lab-, -labl-, -nextnow-, -data-, -datal-, and -base-. When the tag of such a command is just a single unit name (e.g., in a statement like “help uhelper”), we say it is “unconditional”. To make a “conditional” statement out of any of these, we follow the same rule: state the conditional expression, followed by a list of unit names. So we might have:

do N-5,zonk,q,zap,zing,x ⇑exp⇑neg012≥3

Here, as in unconditional pointer-associated statements, “q” means the “data” pointer is cleared so that the DATA key is disabled. This can be used to cancel the effect of an earlier -data- command in this main unit. (Remember that all the unit pointers are cleared when we start a new main unit.) The unit name “x” has the special meaning “don't do anything!” In the example shown, if the condition (N-5) is three or greater, this -data- command has no effect at all and we “fall through” to the next statement without affecting the “data” pointer. Similarly, if a unit name in the conditional -do- discussed above is replaced by “x”, no unit will be done for the corresponding condition and we “fall through” to the next statement.

This “x” option is extremely useful. Consider the following situation:

jump correct-5,x,done * (then show the next item)

If (correct-5) is negative (that is, the student has made fewer than 5 correct answers), we “fall through” to the presentation of the next item. If, however, the student has 5 or more correct, the condition (correct-5) will be zero or greater and we jump to unit “done”. 