Use pseudocode (INPUT, WRITE, FOR, WHILE)

Algorithms and Flowcharts (Cambridge IGCSE/A‑Level IT 9626 – Topic 4)

Learning Objectives (Cambridge Assessment Objectives)

  1. AO1 – Knowledge & Understanding: recognise and use the standard pseudocode constructs required by the syllabus.
  2. AO2 – Application: write correct algorithms and flowcharts, edit given ones and translate between the two representations.
  3. AO3 – Analysis: evaluate the suitability of different control structures and identify errors in algorithms/flowcharts.

Key Concepts

  • What an algorithm is and the characteristics of a good algorithm.
  • Cambridge flow‑chart symbols and the conventions used in examinations.
  • Pseudocode conventions – case‑insensitive, indentation for readability, use of for assignment.
  • Control structures: sequential processing, looping, decision‑making, and sub‑routines.
  • Editing and error‑checking techniques for algorithms and flowcharts.

Full Syllabus Coverage (Topic 4)

Syllabus ItemPresent in Notes?Action Required
INPUT / READYes
WRITE / PRINTYes
FOR … NEXTYes
FOR … NEXT … STEPNoAdded in “Looping Constructs”.
IF … ELSE … ENDIFNoAdded in “Decision‑Making”.
WHILE … END WHILEYes
REPEAT … UNTILNoAdded in “Looping Constructs”.
CASE … ENDCASENoAdded in “Decision‑Making”.
Comparison & arithmetic operatorsImplicitExplicit table added.
Sub‑routines / proceduresNoNew “Sub‑routines” section introduced.
Editing a given algorithm / flowchartNoSpot‑the‑error exercises added.
Nested decisionsNoExample with grading added.

Pseudocode Syntax (Cambridge Standard)

StatementPurposeExample
INPUT / READRead data from the user or a fileINPUT n
WRITE / PRINTDisplay data on the screenWRITE "Result =", result
FOR i = start TO endLoop a known number of times (increment = 1)FOR i = 1 TO n
FOR i = start TO end STEP stepLoop with a user‑defined increment or decrementFOR i = 10 TO 0 STEP -2
END FORTerminate a FOR loopEND FOR
WHILE conditionRepeat while the condition is true (test before first iteration)WHILE total < limit
END WHILETerminate a WHILE loopEND WHILE
REPEATStart a loop that executes at least onceREPEAT
UNTIL conditionTerminate a REPEAT loop when the condition becomes true (test after each iteration)UNTIL password = "Cambridge"
IF condition THENSingle‑branch decisionIF score ≥ 50 THEN
ELSEAlternative branch for an IFELSE
ENDIFTerminate an IF … THEN … ELSE … ENDIF blockENDIF
CASE expression OFMulti‑way selectionCASE choice OF
WHEN value:Branch of a CASEWHEN 1: …
WHEN OTHER:Default branch of a CASEWHEN OTHER: …
ENDCASETerminate a CASE blockENDCASE
CALL subroutine (parameters)Invoke a sub‑routineCALL Factorial (n, result)
PROCEDURE subroutine (parameters)Define a sub‑routinePROCEDURE Factorial (n, result)
END PROCEDURETerminate a sub‑routine definitionEND PROCEDURE

Comparison & Arithmetic Operators (Cambridge)

OperatorMeaning
=Equal to
or #Not equal to
<Less than
>Greater than
Less than or equal to
Greater than or equal to
+Addition
-Subtraction
*Multiplication
/Division
MODRemainder after integer division

Flowchart Symbols (Cambridge Standard)

SymbolNameRepresents
🟠 (Oval)TerminatorStart or End of the process
▭ (Parallelogram)Input/OutputINPUT or WRITE
▭ (Rectangle)ProcessAssignment, calculation or sub‑routine call
◇ (Diamond)DecisionAny conditional test – IF, WHILE, REPEAT…UNTIL, CASE
⇆ (Connector)ArrowShows flow direction
⧉ (Circle)Sub‑routineCall and return points for procedures

Algorithm Constructs

1. Input / Output

  • Use INPUT (or READ) to obtain data.
  • Use WRITE (or PRINT) to display results.

2. Looping Constructs

  • FOR … NEXT: when the number of repetitions is known and the step is 1.
  • FOR … NEXT … STEP: same as above but with a custom increment or decrement.
  • WHILE … END WHILE: test before the first iteration; loop may execute zero times.
  • REPEAT … UNTIL: test after the first iteration; loop executes at least once.

3. Decision‑Making

  • IF … THEN … ENDIF: single‑branch decision.
  • IF … THEN … ELSE … ENDIF: two‑branch decision.
  • Nested IF … THEN …: decisions inside decisions.
  • CASE … OF … ENDCASE: multi‑way selection; useful for menus.

4. Sub‑routines / Procedures

  • Define a reusable block with PROCEDURE … END PROCEDURE.
  • Invoke it with CALL.
  • In flowcharts a circle (or double‑lined rectangle) denotes a sub‑routine.

5. Editing & Spot‑the‑Error Techniques (AO3)

  • Check that every loop has a matching start and end statement.
  • Verify that every IF has a corresponding ENDIF (and an ELSE if required).
  • Confirm that variable names are used consistently and that assignment uses .
  • Look for missing or extra arrows in flowcharts, especially at decision diamonds.

6. Translating Between Flowcharts and Pseudocode – Checklist (AO2)

  1. Identify each symbol and write the matching pseudocode statement.
  2. Follow the arrows to preserve the exact order of execution.
  3. For every decision diamond:
    • If the flow returns to the same point → use WHILE or REPEAT…UNTIL.
    • If the flow diverges once → use IF … THEN … ELSE … ENDIF or CASE.
  4. Mark the start and end of every loop with FOR … END FOR, WHILE … END WHILE or REPEAT … UNTIL.
  5. Ensure every input and output symbol is represented by INPUT or WRITE.
  6. When a sub‑routine symbol appears, write a CALL statement and verify that a matching PROCEDURE … END PROCEDURE exists.

Worked Examples

Example 1 – Sum of the First n Natural Numbers (FOR loop)

INPUT n
total ← 0
FOR i = 1 TO n
    total ← total + i
END FOR
WRITE "Sum =", total

Example 2 – Sum of Even Numbers up to n (FOR … STEP)

INPUT n
total ← 0
FOR i = 2 TO n STEP 2
    total ← total + i
END FOR
WRITE "Even sum =", total

Example 3 – Greatest Common Divisor (WHILE loop)

INPUT a
INPUT b
WHILE b ≠ 0
    temp ← b
    b ← a MOD b
    a ← temp
END WHILE
WRITE "GCD =", a

Example 4 – Password Check (REPEAT … UNTIL)

REPEAT
    INPUT password
UNTIL password = "Cambridge"
WRITE "Access granted"

Example 5 – Simple Menu (CASE … ENDCASE)

WRITE "1 – Add   2 – Subtract   3 – Exit"
INPUT choice
CASE choice OF
    WHEN 1:
        INPUT a, b
        result ← a + b
        WRITE "Result =", result
    WHEN 2:
        INPUT a, b
        result ← a - b
        WRITE "Result =", result
    WHEN 3:
        WRITE "Good‑bye"
    WHEN OTHER:
        WRITE "Invalid choice"
ENDCASE

Example 6 – Grading System (Nested IF … THEN …)

INPUT score
IF score ≥ 80 THEN
    grade ← "A"
ELSE
    IF score ≥ 70 THEN
        grade ← "B"
    ELSE
        IF score ≥ 60 THEN
            grade ← "C"
        ELSE
            grade ← "F"
        ENDIF
    ENDIF
ENDIF
WRITE "Grade =", grade

Example 7 – Factorial Using a Sub‑routine (PROCEDURE & CALL)

PROCEDURE Factorial (n, result)
    result ← 1
    FOR i = 2 TO n
        result ← result * i
    END FOR
END PROCEDURE

INPUT n
CALL Factorial (n, fact)
WRITE "Factorial of", n, "is", fact

Spot‑the‑Error Exercises (AO3)

Algorithm A – Incorrect Loop Bounds

INPUT n
total ← 0
FOR i = 1 TO n‑1
    total ← total + i
END FOR
WRITE total

Error: Loop stops at n‑1; the sum of the first n numbers is incomplete.

Correction: Change FOR i = 1 TO n‑1 to FOR i = 1 TO n.

Algorithm B – Mis‑used REPEAT

REPEAT
    INPUT answer
    WRITE "Try again"
UNTIL answer = correct
WRITE "Correct!"

Error: The message “Try again” is printed even when the first answer is correct.

Fix: Move the WRITE statement after the test, or replace the structure with a WHILE loop:

INPUT answer
WHILE answer ≠ correct
    WRITE "Try again"
    INPUT answer
END WHILE
WRITE "Correct!"

Flowchart C – Missing Decision Exit

Flowchart with a decision diamond but no false branch
The decision diamond “i ≤ n?” only has a “Yes” arrow back to the process. The “No” path to the output is missing.

Correction: Add a second arrow labelled “No” that leads to the WRITE symbol (or the terminator).

Algorithm D – Unmatched ENDIF

INPUT x
IF x > 0 THEN
    WRITE "Positive"
ELSE
    IF x = 0 THEN
        WRITE "Zero"
END IF

Error: Two IF statements but only one ENDIF. The inner IF is not closed.

Fix: Add an additional ENDIF after the “Zero” branch.

Practice Questions

  1. Write pseudocode to read a list of 10 integers and display the largest value using a FOR loop.
  2. Convert the following flowchart (provided by the exam board) into correct Cambridge‑style pseudocode. Include all required END statements.
  3. Design a menu‑driven program that allows the user to:
    • Calculate the area of a rectangle,
    • Calculate the area of a circle,
    • Exit.
    Use CASE … ENDCASE for the menu and appropriate loops to allow repeated use until the user chooses “Exit”.
  4. Write a sub‑routine called IsPrime that takes an integer n and returns TRUE if n is a prime number, otherwise FALSE. Show how you would call this procedure from a main algorithm.
  5. Given the algorithm below, identify and correct all errors (both logical and syntactic).
    INPUT n
    i ← 2
    WHILE i ≤ n
        IF n MOD i = 0 THEN
            WRITE n, "is not prime"
        ENDIF
        i ← i + 1
    END WHILE
    WRITE n, "is prime"
    

Summary Checklist for the Exam

  • Know the exact spelling and order of every Cambridge pseudocode keyword.
  • Remember that FOR loops always require END FOR; WHILE requires END WHILE; REPEAT pairs with UNTIL.
  • All IF structures terminate with ENDIF; include ELSE only when a second branch is required.
  • When translating a decision diamond that loops back, use WHILE or REPEAT…UNTIL; when it splits, use IF…ELSE or CASE.
  • Check indentation – it makes nested structures clear and helps avoid missing END statements.
  • In flowcharts, every diamond must have at least two outgoing arrows (Yes/No or multiple cases).

Create an account or Login to take a Quiz

49 views
0 improvement suggestions

Log in to suggest improvements to this note.