EEasy IGCSELabTracker

On a phone the notes, quizzes and tracker work fine. The 3D models and the practice lab are made for a laptop.

Specification points 8.1.1–8.1.8 ·

variables, constants and data types, input and output, operators, sequence, selection, iteration, totalling and counting, string handling, nesting, procedures and functions, local and global variables, library routines, maintainable programs.

1Variables, constants and data types

Definition

Variable — a named data store whose value can change while the program runs.

Constant — a named data store whose value cannot change while the program runs.

DECLARE Counter : INTEGER      // variable: identifier and data type
CONSTANT TaxRate ← 0.2         // constant: identifier and a literal value
Counter ← 0                    // ← assigns the value on the right
Counter ← Counter + 1          //   to the variable on the left
  • A constant is given its value once, when it is declared, and only a literal may be used (not a variable or an expression). Use constants for fixed values: the program is easier to read, and if the value changes it is edited in one place, so no copy is missed.
  • Identifiers use Pascal case (TotalPrice): letters and digits only, starting with a capital letter; no spaces, underscores or keywords.
Data type Stores Literals
INTEGER a whole number 5, -3
REAL a number with a fractional part 4.7, -4.0 — always a digit on each side of the point
CHAR a single character 'x', '@' — single quotes
STRING a sequence of zero or more characters "Hello", "" — double quotes
BOOLEAN one of two logical values TRUE, FALSE

Data made of digits that is never calculated with, such as a phone number "07700 900123", is stored as a STRING (it keeps the leading 0 and the space).

2Input and output

OUTPUT "Enter your age"                 // prompt: tells the user what to type
INPUT Age                               // stores the value typed in variable Age
OUTPUT "You are ", Age, " years old"    // several values separated by commas

INPUT stores the value in a variable or in one element of an array.

3Operators

Type Operators Result
Arithmetic + - * / ^ (raised to the power of), MOD, DIV a number
Relational = < <= > >= <> (not equal to) TRUE / FALSE
Logical AND (both true), OR (at least one true), NOT (reverses) TRUE / FALSE
  • / gives a real result: 7 / 2 is 3.5. DIV(7, 2) is 3 (whole-number quotient) and MOD(7, 2) is 1 (remainder) — both work on integers.
  • * and / are done before + and -. Use brackets to make the order explicit: Average ← (Mark1 + Mark2) / 2.
  • Each part of a combined condition must be complete: Mark >= 0 AND Mark <= 100.

4Sequence

Definition

Sequence — statements are executed one after another, in the order in which they are written.

Order matters: a value must be input or calculated before it is used — Area ← Length * Width must come after INPUT Length and INPUT Width.

5Selection

Definition

Selection — a condition is tested and its result decides which statements are executed.

IF … THEN … ELSE … ENDIF
IF Mark >= 50
  THEN
    OUTPUT "Pass"
  ELSE
    OUTPUT "Fail"
ENDIF

ELSE is optional: without it, nothing happens when the condition is FALSE.

CASE OF … OTHERWISE … ENDCASE
CASE OF Grade
  'A' : Points ← 3
  'B' : Points ← 2
  'C' : Points ← 1
  OTHERWISE Points ← 0
ENDCASE

Cases are tested in order; the first match runs and the rest are skipped. OTHERWISE (optional, always last) runs if no case matches.

Use IF for any condition — ranges, AND/OR, comparing two variables. Use CASE to choose between several single values of one variable: it is neater than a chain of IFs.

6Iteration

Definition

Iteration — a set of statements is repeated (a loop).

Count-controlled — FOR Pre-condition — WHILE Post-condition — REPEAT Count ← 1 Count <= 5 ? Yes statements Count ← Count + 1 No after loop condition ? True statements False after loop statements condition ? False True after loop Repeats a set number of times Tests first: may run 0 times; repeats while condition TRUE Tests last: runs at least once; stops when condition TRUE
Figure 1. The three types of loop. The position of the condition test is the difference.

Run the three loops

