Skip to main content
SingaporeComputer ScienceSyllabus dot point

How do we write an algorithm in pseudocode using assignment, input, output, selection and loops?

Write algorithms in pseudocode using input, output, assignment, selection (IF) and iteration (WHILE, FOR)

A focused answer to the O-Level Computing point on pseudocode. Writing algorithms with input, output, assignment, IF selection, and WHILE and FOR loops, in clear language-independent steps.

Generated by Claude Opus 4.87 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. The answer
  3. Examples in context
  4. Try this

What this dot point is asking

SEAB wants you to write algorithms in pseudocode using the core building blocks: input, output, assignment, selection with IF, and iteration with WHILE and FOR. The central idea is that pseudocode is a plain, structured way to describe an algorithm in words and keywords, close to code but not tied to any one programming language, so you can plan the logic before writing Python.

The answer

What pseudocode is

Pseudocode is a structured, language-independent way of writing an algorithm. It uses a small set of keywords and plain phrases, with indentation to show structure. It does not have to run on a computer; its job is to make the logic clear and easy to translate into real code.

Input, output and assignment

  • Input reads a value: INPUT age
  • Output displays a value: OUTPUT "Hello"
  • Assignment stores a value in a variable: total = 0 (read as "total becomes 00").

Assignment puts the value on the right into the variable on the left. total = total + 5 means take the current total, add 55, and store the result back.

Selection with IF

Selection chooses between paths:

IF mark >= 50 THEN
    OUTPUT "Pass"
ELSE
    OUTPUT "Fail"
ENDIF

The THEN branch runs if the condition is true; the ELSE branch runs otherwise. The ENDIF marks where the selection finishes.

Iteration with WHILE

A WHILE loop repeats while a condition is true. It is used when you do not know in advance how many times to loop:

count = 0
WHILE count < 5
    OUTPUT count
    count = count + 1
ENDWHILE

The condition is tested before each pass; something inside must move it toward false, or the loop never ends.

Iteration with FOR

A FOR loop repeats a known number of times using a counter:

FOR i = 1 TO 10
    OUTPUT i
NEXT i

The counter i runs from 11 to 1010. Use FOR when the number of repetitions is fixed, and WHILE when it depends on a condition.

Examples in context

Example 1. An ATM PIN check. Pseudocode for a cash machine reads the PIN, then uses a WHILE loop that keeps asking while the PIN is wrong and attempts remain, with an IF inside to count attempts. Writing it in pseudocode first makes the security logic clear before any code is typed.

Example 2. Averaging exam marks. A teacher's algorithm uses a FOR loop to read a known number of marks into an accumulator, then divides by the count to find the average. Because the number of students is known, FOR is the natural choice, and the pseudocode maps almost directly to Python.

Try this

Q1. Write pseudocode that inputs a number and outputs "Even" or "Odd". [3 marks]

  • Cue. INPUT n; IF n MOD 2 = 0 THEN OUTPUT "Even" ELSE OUTPUT "Odd" ENDIF.

Q2. State when you would use a WHILE loop rather than a FOR loop. [2 marks]

  • Cue. When the number of repetitions is not known in advance and depends on a condition.

Q3. Explain what score = score + 1 does. [2 marks]

  • Cue. It takes the current value of score, adds 11, and stores the result back in score.

Exam-style practice questions

Practice questions written in the style of SEAB exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

Original5 marksWrite pseudocode for an algorithm that reads 1010 numbers and outputs their total. Use a FOR loop and an accumulator.
Show worked answer →

Set a running total to zero, then add each input inside a loop:

total = 0
FOR i = 1 TO 10
    INPUT number
    total = total + number
NEXT i
OUTPUT total

The variable total is the accumulator: it starts at 00 and grows by each number. The FOR loop runs exactly 1010 times, and the output happens once after the loop ends.

Markers reward initialising total to 00, a FOR loop running 1010 times, adding each number to the total, and outputting after the loop.

Original5 marksWrite pseudocode that keeps asking the user to enter a positive number, and stops only when they do, then outputs the number. Use a WHILE loop.
Show worked answer →

Read once, then loop while the value is not valid:

INPUT number
WHILE number <= 0
    OUTPUT "Please enter a positive number"
    INPUT number
ENDWHILE
OUTPUT number

The first INPUT gives the loop something to test. The WHILE repeats while the number is not positive (≤0\le 0), asking again each time, and exits as soon as a positive number is entered.

Markers reward an initial input, a WHILE condition that repeats while the input is invalid, a re-prompt inside the loop, and output after the loop.

Related dot points