Introduction: Your Data Toolkit
Welcome to one of the most fundamental chapters in Computer Science: Data types and structures!
Don't worry if this sounds complicated; it's really just about organising information.
Imagine you are packing for a trip. You don't put soup in a shoe box, or tiny earrings in a massive suitcase designed for clothes. You use the right container for the right item.
In programming, data types are the containers we use to store different kinds of information (numbers, letters, true/false values). Data structures are how we group these containers together efficiently. Getting this right is the key to writing efficient and powerful programs!
Quick Takeaway
Data types tell the computer what kind of information (number, text, etc.) a piece of data is, so it knows how much memory to allocate and what operations can be performed on it.
Section 1: The Essential Basic Data Types
These five basic types are the building blocks for all your programming projects. You need to know them inside and out!
1. Integer (INT)
The simplest number type.
Definition: Whole numbers (positive, negative, or zero) without any fractional or decimal part.
- Examples: 5, 100, -12, 0
- Use Case: Counting things (like the number of students), indexing lists, or storing age.
2. Real or Floating Point (FLOAT)
Sometimes just called 'Float'. These numbers need more memory because they contain precision.
- Definition: Numbers that contain a decimal point or fractional part.
- Examples: 3.14, -0.5, 99.99, 1.0
- Use Case: Storing measurements (like height or temperature), currency, or calculations requiring high precision.
🚨 Common Mistake Alert: Students often confuse Integers and Floats. If you are calculating money, use a Real, because £10.50 is not a whole number!
3. Character (CHAR)
A single symbol.
- Definition: Any single letter, digit, punctuation mark, or symbol.
- Examples: 'A', 'z', '5', '!', '$'. (Notice they are usually wrapped in single quotation marks.)
4. String (STR)
The most common way to store text.
- Definition: A sequence or collection of characters. Strings are essentially long lists of characters put together.
- Examples: "Hello World", "CS101", "My name is John". (Notice they are often wrapped in double quotation marks.)
- Did you know? Even if you type "123" in a string, the computer treats it as text, not a number you can perform math operations on.
5. Boolean (BOOL)
The ultimate simple choice! Booleans are essential for decision-making in programs.
- Definition: A type that can only hold one of two values: True or False.
- Use Case: Checking conditions (e.g., Is the light on?), managing program flow (IF/THEN statements).
Memory Aid: Know Your Types
Think about the first letter of each common type: Integer, Float (Real), Character, String, Boolean.
Section 2: Data Structures – Grouping Data Together
When you have thousands of pieces of data, you can't just store them in individual variables. You need structures to organise them neatly. We will focus on two key structures: Arrays and Records.
1. The Array (Lists of Same Type Data)
Think of an array as a tidy, numbered row of pigeonholes where every single hole must hold the exact same type of item (e.g., only numbers, or only names).
- Definition: A data structure used to store a fixed-size sequence of elements of the same data type.
- Indexing: Each element is accessed using an index (a numerical position). In most programming languages, this index usually starts at 0.
1.1 One-Dimensional (1D) Arrays
A simple, straight line or list.
Analogy: A simple shopping list where every item is a string (text).
If we have an array called Scores that holds 4 integers:
Scores = [ 85, 92, 78, 99 ]
- Scores[0] = 85 (The score at index 0)
- Scores[3] = 99 (The score at index 3)
To find the second element, you look at index 1 (since we start counting from 0).
1.2 Two-Dimensional (2D) Arrays
A 2D array is an array of arrays. It's like a grid, table, or spreadsheet, requiring two indices to locate an element: the Row index and the Column index.
Analogy: A chess board or a simple seating plan in a classroom.
Consider a small seating plan array (3 rows, 4 columns):
| (R0, C0) | (R0, C1) | (R0, C2) | (R0, C3) |
| (R1, C0) | (R1, C1) | (R1, C2) | (R1, C3) |
| (R2, C0) | (R2, C1) | (R2, C2) | (R2, C3) |
To access a specific seat, you need both coordinates:
If we wanted the item in the second row and the third column, we would look for ArrayName[1][2] (remembering the indices start at 0).
Key Takeaway for Arrays: Arrays are excellent for storing large amounts of uniform data (all numbers, or all strings).
2. The Record (Mixing Different Type Data)
Arrays are limited because they can only hold one type of data. What if you need to store related information about one single thing—like a student—where the data types are different?
This is where a Record (sometimes called a structure or 'struct') comes in.
- Definition: A data structure that allows you to group together related data items of potentially different data types.
- Fields: The individual data items within a record are called fields or attributes.
Analogy: An ID card or a contact entry on your phone. It contains a mix of text, numbers, and perhaps a picture flag.
Example: Defining a Student Record
We define a structure called StudentInfo with several fields:
Record StudentInfo:
Name: String
Age: Integer
GradeAverage: Real
IsEnrolled: Boolean
To create a student called Sam and access their data, you would use dot notation:
- Sam.Name would return a String (e.g., "Sam Jones")
- Sam.Age would return an Integer (e.g., 16)
- Sam.IsEnrolled would return a Boolean (e.g., True)
Comparison: Array vs. Record
Understanding this difference is vital!
| Feature | Array | Record |
|---|---|---|
| Data Types | Must all be the SAME type (e.g., all Integers). | Can be DIFFERENT types (e.g., String, Integer, Real). |
| Purpose | Storing lists of items or sequences. | Storing attributes/fields describing a single entity (like a Person). |
| Access Method | Accessed by numerical index (e.g., Array[3]). | Accessed by field name (e.g., Record.Name). |
Don't worry if this seems tricky at first. Practice drawing diagrams of 1D and 2D arrays, and label the indices. Once you understand the structure, the programming logic becomes much easier!
Chapter Review: Summary of Key Terms
- Integer: Whole numbers (no decimals).
- Real/Float: Numbers with decimals.
- String: A sequence of characters (text).
- Boolean: True or False values.
- Array (1D/2D): Stores multiple items of the same data type, accessed by numerical index.
- Record: Stores related data items which can be of different data types, accessed by field name.
You now have the fundamental knowledge to choose the correct storage solution for any data you encounter in your programming challenges! Keep practising, and good luck!