Standard Algorithm & Logic Patterns: Flag Variables And Their Applications (Copy)
Flag Variables And Their Applications
What A Flag Variable Is
- A flag variable is a Boolean control variable
- It stores only two logical states:
- TRUE / FALSE
- Used to:
- Indicate whether a condition has occurred
- Control loop execution
- Prevent repeated actions
- Flags do not store data
- They store state
Core Examiner Rule (High-Scoring Concept)
- A flag must be:
- Initialised correctly
- Updated at the correct time
- Checked logically
- Incorrect initialisation or placement = lost marks
Why Flag Variables Are Needed
- Without flags:
- Loops run unnecessarily
- Conditions repeat incorrectly
- Early stopping becomes impossible
- Flags make algorithms:
- Clear
- Efficient
- Examiner-friendly
Common Flag Variable Names (Exam-Friendly)
| Purpose | Flag Name |
|---|---|
| Found condition | found |
| Valid input | valid |
| Error detected | error |
| Swap occurred | swapped |
| Limit exceeded | exceeded |
| Match detected | match |
Basic Flag Pattern (Detection Logic)
When To Use
- Searching
- Checking existence
- Detecting a condition
Standard Pattern
flag ← FALSE
FOR i ← 1 TO n
IF condition THEN
flag ← TRUE
ENDIF
NEXT i
IF flag = TRUE THEN
OUTPUT "Condition Met"
ELSE
OUTPUT "Condition Not Met"
ENDIF
Flag Variables In Searching Algorithms
Example: Linear Search With Flag
found ← FALSE
FOR i ← 1 TO n
IF arr[i] = target THEN
found ← TRUE
ENDIF
NEXT i
IF found = TRUE THEN
OUTPUT "Found"
ELSE
OUTPUT "Not Found"
ENDIF
Why Flag Is Needed
- Target may not exist
- Loop completes regardless
- Flag records whether match occurred
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
Flag Variables With Early Loop Termination
Purpose
- Stop processing once task is complete
- Improve efficiency
Example: Early Stop Linear Search
found ← FALSE
i ← 1
WHILE i <= n AND found = FALSE DO
IF arr[i] = target THEN
found ← TRUE
ELSE
i ← i + 1
ENDIF
ENDWHILE
IF found = TRUE THEN
OUTPUT "Found"
ELSE
OUTPUT "Not Found"
ENDIF
Examiner Insight
- Two conditions control the loop:
- Bounds
- Flag state
- This shows efficiency awareness
Flag Variables In Validation Algorithms
Example: Validate Input With Flag
valid ← FALSE
WHILE valid = FALSE DO
INPUT mark
IF mark >= 0 AND mark <= 100 THEN
valid ← TRUE
ENDIF
ENDWHILE
OUTPUT mark
Why Flag Is Useful Here
- Clean exit condition
- Prevents infinite loops
- Validation logic is clear
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
Flag Variables In Sorting Algorithms
Example: Optimised Bubble Sort
swapped ← TRUE
pass ← 1
WHILE pass <= n - 1 AND swapped = TRUE DO
swapped ← FALSE
FOR j ← 1 TO n - pass
IF arr[j] > arr[j + 1] THEN
temp ← arr[j]
arr[j] ← arr[j + 1]
arr[j + 1] ← temp
swapped ← TRUE
ENDIF
NEXT j
pass ← pass + 1
ENDWHILE
Role Of The Flag
swapped = FALSEmeans:- No swaps occurred
- Array is already sorted
- Algorithm stops early
Flag Variables For Error Detection
Example: Detect Invalid Data In A Series
error ← FALSE
FOR i ← 1 TO n
INPUT value
IF value < 0 THEN
error ← TRUE
ENDIF
NEXT i
IF error = TRUE THEN
OUTPUT "Invalid Data Detected"
ENDIF
Flag Variables In Multi-Condition Problems
Example: Check If Any Value Exceeds Limit
exceeded ← FALSE
FOR i ← 1 TO 24
INPUT temp
IF temp > 90 THEN
exceeded ← TRUE
ENDIF
NEXT i
IF exceeded = TRUE THEN
OUTPUT "Limit Exceeded"
ELSE
OUTPUT "Safe"
ENDIF
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
Flag Variables In Nested Logic
Example: Stop Outer Loop When Found
found ← FALSE
row ← 1
WHILE row <= rows AND found = FALSE DO
col ← 1
WHILE col <= cols AND found = FALSE DO
IF table[row][col] = target THEN
found ← TRUE
ELSE
col ← col + 1
ENDIF
ENDWHILE
row ← row + 1
ENDWHILE
Why Flag Is Critical Here
- Breaks out of multiple loops
- Without flags:
- Nested loops become uncontrollable
Common Flag Variable Mistakes (Very Frequent)
Mistake 1: Not Initialising Flag
IF found = TRUE THEN
foundmust be set to FALSE first
Mistake 2: Resetting Flag Incorrectly
FOR i ← 1 TO n
found ← FALSE
IF arr[i] = target THEN
found ← TRUE
ENDIF
NEXT i
- Resets flag every iteration
- Final value may be wrong
Mistake 3: Checking Flag Too Early
IF found = TRUE THEN
OUTPUT "Found"
ENDIF
- Must be checked after loop, not inside
Mistake 4: Using Flag Instead Of Counter
found ← TRUE
- Flags do not count occurrences
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
Flag Variables vs Counters (Quick Comparison)
| Feature | Flag | Counter |
|---|---|---|
| Data type | Boolean | Integer |
| Purpose | State detection | Quantity tracking |
| Typical values | TRUE / FALSE | 0, 1, 2, … |
| Early stopping | Yes | No |
| Counting occurrences | No | Yes |
Examiner-Approved Explanation Language
- “A Boolean flag is used to record whether the condition occurs”
- “The flag prevents unnecessary further processing”
- “The loop continues while the flag remains FALSE”
- “Once the flag becomes TRUE, the algorithm stops”
Exam-Ready Flag Variable Templates
Detection Template
flag ← FALSE
FOR i ← 1 TO n
IF condition THEN
flag ← TRUE
ENDIF
NEXT i
IF flag = TRUE THEN
OUTPUT "Yes"
ELSE
OUTPUT "No"
ENDIF
Early Termination Template
flag ← FALSE
i ← 1
WHILE i <= n AND flag = FALSE DO
IF condition THEN
flag ← TRUE
ELSE
i ← i + 1
ENDIF
ENDWHILE
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
- Flags store state, not data
- Always initialise flags
- Update flags inside logic blocks
- Check flags after loops
- Flags enable early exit and clean logic
