Implement and write pseudocode from a given design presented as either a program flowchart or structured English

Published by Patrick Mutisya · 14 days ago

Cambridge A-Level Computer Science 9618 – Programming Basics (11.1)

11.1 Programming Basics

Learning Objective

Implement and write pseudocode from a given design presented as either a program flowchart or structured English.

Why Pseudocode?

  • Language‑independent description of an algorithm.
  • Bridges the gap between design (flowchart/structured English) and actual code.
  • Helps to check logic before programming.

Key Conventions for Pseudocode

  1. Upper‑case reserved words: IF, THEN, ELSE, FOR, WHILE, END IF, etc.
  2. Indentation to show nesting.
  3. Variable names should be meaningful and follow camelCase or snake_case.
  4. Input/Output statements: READ, WRITE (or OUTPUT).
  5. Mathematical expressions written as they appear in mathematics, e.g., \$total = total + price \times quantity\$.

From a Flowchart to Pseudocode

A flowchart uses symbols to represent actions, decisions, loops, and start/end points. The conversion process is:

  1. Identify the start and end symbols.
  2. Read the flow from top to bottom (or left to right).
  3. Translate each process box to a single pseudocode statement.
  4. Translate each decision diamond to an IF … THEN … ELSE … END IF construct.
  5. Translate loops (e.g., “repeat until”) to WHILE or FOR structures.

Suggested diagram: Simple flowchart for calculating the sum of the first n natural numbers.

Example Flowchart

Design: Compute the sum \$S = 1 + 2 + \dots + n\$ where \$n\$ is entered by the user.

Pseudocode

READ n

sum ← 0

i ← 1

WHILE i ≤ n DO

sum ← sum + i

i ← i + 1

END WHILE

WRITE "The sum is ", sum

From Structured English to Pseudocode

Structured English is a semi‑formal description using English words mixed with programming keywords. To convert:

  1. Replace “Get” or “Input” with READ.
  2. Replace “Display” or “Output” with WRITE (or OUTPUT).
  3. Identify conditional phrases (“If … then … else …”) and map them to IF … THEN … ELSE … END IF.
  4. Identify repetition phrases (“Repeat … until …” or “For each …”) and map them to WHILE or FOR loops.
  5. Keep the logical order; indentation reflects nesting.

Suggested diagram: Structured English for finding the maximum of three numbers.

Example Structured English

Get three numbers A, B and C.

If A > B then

If A > C then

Display A as the maximum.

Else

Display C as the maximum.

End if

Else

If B > C then

Display B as the maximum.

Else

Display C as the maximum.

End if

End if

Pseudocode

READ A, B, C

IF A > B THEN

IF A > C THEN

WRITE "Maximum is ", A

ELSE

WRITE "Maximum is ", C

END IF

ELSE

IF B > C THEN

WRITE "Maximum is ", B

ELSE

WRITE "Maximum is ", C

END IF

END IF

Common Pitfalls & How to Avoid Them

  • Missing END statements: Every IF, WHILE, and FOR must have a matching END IF, END WHILE, or END FOR.
  • Incorrect loop condition: Ensure the loop condition matches the intended repetition (e.g., i ≤ n vs i < n).
  • Variable initialization: Initialise counters and accumulators before they are used.
  • Ambiguous variable names: Use descriptive names to avoid confusion when reading the pseudocode.

Summary Table

Design ElementFlowchart SymbolStructured English CuePseudocode Equivalent
Start / EndOval“Begin”, “End”None (implicit)
Process / AssignmentRectangle“Set”, “Assign”variable ← expression
Decision / ConditionDiamond“If … then … else …”IF condition THEN … ELSE … END IF
Loop (repeat)Diamond with back‑arrow“Repeat … until …”WHILE condition DO … END WHILE
Input / OutputParallelogram“Get …”, “Display …”READ … / WRITE …

Practice Questions

  1. Given a flowchart that reads an integer \$n\$, then prints all even numbers from 2 up to \$n\$, write the corresponding pseudocode.
  2. Convert the following structured English into pseudocode:

    Get a list of marks.

    Set total to 0.

    For each mark in the list do

    Add mark to total.

    End for.

    Calculate average = total / number of marks.

    Display average.

  3. Identify the error in the pseudocode below and correct it:

    READ x

    IF x > 0 THEN

    WRITE "Positive"

    ELSE IF x = 0 THEN

    WRITE "Zero"

    END IF