Ticket Sales

Line: Conditional Line
Easy Selection Learning Outcome One Walkthrough
Type: Walkthrough
You should have completed: Magic Numbers This topic leads to: Math Functions

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

  1. Create a new console application
  2. 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.

  3. Run the program and see what happens.
  4. Modify the program to use '18' for the age and '110' for the tickets remaining.
  5. Run the program ans see what happens
  6. Modify the program to use '17' for the age and '110' for the tickets remaining.
  7. Run the program ans see what happens
  8. Modify the program to use '17' for the age and '99' for the tickets remaining.
  9. Run the program ans see what happens
  10. 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

  1. 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.
  2. What is missing from the code listing?