Enter the price: 100 Item cost: 100 Tax: 5.0 Total: 105.0
You might wonder about the restriction that
an if
statement makes only two-way decisions.
Surely you must sometimes pick from more than just two branches?
We ran into this problem with a previous example program that divided integers into negative and positive. It really should pick one of three choices:
... -3 -2 -1
— negative 0
— neither +1 +2 +3 ...
— positive Two-way decisions can do this. First divide the integers into two groups (using a two-way decision):
... -3 -2 -1
— negative 0 +1 +2 +3 ...
— zero and positive Then further further divide the second group (by using another two-way decision):
... -3 -2 -1
— negative 0
— neither +1 +2 +3 ...
— positive By repeatedly splitting groups into two subgroups, you can split a collection into any number of fine divisions.
How could you divide a group of people into: