TopMyGrade

GCSE/Computer Science/OCR

2.2.1Programming concepts: variables, constants, sequence, selection (IF), iteration (WHILE, FOR), input/output

Notes

Programming fundamentals

OCR J277 Paper 2 includes code-reading, code-writing and algorithm design questions. You must be comfortable with OCR's reference language (pseudocode) and be able to write, read and trace code involving variables, selection and iteration.

Variables and constants

  • Variable: a named storage location whose value can change during program execution.
    • Syntax: name ← value (OCR pseudocode).
  • Constant: a named storage location whose value is fixed and cannot change.
    • Syntax: CONST name ← value
  • Why use constants? Easier to maintain code — change the value in one place; prevents accidental modification.

Sequence

Instructions executed one after another in the order they appear. The most fundamental concept in programming.

Selection (IF statements)

IF…THEN…ELSE

IF condition THEN
    statements
ELSE IF condition THEN
    statements
ELSE
    statements
END IF

SWITCH/CASE (for multiple specific values)

SWITCH variable:
    CASE value1: statements
    CASE value2: statements
    DEFAULT: statements
END SWITCH

Iteration (loops)

FOR loop (definite iteration — known number of repetitions)

FOR i ← 1 TO 10
    OUTPUT i
END FOR

WHILE loop (indefinite — repeats while condition is true)

WHILE condition DO
    statements
END WHILE

DO…WHILE (post-condition — always executes at least once)

DO
    statements
WHILE condition

Key distinction: WHILE checks the condition before each iteration (may never execute); DO WHILE checks after (executes at least once).

Input and Output

name ← USERINPUT       // get input from keyboard
OUTPUT "Hello, " + name  // display to screen

Common OCR exam mistakes

  1. Using FOR when the number of iterations is unknown — you need WHILE.
  2. Off-by-one errors: FOR i ← 1 TO 10 loops 10 times (1, 2, …, 10); FOR i ← 0 TO 9 also loops 10 times but indices are 0–9 (important for arrays).
  3. Infinite loops: forgetting to update the loop variable inside a WHILE loop.
  4. Not terminating loops correctly — always make sure the condition can become false.

AI-generated · claude-opus-4-7 · v3-ocr-computer-science

Practice questions

Try each before peeking at the worked solution.

  1. Question 13 marks

    Trace a FOR loop

    Trace the following pseudocode and show the output.

    total ← 0
    FOR i ← 1 TO 5
        total ← total + i
    END FOR
    OUTPUT total
    

    [3 marks]

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-ocr-computer-science

  2. Question 24 marks

    Write a WHILE loop

    Write pseudocode for a program that asks the user to enter a number between 1 and 10 (inclusive) and keeps asking until a valid number is entered.

    [4 marks]

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-ocr-computer-science

  3. Question 32 marks

    FOR vs WHILE

    State **one** situation where a FOR loop is appropriate and **one** where a WHILE loop is more appropriate. [2 marks]
    
    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-ocr-computer-science

Flashcards

2.2.1 — Programming concepts: variables, constants, sequence, selection (IF), iteration (WHILE, FOR), input/output

10-card SR deck for OCR Computer Science (J277) topic 2.2.1

10 cards · spaced repetition (SM-2)