[8.1.1-2] Variables, Constants, & Data Types

[8.1.1–2] Variables, Constants, & Data Types

Every piece of program code manipulates data. To do this safely and clearly, we give data a name (a variable or a constant) and a type (such as INTEGER or STRING) so the computer and the programmer know what operations make sense. In this lesson you will learn how to declare and use variables and constants, and how to choose between the core IGCSE data types: integer, real, char, string, and Boolean.

Variables and identifiers

A variable is a named storage location whose value can change while the program runs. The variable's identifier (its name) should be meaningful and follow language rules (e.g. start with a letter, no spaces). You typically declare a variable by stating its name and data type, and optionally giving it an initial value.

  • Good identifiers: studentCount, totalMarks, isValid.
  • Poor identifiers: x, thing, data1 (unclear purpose).

Constants

A constant is a named value that does not change during program execution, such as a maximum class size or VAT rate. Using constants avoids magic numbers and makes maintenance safer: you update the definition in one place and all code uses the new value.

  • Why constants help: improve readability, prevent accidental changes, simplify updates.
  • Naming: many programmers use capital letters with underscores, e.g. MAX_CLASS_SIZE.

Core IGCSE data types

Type What it stores Typical uses Example literals
INTEGER Whole numbers (no decimal point) Counts, indices, menu choices 0, -3, 42
REAL Numbers with fractional part Averages, prices, measurements 3.14, -0.5, 12.0
CHAR Single character Single-letter grades, menu keys 'A', '#', 'y'
STRING Sequence of characters (text) Names, messages, IDs "Mariam", "Year 11"
Boolean Truth values Flags, conditions, toggles TRUE, FALSE

Choosing the right type

Pick the narrowest type that correctly represents the value. Age is an INTEGER, a pupil's name is a STRING, and a light switch state is Boolean. Choosing correctly prevents mistakes such as trying to add a name to a number or comparing text in a numeric way.

Type rules: examples and edge cases

The same concept can behave differently depending on types. Explore the tabs to see how integers and reals affect division, how CHAR vs STRING changes what is allowed, and how Boolean expressions work.

Integer vs Real division

When both operands are INTEGER, some languages perform integer division, discarding any fractional part. With REALs, you keep the fraction. This choice affects calculations like averages.

# Python (demonstration)
7 // 2   # integer floor division → 3
7 / 2.0 # real division → 3.5

To avoid losing precision, cast to REAL before dividing or ensure at least one operand is REAL when you need a fractional answer.

CHAR vs STRING constraints

CHAR holds exactly one character, while STRING can hold many. Attempting to store several characters in a CHAR variable is invalid and should be caught by validation or by the language itself.

// CAIE Pseudocode
DECLARE grade : CHAR
grade ← 'A'       // OK
grade ← 'AB'      // Invalid: too many characters

Use CHAR for single keys or codes, and STRING for names, messages, or any text with length other than one.

Boolean expressions & precedence

Boolean expressions combine comparisons using AND, OR, and NOT. Precedence usually applies as: NOT first, then AND, then OR. Use brackets to make intent explicit.

// CAIE Pseudocode
DECLARE age : INTEGER ← 15
DECLARE hasConsent : BOOLEAN ← TRUE
IF (age >= 16) OR (age >= 13 AND hasConsent = TRUE) THEN
  OUTPUT "Allowed to participate"
ENDIF

Brackets prevent misinterpretation and clarify complex logic, especially when mixing AND and OR.

Declaring variables and constants (CAIE pseudocode & Python)

Below are small reference snippets showing typical declarations and assignments. Remember: use constants for fixed values and variables for values that change.

// CAIE Pseudocode
DECLARE studentName : STRING
DECLARE studentAge  : INTEGER
DECLARE average     : REAL
DECLARE initial     : CHAR
DECLARE passed      : BOOLEAN

studentName ← "Rizwan"
studentAge  ← 16
average     ← 74.5
initial     ← 'R'
passed      ← TRUE

CONSTANT MAX_MARK : INTEGER ← 100
# Python
student_name: str = "Rizwan"
student_age: int = 16
average: float = 74.5
initial: str = "R"   # single character by convention
passed: bool = True

MAX_MARK: int = 100  # treated as a constant by naming convention

Input and output with types

User input arrives as text. If you need a number, convert it. Failing to convert leads to string behaviour when you wanted arithmetic (e.g. "2" + "3" → "23" as text concatenation).

# Python
age_text = input("Enter your age: ")
age = int(age_text)      # convert to INTEGER
print(age + 1)           # safe arithmetic

Casting between types

Casting means converting a value from one type to another, such as converting a STRING to INTEGER after input. Only perform casts that make sense: numbers inside text can become numbers, but a name cannot become a number.

  • Safe cast: "123" → 123 (STRING → INTEGER)
  • Unsafe cast: "Alex" → ? (not a number) → causes an error

Deep Dive: Common pitfalls and how to avoid them

  • Integer division traps: Make sure to use REALs when calculating averages or rates.
  • CHAR vs STRING: Remember that a CHAR is exactly one character. Use STRING for names and sentences.
  • Booleans: Use clear names like isValid or hasPaid. Avoid double negatives such as notInvalid.
  • Magic numbers: Replace unexplained numbers with named constants for clarity.
  • Input conversion: Always convert numeric input before arithmetic to avoid accidental string concatenation.

 Key Takeaways

  • Use variables for values that change and constants for values that must not change.
  • Choose the narrowest appropriate data type: INTEGER, REAL, CHAR, STRING, or Boolean.
  • Be aware of type behaviour: INTEGER vs REAL division, CHAR vs STRING length, Boolean precedence.
  • Convert input to the correct type before processing; avoid magic numbers by using named constants.
  • Clear identifiers and consistent typing make programs easier to read, test, and maintain.