Standard Algorithm & Logic Patterns: Validation Algorithms For Input Data (Copy)
Validation Algorithms For Input Data
What Input Validation Means
- Input validation ensures that:
- Data entered is acceptable
- Data entered is within required limits
- Data entered is of the correct type or format
- Used to:
- Prevent invalid processing
- Avoid runtime errors
- Enforce problem constraints
Core Examiner Rule (Very High Priority)
- Validation must occur before processing
- Invalid data must:
- Be rejected
- Or re-entered
- Examiners expect:
- Clear validation condition
- Correct loop choice
- No processing of invalid data
Common Types Of Validation (AS Level Scope)
| Validation Type | Purpose |
|---|---|
| Range check | Value within limits |
| Length check | Correct number of characters |
| Presence check | Data not empty |
| Type check (logic-based) | Numeric / non-numeric |
| Sentinel validation | Stop input on special value |
Range Check Validation (Most Common)
When To Use
- Marks
- Age
- Scores
- Menu options
Example: Validate Mark Between 0 And 100
INPUT mark
WHILE mark < 0 OR mark > 100 DO
OUTPUT "Invalid"
INPUT mark
ENDWHILE
Examiner Notes
ORis mandatoryANDhere would be logically wrong- Loop repeats until valid input
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 AS Level Computer Science Full Scale Course
Validation Using REPEAT UNTIL (Exam-Favourite)
Why REPEAT UNTIL Is Preferred
- Input is taken at least once
- Cleaner for validation logic
- Matches real-world input flow
Example: Validate Age Between 18 And 60
REPEAT
INPUT age
UNTIL age >= 18 AND age <= 60
Key Rule
- Condition in
UNTILmust describe valid data - Do NOT write invalid condition inside
UNTIL
Menu Option Validation
Example: Accept Only Options 1, 2 Or 3
REPEAT
INPUT option
UNTIL option = 1 OR option = 2 OR option = 3
Common Student Error
UNTIL option >= 1 AND option <= 3
- This allows invalid option
2.5if decimals were possible - Use exact matches unless range is explicitly allowed
Length Check Validation (Strings)
When Used
- Passwords
- IDs
- Codes
Example: Validate Password Length ≥ 8
REPEAT
INPUT password
UNTIL LENGTH(password) >= 8
Combined Length And Range Validation
REPEAT
INPUT code
UNTIL LENGTH(code) = 6 AND code[1] <> "0"
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 AS Level Computer Science Full Scale Course
Presence Check Validation
Purpose
- Prevent empty input
- Ensure something was entered
Example: Non-Empty Name
REPEAT
INPUT name
UNTIL LENGTH(name) > 0
Validation With Counting Attempts (Advanced But Testable)
Example: Allow Only 3 Attempts
attempts ← 0
valid ← FALSE
WHILE attempts < 3 AND valid = FALSE DO
INPUT pin
IF pin >= 1000 AND pin <= 9999 THEN
valid ← TRUE
ELSE
attempts ← attempts + 1
ENDIF
ENDWHILE
IF valid = TRUE THEN
OUTPUT "Access Granted"
ELSE
OUTPUT "Access Denied"
ENDIF
Validation During Repeated Input (Inside Loops)
Example: Validate Each Mark Before Storing
FOR i ← 1 TO 5
REPEAT
INPUT mark
UNTIL mark >= 0 AND mark <= 100
marks[i] ← mark
NEXT i
Examiner Tip
- Validation loop inside input loop
- Not after data collection
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 AS Level Computer Science Full Scale Course
Sentinel-Controlled Validation
When Used
- Unknown number of inputs
- Stop on special value
Example: Accept Positive Numbers Until -1
REPEAT
INPUT num
IF num > 0 THEN
OUTPUT num
ENDIF
UNTIL num = -1
Common Validation Errors (Very High Frequency)
Error 1: Using AND Instead Of OR
WHILE mark < 0 AND mark > 100
- Impossible condition
- Loop never executes
Error 2: Processing Invalid Data
INPUT age
total ← total + age
WHILE age < 18 OR age > 60
- Validation must come before processing
Error 3: Wrong UNTIL Condition
UNTIL age < 18 OR age > 60
- This stops on invalid data
- Must stop on valid data
Error 4: Infinite Validation Loop
- Forgetting to re-input value inside loop
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 AS Level Computer Science Full Scale Course
Validation Pattern Comparison Table
| Pattern | Best Use |
|---|---|
| WHILE invalid DO | When input already exists |
| REPEAT UNTIL valid | Cleanest validation |
| IF only | Single-check warnings |
| Loop + counter | Limited attempts |
Examiner-Approved Validation Language
- “The input is repeatedly requested until valid”
- “A range check ensures the value is acceptable”
- “Invalid data is rejected before processing”
- “The loop prevents incorrect input”
Exam-Ready Validation Templates
Range Validation (REPEAT UNTIL)
REPEAT
INPUT value
UNTIL value >= min AND value <= max
Option Validation
REPEAT
INPUT option
UNTIL option = A OR option = B OR option = C
Length Validation
REPEAT
INPUT text
UNTIL LENGTH(text) = requiredLength
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 AS Level Computer Science Full Scale Course
One-Line Rules To Memorise
- Validate before processing
- REPEAT UNTIL is safest
- OR for invalid ranges, AND for valid ranges
- Never accept bad input silently
- Clear validation logic scores easy marks
