Arrays, Records & Data Handling: Updating Array Values Based On Conditions (Copy)
Updating Array Values Based On Conditions (Cambridge Standard – O Level 2210 + IGCSE 0478)
Why Conditional Array Updates Are Heavily Tested In Paper 2
- Updating array values based on conditions is used to:
- Modify stored data safely
- Apply rules to existing records
- Correct, adjust, or normalise values
- Cambridge tests this to assess:
- Correct traversal logic
- Accurate condition placement
- Safe in-place updates without breaking bounds
- This pattern appears frequently in:
- Pre-release material algorithms
- Section B modification tasks
- Pseudocode completion questions
Core Concept: Conditional Update Vs Conditional Processing
- Conditional processing:
- Uses IF to decide what to count, output, or total
- Conditional updating:
- Uses IF to decide what value in the array should change
Key distinction:
- The array element itself is modified
Fundamental Pattern For Conditional Updates
Standard Cambridge-Safe Pattern
- FOR index ← lowerBound TO upperBound
- IF condition(arrayName[index]) THEN
- arrayName[index] ← newValue
- ENDIF
- IF condition(arrayName[index]) THEN
- ENDFOR
Examiner Expectations
- Loop traverses full required range
- IF condition is evaluated per element
- Assignment updates the array element directly
- No modification of loop index
Simple Conditional Update Examples
Example 1: Resetting Invalid Values
- FOR i ← 1 TO 30
- IF marks[i] < 0 OR marks[i] > 100 THEN
- marks[i] ← 0
- ENDIF
- IF marks[i] < 0 OR marks[i] > 100 THEN
- ENDFOR
Example 2: Applying Bonus Marks
- FOR i ← 1 TO 30
- IF marks[i] >= 40 AND marks[i] < 50 THEN
- marks[i] ← marks[i] + 5
- ENDIF
- IF marks[i] >= 40 AND marks[i] < 50 THEN
- ENDFOR
Examiner Focus
- Update uses:
- marks[i] on both sides of assignment
- Condition is precise
- No accidental double updates
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
Updating Arrays While Preserving Valid Data
Safe Update Pattern With Range Protection
- FOR i ← 1 TO 30
- IF marks[i] >= 0 AND marks[i] <= 100 THEN
- marks[i] ← marks[i] * 1.05
- ENDIF
- IF marks[i] >= 0 AND marks[i] <= 100 THEN
- ENDFOR
Examiner Expectation
- Validity check before update
- No update to invalid values
- Mathematical operation applied correctly
Conditional Update With ELSE Clause
Two-Way Update Pattern
- FOR i ← 1 TO 20
- IF attendance[i] = TRUE THEN
- points[i] ← points[i] + 2
- ELSE
- points[i] ← points[i] + 0
- ENDIF
- IF attendance[i] = TRUE THEN
- ENDFOR
Examiner Note
- ELSE is optional
- Use ELSE only when:
- Both outcomes require explicit action
Conditional Update Using Nested Selection
Multi-Condition Update
- FOR i ← 1 TO 30
- IF marks[i] >= 80 THEN
- grade[i] ← “A”
- ELSEIF marks[i] >= 65 THEN
- grade[i] ← “B”
- ELSEIF marks[i] >= 50 THEN
- grade[i] ← “C”
- ELSE
- grade[i] ← “F”
- ENDIF
- IF marks[i] >= 80 THEN
- ENDFOR
Examiner Expectations
- Conditions ordered correctly (highest to lowest)
- Exactly one update per element
- No overlapping 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
Updating Arrays Based On Another Array
Cross-Array Conditional Update
- FOR i ← 1 TO 30
- IF valid[i] = TRUE THEN
- marks[i] ← marks[i] + bonus
- ENDIF
- IF valid[i] = TRUE THEN
- ENDFOR
Examiner Focus
- Index used consistently across arrays
- Both arrays share same bounds
- No mismatched indices
Updating Arrays Using WHILE Loops
WHILE-Based Update (Allowed, Higher Risk)
- index ← 1
- WHILE index <= 30 DO
- IF marks[index] < 0 THEN
- marks[index] ← 0
- ENDIF
- index ← index + 1
- IF marks[index] < 0 THEN
- ENDWHILE
Examiner Focus
- index initialised before loop
- index incremented inside loop
- Correct loop termination
Updating Arrays In Reverse Order
Reverse Update Pattern
- FOR i ← upperBound TO lowerBound STEP -1
- IF values[i] = 0 THEN
- values[i] ← defaultValue
- ENDIF
- IF values[i] = 0 THEN
- ENDFOR
Examiner Expectation
- STEP is negative
- Bounds respected
- Reverse order justified by task
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
Updating Two-Dimensional Arrays Based On Conditions
Standard Nested Loop Update Pattern
- FOR r ← rowLower TO rowUpper
- FOR c ← columnLower TO columnUpper
- IF data[r,c] < 0 THEN
- data[r,c] ← 0
- ENDIF
- IF data[r,c] < 0 THEN
- ENDFOR
- FOR c ← columnLower TO columnUpper
- ENDFOR
Examiner Focus
- Nested loops mandatory
- Correct row–column order
- Update applied per cell
Updating Arrays Without Breaking Trace Logic
- Updates affect:
- Subsequent operations
- Trace tables must:
- Reflect updated values
- Do not:
- Assume original values persist after update
Trace Table Implication Example
- IF marks[i] < 0 THEN
- marks[i] ← 0
- Next iteration using marks[i]:
- Uses updated value
Common Conditional Update Errors (Guaranteed Mark Loss)
- Updating loop index instead of array element
- Using wrong index variable
- Updating outside array bounds
- Multiple updates per iteration unintentionally
- Forgetting ENDIF
- Using assignment operator incorrectly
High-Risk Mistake Example
- IF marks[i] < 50 THEN
- i ← i + 1
- This modifies loop control variable
- Guaranteed logical failure
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
Conditional Updates In Pre-Release Material
- Pre-release tasks often require:
- Normalising values
- Applying penalties or bonuses
- Correcting invalid data
- You must:
- Follow rules exactly as stated
- Not invent extra conditions
- Not update values outside specification
Conditional Updates In Section B Modifications
- Section B frequently asks:
- “Modify the algorithm so that…”
- Safe strategy:
- Insert IF inside existing loop
- Update array element directly
- Do not alter loop bounds or declaration
Example Section B Modification
Original:
- FOR i ← 1 TO 30
- total ← total + marks[i]
- ENDFOR
Modified:
- FOR i ← 1 TO 30
- IF marks[i] >= 0 AND marks[i] <= 100 THEN
- marks[i] ← marks[i] + 2
- total ← total + marks[i]
- ENDIF
- IF marks[i] >= 0 AND marks[i] <= 100 THEN
- ENDFOR
Examiner Checklist For Safe Conditional Updates
- Loop bounds match array declaration
- IF condition is precise and complete
- Only array element is updated
- Loop index untouched
- Update does not cause out-of-range values
- Logic remains traceable
Best-Practice Strategy For Paper 2
- Decide clearly:
- What condition triggers update
- What new value should be assigned
- Use FOR loops for full traversal
- Use WHILE only when necessary
- Combine update and accumulation carefully
- Trace one full iteration manually
Final Quality Checklist
- Correct loop structure
- Correct condition placement
- Correct assignment target
- No boundary violations
- No loop variable modification
- Updated values used consistently
Final Lock-In Rules
- Conditional updates modify array contents
- IF controls whether update occurs
- Loop controls how many elements are checked
- Loop index must never be modified
- Clean conditional updates = safe Paper 2 marks
