Understand and use basic data types

Programming – Basic Data Types (IGCSE 0478)

In any programming language a data type tells the computer what kind of value is being stored and which operations are permitted. Mastery of the basic data types is essential for writing correct programs and for answering exam questions in the Cambridge IGCSE Computer Science syllabus.

1. Why Data Types Matter

  • Determine the amount of memory a variable occupies.
  • Restrict the set of permissible values, helping to avoid logical errors.
  • Decide which operators (arithmetic, logical, concatenation, etc.) can be applied.
  • Smaller types usually run faster and use less storage.

2. Number Systems & Binary Representation

All data types are stored as binary (base‑2) patterns. Understanding the link between size, binary/hexadecimal representation and overflow is a core part of Topic 1.1 – Number systems.

  • Binary – each bit is 0 or 1.
  • Denary (decimal) – the number system we use in everyday life.
  • Hexadecimal – base‑16, useful for compactly writing binary values (0‑9, A‑F).
  • Two’s‑complement – the standard way of representing signed integers.
  • Logical shift – moves bits left or right, useful for fast multiplication/division by powers of two.

2.1 Bit‑level limits

BitsSigned range (Two’s‑complement)Unsigned range
8-2⁷ … 2⁷‑1 (‑128 … 127)0 … 2⁸‑1 (0 … 255)
16-2¹⁵ … 2¹⁵‑1 (‑32 768 … 32 767)0 … 65 535
32-2³¹ … 2³¹‑1 (‑2 147 483 648 … 2 147 483 647)0 … 4 294 967 295

Example – 8‑bit signed integer

Binary : 1111 1010
Two’s‑complement → invert bits (0000 0101) + 1 = 0000 0110 = 6
Result = -6

If an operation produces a value outside the defined range an overflow occurs; the value “wraps around” to the opposite end of the range.

3. Common Data Types in the IGCSE Syllabus

Data Type Typical Size (bits) Range / Values Example in Pseudocode
Integer 16 (default) or 32 -215 … 215‑1 (16‑bit)
-231 … 231‑1 (32‑bit)
DECLARE age : INTEGER ← 17
Real (Float) 32 (single precision) ≈ ±3.4 × 1038 with about 6‑7 decimal digits of precision DECLARE price : REAL ← 12.99
Boolean 1 (often stored as a byte) TRUE or FALSE DECLARE isValid : BOOLEAN ← TRUE
Character 8 (ASCII) or 16 (Unicode) Any single printable symbol, e.g. ‘A’, ‘7’, ‘$’ DECLARE grade : CHAR ← 'A'
String 8 per character (ASCII) or 16 per character (Unicode) Sequence of characters, length ≥ 0 DECLARE name : STRING ← "Alice"
Array (List) Size = element size × number of elements Ordered collection of elements of the same type DECLARE scores : ARRAY[1:10] OF INTEGER

4. Detailed Look at Each Type

4.1 Integer

  • Used for counting, indexing arrays, and any situation where fractions are not required.
  • Typical operators: +, -, *, /, % (modulus).
  • Watch for overflow – if the result lies outside the defined range the value “wraps around”.
  • Example of overflow (8‑bit):
    DECLARE x : INTEGER ← 120
    x ← x + 10 ⟹ -126 (overflow)

4.2 Real (Float)

  • Store numbers with a decimal point; ideal for measurements and scientific calculations.
  • Same arithmetic operators as integers, but results are rounded to the available precision (≈6‑7 decimal digits).
  • Never test equality directly; use a tolerance, e.g. ABS(a‑b) < 0.0001.
  • Rounding example:
    DECLARE a : REAL ← 0.1 + 0.2 ⟹ 0.3000000119 (rounded)

4.3 Boolean

  • Only two possible values: TRUE or FALSE.
  • Logical operators: AND, OR, NOT (short‑circuit in most languages).
  • Fundamental for IF statements, WHILE loops and validation checks.

4.4 Character & String

  • Character – a single symbol stored using an encoding such as ASCII (8 bits) or Unicode (16 bits).
  • String – a sequence of characters; length is stored or can be obtained with LENGTH().
  • Common operations:
    • Concatenation: fullName ← firstName + " " + lastName
    • Length: LENGTH(myString)
    • Indexing (1‑based): myString[3]
    • Slicing: myString[2:5]

4.5 Array (List)

  • Holds multiple values of the same type; indexed from a lower bound to an upper bound.
  • The IGCSE syntax is DECLARE name : ARRAY[lower:upper] OF type. The lower bound may be 0 or 1 – always state it explicitly.
  • Example declaration (10 integers, index 1‑10):
    DECLARE Scores : ARRAY[1:10] OF INTEGER
  • Accessing and updating elements:
    value ← Scores[3] ⟹ third element
    Scores[1] ← 100 ⟹ update first element
  • Iterating with a FOR loop:
    FOR i ← 1 TO LENGTH(Scores) DO
        OUTPUT "Score", i, ":", Scores[i]
    END FOR
  • Two‑dimensional array example (matrix 3×3) and nested loop:
    DECLARE matrix : ARRAY[1:3,1:3] OF INTEGER
    FOR r ← 1 TO 3 DO
        FOR c ← 1 TO 3 DO
            matrix[r,c] ← r*c          ⟹ fill with multiplication table
        END FOR
    END FOR

5. Data Storage & Compression (Topic 1.3)

Example – image size

DECLARE image : ARRAY[1:640*480] OF BYTE   ⟹ 640 × 480 × 8 = 2 457 600 bits
→ 307 200 bytes → 300 KiB (≈0.3 MiB)

