C2.1 · State algorithm preconditions and postconditions
Learn to state algorithm preconditions and postconditions through clear examples and targeted practice.
Ontario Grade 12 Computer Science
Designing Modular Programs
Making clear what an algorithm expects and promises
An algorithm description is more useful when it tells us not only what steps to follow, but also what must be true before those steps begin and what will be true when they finish. In ICS3U, you have worked with inputs, processing, and outputs. This lesson adds a precise way to describe the conditions around that work. The focus is C2.1: stating algorithm preconditions and postconditions. A precondition describes what must be true before an algorithm is used. A postcondition describes what the algorithm promises about its result after it finishes. Neither statement replaces the algorithm steps; together, they help a reader understand when the algorithm is suitable and what result to expect.
What you will learn
Explain what a precondition and a postcondition say about an algorithm.
State preconditions and postconditions in clear language for a simple algorithm.
Check that a stated postcondition matches the algorithm’s input and expected result.
1. Prerequisite review: inputs, steps, and outputs
An algorithm receives input, follows steps, and produces an output. For example, an algorithm might receive a list of numbers and report the largest number. Knowing the input and output is a useful start, but it may not tell us all the conditions needed for the algorithm to make sense. Does the list have to contain at least one number? Is the output supposed to be one of the values in the list? Preconditions and postconditions make such expectations explicit.
A precondition is a statement about the situation before the algorithm begins. It may describe acceptable input or a fact the caller must ensure. A postcondition is a statement about the situation after the algorithm finishes. It describes what the output means or what has changed. State both in ordinary, specific language: avoid vague phrases such as “the input is okay” or “the result is correct.”
The words “before” and “after” are useful reminders. If a condition must be true before the first algorithm step, it belongs in the precondition. If it describes the result after the steps, it belongs in the postcondition. Do not call an expected output a precondition just because the algorithm needs to produce it.
Precondition: what must be true before the algorithm starts.
Postcondition: what the algorithm guarantees about the result after it finishes.
Name input and output clearly so the conditions have something specific to describe.
2. State a useful algorithm contract
A compact description can list the input, precondition, algorithm steps, expected output, and postcondition. The precondition sets the conditions under which the steps are intended to be used. The postcondition describes the expected result for input meeting those conditions. A good postcondition is more informative than “the algorithm returns an answer”: it says what the answer represents.
For example, suppose an algorithm receives a non-empty list of numbers and returns its largest value. The precondition can say that the list contains at least one number. The postcondition can say that the returned value is a member of the list and is at least as large as every value in it. These statements describe the input and intended result without giving a lengthy proof.
Keep statements within the information the algorithm actually has. If an algorithm receives only a number, do not claim that it returns the position of that number in a list. If a postcondition says the result is the largest list value, the described steps must actually find a largest value. The condition and the algorithm description must agree.
Make each condition specific to the stated input and output.
A precondition is not a guarantee about what the algorithm will return.
A postcondition must match what the algorithm’s steps are intended to produce.
3. Describe conditions alongside simple code
A short function can be accompanied by plain-language conditions. The comments below describe the function’s intended use and result; they do not make Python enforce those conditions automatically. A person using the function is responsible for providing input that meets its precondition.
In a trace, record the input and follow the stated steps to determine the expected output. Then compare that output with the postcondition. This is a reading exercise: the code shown here has not been run.
Conditions describe expectations; they are not necessarily checks performed by the code.
Trace the steps using the stated input, then see whether the expected output fits the postcondition.
4. Review the contract before relying on it
When reviewing an algorithm description, first identify the input and the output. Next, ask what must already be true for the steps to be suitable. That is the precondition. Finally, ask what can be said about the result after the steps finish. That is the postcondition. Keep the two statements separate, and check that neither makes a claim unsupported by the described algorithm.
A missing precondition can leave a reader unsure which inputs are intended. A missing or vague postcondition can leave a reader unsure what a result means. Stating both helps a programmer and a reader agree on how an algorithm should be used and what it is expected to deliver.
Read the input and output before writing either condition.
Check that the precondition concerns the starting situation and the postcondition concerns the ending result.
Use wording that a reader can check against the stated input and output.
Worked example
Finding the largest value
Describe the precondition and postcondition for an algorithm that receives a list of numbers and returns the largest value. Trace the listed steps for [6, 2, 9, 4].
Pseudocode
largest(numbers)
best = first value in numbers
for each remaining value n:
if n > best:
best = n
return best
State the contract
Input: a list of numbers. Precondition: the list contains at least one number. Expected output: one number from the list that is no smaller than any other list value. This postcondition says what the returned value represents.
Trace the steps
Start with 6 as the current largest. Compare 2 with 6 and keep 6; compare 9 with 6 and keep 9; compare 4 with 9 and keep 9. The expected output is 9, which is in the input list and is at least as large as each listed value.
Answer: Precondition: the input list contains at least one number. Postcondition: the output is a value in that list that is at least as large as every value in the list.
Check: The non-empty condition matters because the described first step needs a list value to start with.
Worked example
Computing an average
State conditions for an algorithm that receives a list of numbers and returns their average. Use [4, 8, 10] to determine the expected output.
Pseudocode
average(numbers)
total = 0
for each number n in numbers:
total = total + n
return total / number of values
Identify the starting condition
Input: a list of numbers. Precondition: the list contains at least one number. This ensures that an average is defined for the stated task.
Trace and state the result
Add the values: 4 plus 8 plus 10 gives 22. Divide that total by the three values, giving an expected output of 22 divided by 3, or approximately 7.33. The postcondition is that the output equals the sum of the input values divided by the number of values.
Answer: Precondition: the list has at least one number. Postcondition: the returned number is the sum of the list values divided by how many values the list contains.
Check: The postcondition describes the meaning of the output, not just its data type.
Worked example
Converting minutes to hours and minutes
Describe the conditions for an algorithm that receives a non-negative whole number of minutes and returns the equivalent number of whole hours and leftover minutes. Trace the input 137.
Pseudocode
convert(minutes)
hours = whole number of groups of 60 in minutes
leftover = minutes after removing those groups
return hours, leftover
State the precondition
Input: a whole number of minutes. Precondition: the input is a whole number that is zero or greater. This describes the values intended for the conversion.
Trace the conversion
There are 60 minutes in an hour. For 137 minutes, take 2 whole hours, which account for 120 minutes, leaving 17 minutes. The expected output is 2 hours and 17 minutes.
State the postcondition
The returned hours and leftover minutes represent the original total: their combined minutes equal the input. The leftover minutes are from 0 through 59. These statements describe the promised result.
Answer: Precondition: the input is a non-negative whole number of minutes. Postcondition: the output gives whole hours and leftover minutes representing the input, with 0 to 59 leftover minutes.
Check: For the trace, 2 hours and 17 minutes represent 120 plus 17, which is 137 minutes.
Common mistakes and how to avoid them
Writing an expected result as the precondition.
Correction: The precondition describes what must be true before the algorithm starts; the expected result belongs in the postcondition.
Using a vague postcondition such as “returns the right answer.”
Correction: Say what the output represents, such as “the output is a largest value in the input list.”
Assuming a stated precondition is automatically checked by the code.
Correction: A condition may be documentation only. Unless the algorithm includes a check, the caller must provide input meeting the precondition.
Stating a postcondition that does not match the described steps.
Correction: Trace the steps on a suitable input and check that the result satisfies the claim.
Lesson summary
A precondition states what must be true before an algorithm begins.
A postcondition states what the algorithm promises about its result after it finishes.
Useful conditions are specific, fit the stated input and output, and agree with the algorithm steps.
A trace can help check whether a stated expected output matches the postcondition.
Check your understanding
Question 1
An algorithm returns the first value in a list. Which statement is a precondition for this task?
The list contains at least one value.
The returned value is the first value in the list.
The output has already been produced.
The returned value is the largest value.
Show answer and explanation
The list contains at least one value.
The list must contain a value before the algorithm can return its first value. The other statements describe an output or a claim not promised by this task.
Question 2
An algorithm counts the number of values in a list. Which is a suitable postcondition?
The list is provided before the algorithm starts.
The output equals the number of values in the input list.
The input is a list.
The algorithm has not started.
Show answer and explanation
The output equals the number of values in the input list.
A postcondition describes the result after completion. The count should equal how many values were in the input list.
Question 3
A description says an algorithm returns the largest value in a list. The list is empty. What is the clearest issue to address in its contract?
State a precondition requiring at least one list value.
Change the postcondition to say the list was an input.
Call the empty list the largest value.
Leave the conditions unstated because the output is obvious.
Show answer and explanation
State a precondition requiring at least one list value.
A list with at least one value gives the described task a value to return. The precondition should make that requirement clear.
Key terms
Algorithm
A set of steps for completing a task or producing a result.
Precondition
A statement of what must be true before an algorithm begins.
Postcondition
A statement of what the algorithm promises about the result after it finishes.
Trace
A step-by-step record of how an algorithm handles a particular input.
Tutor support for C2.1 · State algorithm preconditions and postconditions
Working on C2.1 · State algorithm preconditions and postconditions in ICS4U? Bring the question and your own attempt. In a live session, you can trace a small input step by step, compare expected and actual output, and test an edge case. Ask about the first step that does not make sense, then try a similar question yourself.
برای مبحث C2.1 · بیان پیششرط و پسشرط الگوریتم در درس ICS4U، سؤال و تلاش خود را همراه بیاورید. در جلسه میتوانید یک ورودی کوچک را گامبهگام دنبال کنید، خروجی مورد انتظار و واقعی را مقایسه کنید و یک حالت مرزی را بیازمایید. راهنمای معلم برنامهنویسی را ببینید یا جلسهٔ معارفهٔ رایگان ۳۰ دقیقهای درخواست کنید. زمان جلسه پس از پذیرش معلم قطعی میشود.
About this lesson and its review
Published by DoAssignment. This reviewed lesson follows Ontario Grade 12 Computer Science (ICS4U), expectation C2.1. It is a study resource, not an official curriculum publication.
Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.