Topic 11 Β· Programming Scenarios Β· 11.3

Writing the Code

Learn how to turn a clear IPO plan into structured pseudocode one stage at a time.

Invitation

Where should you begin?

Once the inputs, processes and outputs are clear, the code no longer begins as a blank page.

The IPO plan already tells you what must happen. Your job is to translate each part into code in a sensible order.

Do not try to write the whole solution at once. Build it one stage at a time.
From plan to code
Inputs
↓
Processes
↓
Outputs
Big Idea

Turn each part of the plan into code

1Inputs
2Validation
3Processes
4Outputs
5Comments
6Final checks
A structured method reduces mistakes and makes every requirement easier to check.
Start with Inputs

Store every input in a variable

The calculator needs two numbers and one operator.

Each value must be stored so the program can use it later.

Correct input statementsPseudocode
Number1 ← INPUT "Enter the first number"
Operator ← INPUT "Enter an operator"
Number2 ← INPUT "Enter the second number"
The variable on the left stores the value entered by the user.
Input must be stored
User enters 12
↓
Stored in Number1
↓
Used later
Validation

Check that inputs are acceptable

Invalid value
Ask again
β†’
Valid value
Continue
Validate the operatorPseudocode
REPEAT
    Operator ← INPUT "Enter +, -, * or /"
UNTIL Operator = "+" OR Operator = "-" OR
      Operator = "*" OR Operator = "/"
Validation prevents unsuitable data from moving into the process stage.
Processes

Choose the correct programming construct

SequenceUse when statements run in order.
SelectionUse when a condition chooses between different paths.
IterationUse when statements must repeat.

Calculator process

The operator decides which calculation runs, so this process needs selection.

Check the operatorPseudocode
IF Operator = "+"
THEN
    Result ← Number1 + Number2
ELSEIF Operator = "-"
THEN
    Result ← Number1 - Number2
ELSEIF Operator = "*"
THEN
    Result ← Number1 * Number2
ELSE
    Result ← Number1 / Number2
ENDIF
Outputs

Explain what the result means

βœ— Weak output

OUTPUT Result

A value appears, but the user may not know what it represents.

βœ“ Clear output

OUTPUT "The result is: ", Result

The message gives the value meaning.

An appropriate output message makes the program easier to understand and use.
Worked Example

Build the calculator in stages

Stage 1: Input

Store the two numbers and the operator.

Stage 2: Validate

Only allow recognised operators.

Stage 3: Process

Use selection to perform the correct calculation.

Stage 4: Output

Display the result with a clear message.

Complete calculator structurePseudocode
// input the first number
Number1 ← INPUT "Enter the first number"

// validate the operator
REPEAT
    Operator ← INPUT "Enter +, -, * or /"
UNTIL Operator = "+" OR Operator = "-" OR
      Operator = "*" OR Operator = "/"

// input the second number
Number2 ← INPUT "Enter the second number"

// perform the selected calculation
IF Operator = "+"
THEN
    Result ← Number1 + Number2
ELSEIF Operator = "-"
THEN
    Result ← Number1 - Number2
ELSEIF Operator = "*"
THEN
    Result ← Number1 * Number2
ELSE
    Result ← Number1 / Number2
ENDIF

// output the result
OUTPUT "The result is: ", Result
Computational Thinking

The thinking continues while you code

DecompositionWrite one section of the solution at a time.
Pattern RecognitionRecognise familiar patterns such as validation loops and selection.
AbstractionUse clear variable names that represent the important data.
Algorithm DesignPlace every statement in the order required by the plan.
Hinge Question

Why must an input be stored?

Question

Why is INPUT "Enter a number" incomplete if the value is not stored in a variable?

β€œThe value enters the program but is not saved, so later statements cannot use it in a calculation or decision.”
Final Checks

Return to the scenario

βœ“ Inputs: Are all values stored in variables?
βœ“ Validation: Are unsuitable values rejected?
βœ“ Processes: Has every required action been included?
βœ“ Outputs: Are all required results displayed?
βœ“ Messages: Do inputs and outputs have clear prompts?
βœ“ Comments: Do comments explain sections of code?
βœ“ Identifiers: Are variable names meaningful?
βœ“ Requirements: Can every statement in the scenario be ticked?
Exam tip: Use the question as a checklist. If one requirement cannot be ticked, the solution is incomplete.
Common Mistake

Do not write English instructions instead of code

βœ— Not code-like

Start at 1 and loop until 10.

βœ“ Code-like

FOR Count ← 1 TO 10
Remember: Pseudocode does not need to belong to one programming language, but it must still use recognisable programming structures.
Summary

Build, check and improve

BuildOne stage at a time.
CheckEvery requirement.
ImproveMessages, validation and comments.
Final rule: A clear plan becomes clear code when each requirement is translated into an appropriate programming statement.