Ticket Sales
Summary
When selecting between different sets of code to execute, sometimes the decision is more complicated than "Do A or do B". The if statement can be used within another if statement (called 'nesting'). In this example a program will make a decision on whether a purchaser of a gig ticket can get a discount or not. Only patrons younger than 18 can get a discount, and discounts are only available when more than 100 tickets remain unsold.
Task
- Create a new console application
- Within "main" type in the following code
Note that every time we start a new block of code, the statements inside are indented by one tab or four spaces. Also see how the brackets line up vertically. This makes the logic and structure of the program much clearer.
- Run the program and see what happens.
- Modify the program to use '18' for the age and '110' for the tickets remaining.
- Run the program ans see what happens
- Modify the program to use '17' for the age and '110' for the tickets remaining.
- Run the program ans see what happens
- Modify the program to use '17' for the age and '99' for the tickets remaining.
- Run the program ans see what happens
- Modify the program to add another condition, that a discount is available if the person has an NUS card, regardless of their age (the tickets remaining limitation still applies).
Questions
- An alternative strategy when there are two outcomes like this, is to have a single if...else statement and calculate a boolean value from the options which is then used as the condition in the if statement. Use Boolean algebra to solve this same problem in this fashion.
- What is missing from the code listing?