# CDC Community

๐น Historical Conservation ๐น

### Site Tools

tutor:if_else_commands

# The -if- and -else- Commands

Suppose you want to do one set of statements if x is greater than y, and a different set of statements. One way to do this, as we have seen, is to put the two sets of statements in two different units and write โdo x>y, unita, unitbโ. Another way to perform these operations is to use -if- and -else- commands:

The statements between the -if- and -else- commands are performed only if x is greater than y, and the statements between the -else- and -endifcommands are performed otherwise. The tag of the -if- command must be a logical expression (one that has values -1 or 0). The tag of the -elsecommand must be blank. The -endif- command identifies the end of the sequence.

Note that the statements bracketed by -if-, -else-, and -endif- must be indented, with an initial period identifying them as indented statements. (It is possible that the details of this indenting format may change. Consult on-line PLATO aids for up-to-date information.)

When do you use a conditional -do-, and when do you use -if- and -else-? This depends mainly on the number of statements involved. If there are few statements to be performed, -if- and -else- is probably more readable. But, if โunitaโ and โunitbโ are long subroutines, the conditional -do- is the more convenient form.

There doesn't have to be an -else-:

if x>y . calc Zโ5y . draw x,Z;x+100,Z+100 endif

This will do the -talc- and -draw- only if x is greater than y.

There is also an -elseif- for specifying an additional condition:

if x>y . calc Zโ5y . draw x,Z;x+100,Z+100 elseif x>.5y . at 1225 . write This paragraph will be . displayed only if x is . not greater than y but . is greater than .5y. else . at 1225 . write x is less than .5y! endif

if a=b \$or\$ b>3 . calc xโb+2 . if count<8 . . at 2513 . . write Two levels! . else . . do subr . endif else . at 912 . show x endif

The text โTwo levels!โ will appear on the screen if (a=b \$or\$ b>3) and if (count<8).