Arrays, Records & Data Handling: Using Temporary Variables Correctly (Copy)
Using Temporary Variables Correctly (Cambridge Standard – O Level 2210 + IGCSE 0478)
Purpose Of Temporary Variables In Cambridge Pseudocode
- Temporary variables are used to:
- Hold intermediate values during processing
- Prevent loss of original data
- Enable safe updates, swaps, and calculations
- Cambridge tests temporary variable usage to assess:
- Understanding of data flow
- Correct sequencing of assignments
- Avoidance of overwriting errors
- Temporary variables are common in:
- Swapping values
- Updating arrays based on conditions
- Counting, totalling, max/min algorithms
- Pre-release material logic
- Section B algorithm modifications
What Cambridge Means By A Temporary Variable
- A temporary variable:
- Stores a value for short-term use
- Is often used inside loops, IF statements, procedures, or functions
- It is:
- Declared like any other variable
- Used briefly
- Not required after the operation completes
Core Principle Of Temporary Variables
- Temporary variables prevent:
- Accidental data loss
- Incorrect overwriting of values
- Any time a value is:
- Needed later
- But must be replaced or updated
- A temporary variable is required
Declaring Temporary Variables (Cambridge Style)
- DECLARE temp : dataType
Examples:
- DECLARE temp : INTEGER
- DECLARE oldValue : REAL
- DECLARE current : STRING
Examiner Expectation
- Data type must:
- Match the value stored
- Variable name:
- Should indicate temporary purpose clearly
Using Temporary Variables For Swapping Values (Classic Use Case)
Incorrect Swap (Guaranteed Failure)
- a ← b
- b ← a
Why this fails:
- Original value of
ais lost
Correct Swap Using Temporary Variable
- temp ← a
- a ← b
- b ← temp
Examiner Focus
- Correct order of assignments
- temp holds original value safely
- No data loss occurs
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change O Level And IGCSE Computer Science Full Scale Course
Temporary Variables In Array Updates
Updating Array Values Safely
- FOR i ← 1 TO 30
- temp ← marks[i]
- IF temp < 0 THEN
- marks[i] ← 0
- ENDIF
- ENDFOR
Why temp Is Useful Here
- Original value:
- Can be checked without modifying array immediately
- Logic remains:
- Clear
- Traceable
Temporary Variables In Calculations
Example: Percentage Calculation
- temp ← (obtained / total) * 100
- percentage ← temp
Examiner Focus
- Intermediate calculation stored safely
- Final variable receives correct value
- Avoids repeated calculations
Temporary Variables In Conditional Logic
Example: Normalising Values
- FOR i ← 1 TO 20
- temp ← values[i]
- IF temp > maxAllowed THEN
- values[i] ← maxAllowed
- ELSE
- values[i] ← temp
- ENDIF
- ENDFOR
Examiner Expectation
- temp captures original value
- Decision made before overwriting
- Clean separation of logic
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change O Level And IGCSE Computer Science Full Scale Course
Temporary Variables In Counting And Totalling
Example: Conditional Totalling With Temp
- total ← 0
- FOR i ← 1 TO 30
- temp ← marks[i]
- IF temp >= 0 AND temp <= 100 THEN
- total ← total + temp
- ENDIF
- ENDFOR
Examiner Focus
- temp prevents repeated array access
- Logic is clearer in trace tables
- Validity checked before accumulation
Temporary Variables In Max/Min Algorithms
Incorrect Max Logic Without Temp
- IF marks[i] > max THEN
- max ← marks[i]
- ENDIF
This is correct logically, but:
- In complex logic:
- temp improves clarity and safety
Correct Max Logic With Temp
- temp ← marks[i]
- IF temp > max THEN
- max ← temp
- ENDIF
Examiner Preference
- temp makes logic:
- Easier to follow
- Less error-prone in extended conditions
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change O Level And IGCSE Computer Science Full Scale Course
Temporary Variables In Two-Dimensional Arrays
Example: Cell Validation
- FOR r ← 1 TO 5
- FOR c ← 1 TO 4
- temp ← grid[r,c]
- IF temp < 0 THEN
- grid[r,c] ← 0
- ENDIF
- ENDFOR
- FOR c ← 1 TO 4
- ENDFOR
Examiner Focus
- temp used per cell
- No confusion between row and column
- Safe in-place update
Temporary Variables In Procedures And Functions
Procedure Example
- PROCEDURE updateScore(score)
- temp ← score
- IF temp < 0 THEN
- score ← 0
- ENDIF
- ENDPROCEDURE
Function Example
- FUNCTION adjustedScore(score)
- temp ← score
- IF temp > 100 THEN
- temp ← 100
- ENDIF
- RETURN temp
- ENDFUNCTION
Examiner Expectation
- temp is local
- Does not affect external variables unless returned
- Improves clarity of return logic
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change O Level And IGCSE Computer Science Full Scale Course
Temporary Variables In Searching Algorithms
Example: Linear Search Comparison
- found ← FALSE
- index ← 1
- WHILE found = FALSE AND index <= 30 DO
- temp ← names[index]
- IF temp = target THEN
- found ← TRUE
- ELSE
- index ← index + 1
- ENDIF
- ENDWHILE
Examiner Focus
- temp stores current element
- Comparison is clear
- Easier to trace step-by-step
Temporary Variables And Trace Tables
How Temp Appears In Tracing
- temp:
- Changes every iteration
- Reflects current value being processed
- Trace tables often include:
- temp
- index
- updated variables
Common Trace Errors Without Temp
- Confusing old and new values
- Losing track of overwritten data
- Misinterpreting update order
Common Mistakes When Using Temporary Variables
- Forgetting to declare temp
- Using temp before assigning a value
- Reusing temp incorrectly across loops
- Treating temp as permanent storage
- Confusing temp with loop index
When Temporary Variables Are NOT Required
- Simple direct assignments:
- total ← total + marks[i]
- Straight comparisons:
- IF marks[i] > max THEN
- When no overwriting risk exists
Using temp unnecessarily:
- Is not penalised
- But may clutter logic
Temporary Variables In Pre-Release Material
- Pre-release tasks often:
- Require safe updates
- Combine multiple conditions
- temp helps:
- Avoid logic errors
- Preserve original values during decision-making
Temporary Variables In Section B Modifications
- Section B commonly asks:
- “Modify the algorithm so that…”
- temp is useful when:
- Adding extra rules
- Adjusting values conditionally
- Safe strategy:
- Introduce temp
- Perform checks
- Update final variable
Example Section B Modification
Original:
- FOR i ← 1 TO 30
- total ← total + marks[i]
- ENDFOR
Modified With Temp:
- FOR i ← 1 TO 30
- temp ← marks[i]
- IF temp >= 0 AND temp <= 100 THEN
- total ← total + temp
- ENDIF
- ENDFOR
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change O Level And IGCSE Computer Science Full Scale Course
Examiner Checklist For Correct Use Of Temporary Variables
- temp is declared before use
- temp is assigned before being checked
- temp stores original value safely
- temp is used only where necessary
- No overwriting occurs before checks
- Logic remains traceable
Best-Practice Strategy For Paper 2
- Use temp when:
- A value must be checked before updating
- A value would otherwise be lost
- Do not use temp to:
- Replace loop counters
- Store final results permanently
- Keep temp:
- Local
- Short-lived
- Clearly named
Final Quality Checklist
- temp declared correctly
- Assignment order correct
- No data loss
- No index modification
- Logic traceable step-by-step
Final Lock-In Rules
- Temporary variables protect data
- Assignment order matters
- temp prevents overwriting errors
- temp improves trace clarity
- Correct temp usage = safer Paper 2 algorithms
