Programming Concepts · Cambridge syllabus 8.1 · Lesson 3 of 4
Input and Output
A program reads input into variables and displays output with suitable labels. Input may require conversion and validation before arithmetic. Output should communicate the result clearly rather than exposing an unexplained number.
By the end, you can
- Accept data from a user.
- Store input in variables.
- Use prompts appropriately.
- Display information to the user.
- Format output appropriately.
- Combine text and variable values in outputs.
Input
Input brings a value from the user into a variable, and the pattern is always the same three parts: say what is wanted, read it, check it.
The prompt comes first and belongs in the program. OUTPUT "Enter your age in whole years" followed by INPUT Age prevents more errors than any validation that follows it, because it tells the user what form the answer should take.
Input nearly always arrives as text, whatever the user typed. A program that needs a number must convert it, and the conversion must be able to fail: if the user typed hello where a number was wanted, the conversion fails, and a program that does not expect that simply crashes. This is why a type check is part of reading a number, not an optional extra.
Validation follows immediately, before the value is used for anything. The standard shape is a loop that keeps asking until the value is acceptable, with a message each time saying why it was not: REPEAT, prompt, INPUT Age, UNTIL Age >= 0 AND Age <= 120.
A value read once and used many times should be stored in a variable, not read again. Asking the user the same question twice is both slower and a bug waiting to happen, because the two answers may differ.
Worth remembering
- Prompt, read, then check — in that order, before the value is used.
- Input arrives as text, so a number must be converted and the conversion can fail.
- Loop until the value is acceptable, saying each time why it was not.
Output
Output puts information in front of the user, and it is judged on whether the person reading it can tell what it means.
A bare value says nothing. OUTPUT Total prints 47 with no explanation; OUTPUT "Total marks: ", Total prints something a reader understands. In 0478 pseudocode, text and values are output together by separating them with commas: OUTPUT "You have ", Lives, " lives left".
Formatting matters for numbers. An average of 72.33333333333333 should be rounded for display, and OUTPUT ROUND(Average, 2) shows it to two decimal places; money should always show two. Rounding for display is not the same as rounding the stored value: calculate with the full precision and round only when outputting, or the errors accumulate.
Layout helps when there is more than a line. Putting a label on each line, lining up columns, and separating sections with a blank line turn a wall of numbers into a report. Where a list is printed inside a loop, a heading before the loop is printed once and the detail line inside it once per item.
The output should answer the question that was asked. If the program was asked whether the pupil passed, print Pass or Fail, not the mark and not a code that has to be looked up.
Worth remembering
- Label every value so the reader knows what it is.
- Round for display only; keep the full precision in the calculation.
- Print headings outside a loop and detail lines inside it.
Worked example
INPUT Quantity, Total ← Quantity * 3, OUTPUT "Cost: ", Total reads, processes and displays a value.
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
Validate that it represents an allowed number and convert or read it into the required numeric type.
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.