Loading the interactive…

  • Count-controlled — FOR Count ← 1 TO 5 … NEXT Count — when the number of repetitions is known, e.g. 30 students. The loop variable is an INTEGER that takes every value from the first to the last inclusive; STEP sets a different increment, which can be negative: FOR Count ← 10 TO 0 STEP -2.
  • Pre-condition — WHILE <condition> DO … ENDWHILE — when the loop may not need to run at all.
  • Post-condition — REPEAT … UNTIL <condition> — when the statements must run at least once, e.g. input validation: keep asking until the input is valid.
WHILE example
WHILE Number > 9 DO
    Number ← Number - 9
ENDWHILE
REPEAT example
REPEAT
    OUTPUT "Enter the password"
    INPUT Password
UNTIL Password = "Secret"

7Totalling and counting

Definition

Totalling — keeping a running total by adding each new value: Total ← Total + Value.

Counting — adding 1 each time something happens: Count ← Count + 1.

The total and the counter must be initialised (usually to 0) before the loop.

Total ← 0
PassCount ← 0
FOR Student ← 1 TO 30
    INPUT Mark
    Total ← Total + Mark                // totalling
    IF Mark >= 50
      THEN
        PassCount ← PassCount + 1       // counting
    ENDIF
NEXT Student
OUTPUT "Average mark: ", Total / 30
OUTPUT "Number of passes: ", PassCount

Finding the maximum, minimum and average are standard methods (→ Topic 7).

8String handling

Function Returns Example
LENGTH(Text) the number of characters LENGTH("Happy Days") → 10
SUBSTRING(Text, Start, Length) Length characters starting at position Start SUBSTRING("Happy Days", 1, 5) → "Happy"
UCASE(Text) the string or character in upper case UCASE("Happy") → "HAPPY"
LCASE(Text) the string or character in lower case LCASE('W') → 'w'
H 1 a 2 p 3 p 4 y 5 space 6 D 7 a 8 y 9 s 10 position LENGTH("Happy Days") → 10 SUBSTRING("Happy Days", 7, 4) → "Days" Positions usually start at 1 (some languages start at 0). The space counts as a character.
Figure 2. Character positions. Cambridge pseudocode normally starts at position 1.

Try the string routines

Loading the interactive…

Two common uses:

FOR Position ← 1 TO LENGTH(Word)            // process one character at a time
    OUTPUT SUBSTRING(Word, Position, 1)
NEXT Position

IF UCASE(Answer) = "YES"                    // accepts "yes", "Yes", "YES" …
  THEN
    OUTPUT "Confirmed"
ENDIF

9Nested statements

Definition

Nested statement — a selection or iteration statement placed inside another selection or iteration statement.

The loop in §7 has an IF nested inside a FOR; nested loops are used for 2D arrays (→ 8.2). Nested IFs test one condition only when another is already known:

IF Mark >= 70
  THEN
    Grade ← 'A'
  ELSE
    IF Mark >= 50                // only reached when Mark < 70
      THEN
        Grade ← 'B'
      ELSE
        Grade ← 'C'
    ENDIF
ENDIF

Indentation shows which ENDIF, NEXT or ENDWHILE closes which statement. You will not be asked to write more than three levels of nesting.

10Procedures, functions and parameters

Definition

Procedure — a named set of statements, defined once, that performs a task and can be called from any point in the program.

Function — like a procedure, but it returns a single value to the point where it was called.

Parameter — a variable in the procedure or function header that receives a value (an argument) passed to it when it is called.

Writing a task once and calling it many times makes programs shorter, avoids repeated code, lets each subroutine be tested on its own, and makes the program easier to maintain.

Procedure Function
Returns a value no yes — one value, sent back with RETURN
Header PROCEDURE Name(Param : TYPE) … ENDPROCEDURE FUNCTION Name(Param : TYPE) RETURNS TYPE … ENDFUNCTION
Called by CALL Name(Argument) — a complete statement its name inside an expression, e.g. X ← Name(Argument); never CALL
Procedure example
PROCEDURE Stars(Number : INTEGER)       // defined at the start of the code
    DECLARE Count : INTEGER
    FOR Count ← 1 TO Number
        OUTPUT '*'
    NEXT Count
ENDPROCEDURE

