Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
At a club door the guard asks the age first. A person of 18 or more is then asked for a ticket. Write the decision with a nested if, so that the result is one of three messages: Welcome in., You need a ticket. or You are too young. Then write the table of the four cases.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. The ticket question belongs inside the age question.
The four cases.
| Age | Ticket? | Output |
|---|---|---|
| 20 | yes | Welcome in. |
| 20 | no | You need a ticket. |
| 15 | yes | You are too young. |
| 15 | no | You are too young. |
Check. The inner else lines up with the inner if, and the outer else lines up with the outer if. A young person never gets the ticket question.
Answer: The program above. The last two rows give the same message, because the age decides first.
Log in to join the discussion.
Loading…