Lesson overview
From real world steps to clear algorithms
A vending machine is a simple example of an algorithm in action. Behind every button press, there is a step by step process that the machine follows. It needs to check the selected item, compare money with price, decide whether to dispense the product, and return change if needed.
To design this behavior, we first describe the logic using pseudocode and a flowchart. These tools help us plan the algorithm in a way that is easy to understand even before writing a real program.
Basic vending machine scenario
In a basic transaction, the user inserts money and chooses a product. The vending machine has to:
- Check if the selected item exists and still has stock.
- Check if the inserted money is enough for that item.
- Dispense the product if the money is enough.
- Return change when the payment is more than the price.
- Return the money or ask for more if the payment is not enough.
Pseudocode for a vending machine
Pseudocode is a structured description of an algorithm written in a simple, human friendly style. It does not follow the exact syntax of a programming language, but it uses a clear format with keywords like IF, THEN, ELSE, and WHILE.
Example pseudocode for a vending machine transaction:
Vending machine pseudocode (simplified)
1. Start 2. Display list of products 3. Ask user to insert money 4. Ask user to select product 5. If product is out of stock, display message and return money 6. Else if money is less than price, ask user to add more or cancel 7. If money is enough, dispense product 8. If money is more than price, calculate and return change 9. Update stock and go back to idle state 10. End
Flowchart for a vending machine
A flowchart shows the same logic using symbols and arrows. Common symbols are:
- Oval for Start and End.
- Parallelogram for Input and Output (insert money, display message).
- Rectangle for Process steps (update stock, dispense product).
- Diamond for Decision points (enough money, in stock or not).
For the vending machine, the flowchart might start at Start, then move to Insert money and Select item. A decision checks if the item is in stock. If not, the flow goes to a message and money return. If it is in stock, the next decision checks if the money is enough. Different arrows show the paths for not enough money, exact payment, or excess payment.
Why this matters in IT
Using pseudocode and flowcharts helps programmers and stakeholders think through all possible cases. They can see what happens when money is short, when an item is out of stock, or when the user cancels. Planning at this level reduces errors and makes the final program easier to write, test, and maintain.
Key idea
The vending machine is a clear, everyday example that shows how algorithms guide real world processes using structured steps and decisions.