CALL Stars(7)                           // outputs 7 stars
Main program RoomArea ← Area(4.0, 5.5) OUTPUT RoomArea Function definition FUNCTION Area(Length : REAL, Width : REAL) RETURNS REAL RETURN Length * Width ENDFUNCTION 1 Arguments are passed to parameters in order: 4.0 → Length, 5.5 → Width 2 RETURN sends back 22.0, which replaces the call: RoomArea ← 22.0
Figure 3. Calling a function: arguments go in, one value comes back.

Follow a function call

Loading the interactive…

  • Up to three parameters, each with its data type. The arguments in the call must match the parameters in number, order and data type.
  • Without parameters: PROCEDURE Line … CALL Line, or FUNCTION GetMark RETURNS INTEGER.

11Local and global variables

Main program — global scope DECLARE Total : INTEGER // global PROCEDURE AddBonus — local scope DECLARE Bonus : INTEGER // local Bonus ← 5 Total ← Total + Bonus ✓ global can be used here ENDPROCEDURE OUTPUT Bonus ✗ error: Bonus does not exist outside AddBonus
Figure 4. Scope: where in the program a variable can be used.

Global or local?

Loading the interactive…

Definition

Global variable — declared in the main program; it can be used in any part of the program.

Local variable — declared inside a procedure or function; it can be used only within that subroutine.

Prefer local variables: they cannot be changed by mistake elsewhere in the program, the same name can be reused in different subroutines, and each subroutine is self-contained, so it is easier to test and reuse. Parameters behave as local variables.

12Library routines

Definition

Library routine — a pre-written, pre-tested procedure or function supplied with the programming language, ready to be called in any program. It saves writing and testing that code again.

Routine Returns Example
MOD(A, B) the remainder of A ÷ B MOD(10, 3) → 1
DIV(A, B) the whole‑number quotient of A ÷ B DIV(10, 3) → 3
ROUND(Value, Places) Value rounded to Places decimal places ROUND(6.97354, 2) → 6.97
RANDOM() a random real number from 0 to 1 inclusive e.g. 0.4271
  • MOD(Number, 2) = 0 is TRUE when Number is even.
  • Dice ← ROUND(RANDOM() * 5, 0) + 1 gives a whole number from 1 to 6.

13Maintainable programs

