Algorithms · Cambridge syllabus 7.2, 7.9, 8.1 · Lesson 3 of 3
Inputs and Outputs
Input obtains data for processing, and output communicates a result. Clear prompts state what is expected. Read into an appropriate variable, check validity when required, then produce an output that includes useful context.
By the end, you can
- Design algorithms that accept user input.
- Validate or check input where appropriate.
- Produce appropriate outputs.
- Include prompts and messages in algorithms.
User interaction
An algorithm that takes no input solves exactly one problem. Taking input is what makes it general, and every input needs three things: a prompt saying what is wanted, a variable to hold it, and a check that what arrived is usable.
The prompt is part of the algorithm, not decoration. OUTPUT "Enter the mark out of 100" followed by INPUT Mark tells the user what to type and, by saying out of 100, prevents half the errors before they happen.
Checking input is expected wherever a wrong value would break the algorithm. A mark should be between 0 and 100, a name should not be empty, a date should have the right shape. The usual pattern is a loop that repeats until the value is acceptable: REPEAT, INPUT Mark, UNTIL Mark >= 0 AND Mark <= 100, with a message explaining the problem each time round.
Output should be meaningful rather than bare. OUTPUT Average prints a number with no context; OUTPUT "The class average is ", Average tells the reader what it is, with the values separated by commas. Where the result is a decision, say the decision: OUTPUT "Pass" rather than OUTPUT Grade when Grade is a code the user has never seen.
Writing the inputs and outputs down first is the fastest way to start an algorithm, because they fix what the processing has to turn one into the other.
Worth remembering
- Every input needs a prompt, a variable and a check.
- Repeat the input until the value is acceptable, explaining each rejection.
- Label output so the number means something to the reader.
Worked example
INPUT Name followed by OUTPUT "Hello ", Name uses the entered value in a message.
Practice
Warm-up · not marked
Check your understanding
Answer in your own words, then compare with the worked answer. Nothing here is saved or scored.
Worked answer
So the input has an unambiguous interpretation and the calculation uses consistent units.
Check your method, units and explanation against it.
Marked practice
Set 1 of 4 · easiest first · Checked as you go, with the worked answer. Sign in or create a free account to save your progress.