Run‑Length Encoding (RLE) – lossless compression

Original bytes :  A A A A B B C C C
RLE output     :  (4,A) (2,B) (3,C)
Stored bits    :  (count 3 bits + symbol 8 bits) × 3 = 33 bits

RLE replaces consecutive identical values with a count‑value pair, reducing file size without losing information.

6. Data Transmission (Topic 2)

  • Data are sent as binary packets. Each field in a packet has a defined data type (e.g., an INTEGER for a sequence number, a CHAR[4] for a protocol identifier).
  • IP address – 32‑bit unsigned integer; often shown in dotted decimal (e.g., 192.168.0.1).
  • Error detection – parity bit, checksum, CRC. These are extra bits calculated from the data bytes and transmitted to verify integrity.
  • Encryption basics – symmetric algorithms treat the plaintext as a sequence of bytes (often BYTE or CHAR) and apply a key‑dependent transformation before transmission.

7. Hardware Context (Topic 3)

  • CPU registers – hold data temporarily; most registers are 32‑bit, so a 32‑bit integer fits in a single register, whereas a larger array must be fetched from RAM.
  • Cache – fast memory that stores recently used data; the size of a data type influences cache utilisation.
  • Embedded systems – often use fixed‑size types (e.g., 8‑bit BYTE) to meet memory constraints.
  • Understanding the size of a type helps you predict whether an operation can be performed in‑register or requires memory access.

8. Software Context (Topic 4)

  • Operating system (OS) allocates memory for variables, manages file handles, and schedules CPU time.
  • Interrupts – hardware or software signals that temporarily suspend the current program; the data passed to an interrupt routine must match the expected type (often INTEGER or CHAR).
  • Translators – compilers, interpreters and assemblers all need to know the data type of each identifier to generate correct machine code.
  • Integrated Development Environments (IDEs) provide syntax highlighting for data‑type declarations and can warn about type mismatches.

9. Databases & SQL (Topic 9)

When a program stores data in a relational database, each column has a type that mirrors the programming language types.

Programming typeTypical SQL column type
INTEGERINT, SMALLINT, BIGINT
REALFLOAT, DOUBLE, DECIMAL(p,s)
CHARCHAR(n)
STRINGVARCHAR(n), TEXT
BOOLEANBOOLEAN (or TINYINT(1) in some DBMS)

Choosing the correct column type prevents truncation, overflow, and unnecessary storage consumption.

10. Boolean Logic & Logic‑Gate Circuits (Topic 10)

  • Boolean variables correspond directly to binary values: TRUE = 1, FALSE = 0.
  • Logical operators map to logic gates:
    • AND → AND gate
    • OR → OR gate
    • NOT → NOT (inverter) gate
  • Truth table example for (A AND NOT B) OR C:
ABCResult
0000
0011
0100
0111
1001
1011
1100
1111

11. File Handling (Topic 8 – Programming)

The FILE type is used only in I/O statements. A complete file‑handling block must contain OPEN, the I/O operation, and CLOSE.

DECLARE inFile  : FILE
DECLARE outFile : FILE
DECLARE line    : STRING

OPEN inFile  FOR READ FROM "students.txt"
OPEN outFile FOR WRITE TO "report.txt"

WHILE NOT EOF(inFile) DO
    READLINE inFile INTO line
    WRITE outFile FROM line
END WHILE

CLOSE inFile
CLOSE outFile

Example of reading an array of integers from a file:

DECLARE scores : ARRAY[1:5] OF INTEGER
DECLARE i      : INTEGER ← 1

OPEN inFile FOR READ FROM "scores.txt"

WHILE NOT EOF(inFile) AND i ≤ 5 DO
    READ inFile INTO scores[i]
    i ← i + 1
END WHILE

CLOSE inFile

12. Validation, Testing & Trace Tables (Topic 7)

Choosing the correct data type helps you write validation checks and produce reliable trace tables.

DECLARE age : INTEGER
READ age FROM keyboard
IF age >= 0 AND age <= 120 THEN
    OUTPUT "Age accepted"
ELSE
    OUTPUT "Invalid age – must be 0‑120"
END IF

When constructing a trace table, record:

  • Variable name, type, and initial value.
  • Each statement number.
  • Values of all variables after the statement is executed.

13. Exam Tips

  1. Always state the data type when declaring a variable in pseudocode.
  2. Default integer size is 16‑bit unless the question explicitly says 32‑bit.
  3. Write conversions explicitly, e.g. INTEGER(price) or REAL(score).
  4. Use the exact Boolean operators required by the syllabus: AND, OR, NOT.
  5. For arrays, always show the lower and upper bounds and the element type, e.g. DECLARE Marks : ARRAY[0:4] OF INTEGER.
  6. In file‑handling questions, include the three mandatory statements: OPEN, the I/O operation, and CLOSE.
  7. When a question involves binary/hexadecimal or two’s‑complement, write the conversion steps clearly; this earns marks for understanding number systems.
  8. Remember that REAL numbers have limited precision – avoid direct equality tests.
  9. When calculating storage or transmission size, convert bits → bytes → KiB/ MiB and show your work.

14. Summary

Basic data types – integers, reals, Booleans, characters, strings, and arrays – are the building blocks of every program. Knowing their size, range, binary representation, and appropriate operations enables you to store information correctly, manipulate it safely, and produce clear, exam‑ready pseudocode. These concepts link directly to the wider IGCSE topics of number systems, data storage & compression, data transmission, hardware, software, databases, and Boolean logic.

Create an account or Login to take a Quiz

27 views
0 improvement suggestions

Log in to suggest improvements to this note.