Answer:

If you focus on just the innermost nested IF, the answer will be:

    IF AGE < 65  THEN
      LET RATE = 1.0
    ELSE
      LET RATE = 0.75
    END IF

Branches within Branches

When program statements are nested three levels down, as in this program, it is hard to see what is controlling what. To figure this out, match "brackets" as was done in a previous chapter.

Here is the program with boxes around the true branch and the false branch of the first IF. Notice how the false branch contains a complete control structure.

PRINT "Enter full fare"
INPUT FARE
PRINT "Enter age"
INPUT AGE
'
'Calculate discount based on age
IF AGE < 12 THEN
+------------------------------------------------+
| LET RATE = 0.0                                 |
+------------------------------------------------+

ELSE
+------------------------------------------------+
| IF AGE  < 24 THEN                              |                           
|   LET RATE = 0.70                              |
|                                                |
| ELSE                                           |
|                                                |
|   IF  AGE < 65 THEN                            |
|     LET RATE = 1.0                             |
|   ELSE                                         |
|                                                |
|     LET RATE = 0.75                            |
|                                                |
|   END IF                                       |
|                                                |
|                                                |
| END IF                                         |
+------------------------------------------------+

END IF

'Calculate fare
PRINT "Your fare is:", FARE * RATE
END

Remember that when a false (or true) branch is chosen that the entire branch is chosen. The chosen branch acts like a tiny program-within-a-program to do its job.

QUESTION 7:

The user has just typed in $300 for FARE and 77 for AGE. Which branch (true or false) of the first IF is chosen?