A recipe for an omelette assumes that the cook has eggs. It promises that, if the steps are followed, an omelette results. Every algorithm likewise assumes certain things before it starts and promises certain things when it ends. These are its preconditions and postconditions.
Preconditions
Definition: Precondition. A condition that must be true before an algorithm or a step begins, for it to work correctly.
For the algorithm that finds the largest value in a list, a precondition is that the list contains at least one value. If the list is empty, there is no largest value, and the algorithm cannot give a meaningful answer.
Postconditions
Definition: Postcondition. A condition that must be true after an algorithm or a step has finished correctly.
For the same algorithm, the postcondition has two parts: the result is one of the values in the list, and no value in the list is larger than the result. Both parts are needed. A result of 1000 would satisfy the second part for the list 5, 2, 9, but it is not in the list.
Stating Conditions Precisely
Conditions must be stated so that they can be checked. "The input is good" and "the answer is correct" cannot be checked. "The list has at least one value" and "the result is greater than or equal to every value in the list" can. Table 1.11 gives conditions for three familiar algorithms.
Table 1.11: Preconditions and postconditions of three algorithms
| Algorithm | Precondition | Postcondition |
|---|---|---|
| Largest value in a list | The list is not empty | Result is in the list and no value exceeds it |
| Average of n marks | n is greater than 0 | Result equals sum of marks divided by n |
| Division of a by b | b is not zero | Result × b equals a |
The Contract Between Caller and Algorithm
Preconditions and postconditions together form a kind of contract Whoever uses the algorithm is responsible for making the precondition true. In return, the algorithm guarantees the postcondition. If the precondition is false, the algorithm promises nothing.
Using the Conditions
The conditions are useful throughout problem solving.
- In validation: when a user can supply data that breaks a precondition, the program checks it first. Input validation is the checking of preconditions on input.
- In testing: a postcondition says exactly what a correct result looks like, so it tells the tester what to check.
- In decomposition: the postcondition of one subproblem must satisfy the precondition of the next.
Example 1.11: Checking a result against its postcondition
Problem: An algorithm for the largest value is run on the list 5, 2, 9 and reports 9. Check the precondition and the postcondition.
Solution:
- Precondition: the list must not be empty. The list has three values, so the precondition holds.
- Postcondition, part 1: the result must be in the list. 9 appears in the list.
- Postcondition, part 2: no value may be larger than the result. 5 ≤ 9, 2 ≤ 9 and 9 ≤ 9.
Result: Both parts of the postcondition hold, so 9 is a correct result for this instance.
Common mistakes
- Stating a condition too vaguely to be checked, such as "the data is fine".
- Running an algorithm when its precondition is false and trusting the answer.
- Giving only part of a postcondition, so that a wrong result can still satisfy it.
Key takeaways
- A precondition must be true before an algorithm starts.
- A postcondition is what the algorithm guarantees when it finishes correctly.
- Conditions must be precise enough to be checked.
- Validation checks preconditions; testing checks postconditions.
Practice questions
- State a precondition for calculating the average of n numbers.
- Who is responsible for making a precondition true?
- Why is "the answer is correct" a poor postcondition?
Answers to the practice questions
- n must be greater than 0, since division by zero is not allowed.
- The user or caller of the algorithm.
- It cannot be checked, because it does not say what a correct answer is.
Sources
- Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit I
