Welcome to Data Types and Structures!

In this chapter, we are going to learn how computers store and organize information. Think of a program like a chef following a recipe: the instructions are the code, but the ingredients are the data. To be a great programmer, you need to know exactly which "ingredients" you are working with and how to store them so they don't get messy!

1. Variables and Constants

Before we look at specific types of data, we need to know where we put them. In programming, we use "containers" to hold our information.

Variables

A variable is a location in the computer's memory where data can be stored. The important thing about a variable is that the value inside it can change while the program is running.
Example: In a video game, your "score" is a variable. It starts at 0 and goes up as you play.

Constants

A constant is also a memory location, but once you put a value in it, it cannot be changed. It stays the same the whole time the program runs.
Example: The value of Pi (\( \pi \)) or the number of levels in a game.

Quick Review:
Variable = Can change (like your age).
Constant = Stays the same (like your date of birth).

2. Primitive Data Types

Every piece of data has a "type" that tells the computer how to handle it. Here are the four "primitive" (basic) types you need to know:

1. Integer: These are whole numbers with no decimals. They can be positive or negative.
Examples: 10, -5, 0, 42

2. Real (or Float): These are numbers that have a decimal point.
Examples: 3.14, -0.5, 10.0

3. Boolean: This type can only ever have two values: True or False. Think of it like a light switch.
Example: IsGameOver = True

4. Character (Char): A single letter, number, or symbol.
Examples: 'A', '!', '3'

Memory Aid: Just remember I.R.B.C. (I Read Big Comics) to recall Integer, Real, Boolean, Character!

Key Takeaway: Choosing the right data type saves memory and prevents errors. You wouldn't try to do math with a Boolean or use an Integer to store a price like £1.99!

3. Structured Data Types

Sometimes, a single piece of data isn't enough. We need ways to group data together. These are called structured data types.

Strings

A string is a collection of characters joined together. We usually put them in quotation marks. Even though they look like words, the computer sees them as a "chain" of individual characters.
Example: "Hello World" or "Computer Science123"

Arrays (One-Dimensional)

An array is a list of items that are all the same data type stored under one name.
Analogy: Think of a 1D array like an egg carton. Each slot has an index number (starting at 0!), and you can put one item in each slot.

Arrays (Two-Dimensional)

A 2D array is like a table with rows and columns. To find a piece of data, you need two coordinates.
Analogy: Think of a 2D array like a cinema seating plan or a chessboard. To find a seat, you need the row number and the seat number.

Records

A record allows you to store a collection of related data items that can be of different data types.
Example: A "Student" record might contain:
- FirstName (String)
- Age (Integer)
- AttendancePercent (Real)

Common Mistake: Forgetting that arrays almost always start counting at 0, not 1! This is called zero-indexing.

4. String Manipulation

Programs often need to change or "manipulate" strings. Here are the four main tools you'll use in Paper 2:

1. Length: Finding out how many characters (including spaces!) are in a string.
Example: The length of "Cat" is 3.

2. Position: Finding where a specific character is. Remember to start counting at 0!
Example: In the string "Apple", the position of 'p' is 1.

3. Substring: Taking a smaller part out of a larger string.
Example: Taking a substring of "Strawberry" to get "Berry".

4. Case Conversion: Changing the text to all UPPERCASE or all lowercase.
Useful for: Checking if a user typed "YES", "yes", or "Yes" – you can convert it all to lowercase first to make it easy to check!

Did you know? Computers treat 'A' and 'a' as completely different things because they have different underlying binary codes!

Chapter Summary

Checklist: Can you...

- Explain the difference between a variable and a constant?
- Identify Integer, Real, Boolean, and Char types?
- Explain that an array holds items of the same type, while a record can hold different types?
- Calculate the length or find a substring of a piece of text?

Don't worry if this seems like a lot to remember. The more you practice writing small programs with these types, the more natural it will feel!