A maintainable program is one that another programmer — or you, later — can easily understand, correct and update. Make it maintainable with:

  • meaningful identifiers for variables, constants, arrays, procedures and functions — the name says what it stores or does (TotalPrice, CalculateTax, not X, P1);
  • comments (// in pseudocode; the language's own commenting feature in code) explaining the purpose of the program, of each procedure and function, and of any statement whose purpose is not obvious;
  • procedures and functions, so each task is a separate, named module.
Traps
  • CALL is only for procedures. A function is used inside an expression: Tax ← CalcTax(Price).
  • REPEAT … UNTIL stops when the condition becomes TRUE; WHILE continues while it is TRUE.
  • Initialise totals and counters before the loop — resetting them inside it loses the total.
  • = compares (in a condition); ← assigns: Count ← Count + 1, never Count = Count + 1.
  • A number stored as a STRING ("25") cannot be used in arithmetic.
  • Every structure needs its closing keyword: ENDIF, ENDCASE, NEXT <variable>, ENDWHILE, UNTIL, ENDPROCEDURE, ENDFUNCTION.
Exam language

In Paper 2, any answer that involves code must be written in pseudocode — program code scores nothing — except the 15-mark scenario question, which also accepts Python, Visual Basic or Java. Logic matters more than exact syntax, but conditions must be precise: Mark > 50, not "mark is more than 50".

Quick check

14 multiple-choice questions. Pick an answer to see whether it is right and why.

  1. A membership number such as 004512 is never used in a calculation. Which data type should store it?

  2. Which statement correctly declares a constant?

  3. What is the value of DIV(23, 4) + MOD(23, 4)?

  4. What is the value of X after X ← 2 + 6 * 3 - 4?

  5. A CASE OF Size statement has the cases 'S' : Price ← 5 and 'M' : Price ← 8, then OTHERWISE Price ← 10. What is Price when Size is 'L'?

  6. Nested IFs set Grade ← 'A' when Mark >= 70, otherwise Grade ← 'B' when Mark >= 50, otherwise Grade ← 'C'. What is Grade when Mark is 50?

  7. How many times are the statements inside FOR Count ← 10 TO 0 STEP -2 … NEXT Count executed?

  8. X ← 5 is followed by REPEAT, X ← X + 1, UNTIL X > 3. What is the final value of X?

  9. A loop FOR I ← 1 TO 3 contains Total ← 0 followed by Total ← Total + I. What is Total after the loop?

  10. What does SUBSTRING("Keyboard", 4, 5) return?

  11. A function Twice(N : INTEGER) RETURNS INTEGER contains only RETURN N * 2. What is X after X ← Twice(4) + 1?

  12. DECLARE Count : INTEGER is written inside PROCEDURE Stars. Where can Count be used?

  13. RANDOM() returns 0.8. What value does ROUND(RANDOM() * 5, 0) + 1 give?

  14. Which change makes a program more maintainable?

Exam-style questions

Write your answer, then show the answer and compare. Nothing here is marked for you: you decide how close you were. What you write is saved in this browser.

Recall

1

Define the terms variable and constant.

2 marks
2

State the most suitable data type for: (a) the number of people in a room (b) a price of 4.99 (c) whether a door is locked (d) a postcode (e) a single grade letter.

5 marks
3

State the value returned by: (a) DIV(17, 5) (b) MOD(17, 5) (c) ROUND(3.456, 1) (d) LENGTH("Computer") (e) SUBSTRING("Computer", 4, 3) (f) UCASE("abc").

6 marks

Understanding

4

Explain the difference between a pre-condition loop and a post-condition loop.

2 marks
5

Explain the difference between a procedure and a function.

2 marks
6

Explain why local variables are usually used instead of global variables.

2 marks
7

A program uses the VAT rate 0.2 in several places. Explain why it should be a constant.

2 marks
8

Describe three ways a programmer can make a program maintainable.

3 marks

Tracing and errors

9

Complete a trace table (Number, Total, Count, output) for this algorithm.

Total ← 0
Count ← 0
Number ← 5
WHILE Number > 0 DO
    Total ← Total + Number
    Count ← Count + 1
    Number ← Number - 2
ENDWHILE
OUTPUT Total, " ", Count
4 marks
10

This algorithm should total 10 valid marks (0–100). Identify three errors and give the corrections.

Count ← 1
REPEAT
    INPUT Mark
    IF Mark > 100
      THEN
        OUTPUT "Invalid"
    ENDIF
    Total = Total + Mark
UNTIL Count = 10
6 marks

Writing pseudocode

11

Write an algorithm that inputs a whole number and keeps rejecting it, with a message, until it is between 1 and 50 inclusive.

4 marks
12

Write a function Largest that takes two integers and returns the larger. Show how it is called and the result output.

4 marks
13

Write a CASE statement for a menu choice Choice (a CHAR): 'A' calls procedure AddItem, 'Q' outputs "Goodbye", anything else outputs "Invalid choice".

3 marks
14

Write an algorithm that inputs a word and outputs how many times the letter e appears, in upper or lower case.

5 marks
15

Write an algorithm that simulates 100 rolls of a six-sided dice and outputs how many sixes were rolled.

4 marks

How well do you know this?

Rate each specification point. It is saved to your tracker as a new revision round.

All points:
  • 8.1.1Declare and use variables and constants
  • 8.1.2Understand and use basic data types
  • 8.1.3Understand and use input and output
  • 8.1.4
    • Understand and use the concept of sequence
    • Understand and use the concept of selection
    • Understand and use the concept of iteration
    • Understand and use the concepts of totalling and counting
    • Understand and use the concept of string handling
  • 8.1.5Understand and use nested statements
  • 8.1.6
    • Understand what is meant by procedures, functions and parameters
    • Define and use procedures and functions, with or without parameters
    • Understand and use local and global variables
  • 8.1.7Understand and use library routines
  • 8.1.8Understand how to create a maintainable program

Settings

Your subjects

Untick the subjects you don't take. They leave the menu, the home page and the tracker. Your ratings are kept, so you can bring a subject back at any time.

Page style

Colours