Unit 4.3 -- If Example:  Max of Three Numbers

PROG

Take the maximum of three numbers

PED

Illustrate conversion of nested If's

CONCEPTS

Nested  and multiple If's are handle in the obvious  manner.
Obviously,  numbering of labels generated in the  conversion
process must be kept unique for each if statement converted.

SF

None

6.html


         Unit 4.3, If Example: Max of Three Numbers

This example illustrates how we nest if's.  You will  recall
in unit 9, we indicated that when we see PASCAL like,

IF var1 ro var2 then
  statements 1
else
  statements2

"statements 1" and "statement 2" get translated and  put  in
the  appropriate  places  in the template.   Our  first  two
examples  all illustrated cases where the statements  to  be
translated  were assignment statements.  We just  translated
them  into the appropriate sequence of loads, stores,  adds,
etc.  and  put  them  in the appropriate place  between  two
branches.

However,   "statements1"   and   "statements2"   could    be
if-then-else sequences, themselves.  We translate the  inner
if-then-else as discussed in units 9 and 10.   We  just  put
the   result   in  between  the  branches   of   the   outer
if-then-else.   We must remember to use unique  label  names
(Innn,FInnn,  etc.)  between all if-then-else's  translated.
In other words, if the outer if-then-else had I001 and FI001
as  labels, then an inner if-then-else should use  I002  and
FI002.

The  example  to  illustrate  takes  the  maximum  of  three
numbers.   It  is  worthwhile to review the  logic  of  this
program.

There are three numbers, stored in A,B and C.  At the end of
the  program, it is desired that MAX contain the maximum  of
A,B and C--max(A,B,C).

If  A<B, that means that A is not the maximum.  Thus we have
to  take the maximum of the two remaining quantitites, B and
C.   Thus  the outer if compares A and B.  In the then  part
where A<B, the inner-if takes the maximum of B and C.

On  the  other hand, if A>B, that means that B  is  not  the
maximum.   Thus,  in  the else part  of  the  outer-if,  the
inner-if takes the maximum of A and C as B was eliminated.

Don't  worry about cases where numbers are equal.   In  this
case, it doesn't matter which branch we take as when A and B
are  equal,  either  value can be correctly  placed  in  the
variable MAXIMUM.

Looking at our example, we see that if the comparison,  A<B,
fails we branch to I001.  Between this branch and "I001  EQU
*"  is  the  code to take the maximum of B and C, the  first
inner IF.

The  B<C comparison is at lines 22 to 24.  C is put into MAX
at  line 26 and 27. The else part of the inner if where  MAX
is  assigned  the value B is at line 29 to  31.   When  this
inner IF is finished, it reaches FI002 at line 32.  Then the
B  FI0002 of the outer IF's ELSE part takes the execution to
the end of the outer IF.

The  ELSE part of the outer IF is from line 34 to  45.   The
comparison  of A with C is at line 35 to 37.   A  is  stored
into MAX at lines 39 to 40 and the ELSE for the second inner
IF  is  at line 41 to 42.  The final MAX:=C is converted  at
lines  43 to 44.  The end of the second inner if is at  line
45  and we just fall through to the end of the outer  IF  at
line 46.