CS6: Data Types
Understanding the five main data types, type casting, and why choosing the right data type matters for memory and validation.
Understanding the five main data types, type casting, and why choosing the right data type matters for memory and validation.
| Data Type | Description | Examples | Memory |
|---|---|---|---|
| Integer | A whole number (no decimal point) | 42, -7, 0, 1000 | 2 or 4 bytes |
| Real / Float | A number with a decimal point | 3.14, -0.5, 9.81 | 4 or 8 bytes |
| Boolean | Can only be TRUE or FALSE | TRUE, FALSE | 1 byte |
| Character | A single symbol (letter, digit, punctuation) | 'A', '7', '?', 'z' | 1 byte (ASCII) |
| String | A sequence of characters (text) | "Hello", "CS123", "" | Variable (1 byte per char) |
age ← 16 score ← 0 lives ← 3 temperature ← -5
pi ← 3.14159 price ← 9.99 average ← 67.5 gravity ← 9.81
isLoggedIn ← FALSE hasPassed ← TRUE gameOver ← FALSE isValid ← TRUE
IF hasPassed = TRUE THEN
OUTPUT "Congratulations!"
ELSE
OUTPUT "Try again"
ENDIF
This is equivalent to:
IF hasPassed THEN
OUTPUT "Congratulations!"
ENDIF
grade ← 'A' separator ← ',' digit ← '7' symbol ← '&'
name ← "Alice" message ← "Hello, World!" empty ← "" serialNumber ← "CS-2024-001"
greeting ← "Hello" & " " & "World" // Concatenation
len ← LENGTH("Computer") // len = 8
sub ← SUBSTRING("Computer", 0, 3) // sub = "Com"
pos ← POSITION("Hello", "ll") // pos = 2
| Conversion | Function | Example | Result |
|---|---|---|---|
| String to Integer | INT() | INT("42") | 42 |
| String to Real | FLOAT() | FLOAT("3.14") | 3.14 |
| Integer to String | STR() | STR(42) | "42" |
| Real to Integer | INT() | INT(3.99) | 3 (truncates, does not round) |
| Integer to Real | FLOAT() | FLOAT(7) | 7.0 |
| Character to Code | ASC() | ASC('A') | 65 |
| Code to Character | CHR() | CHR(65) | 'A' |
INPUT userAge // userAge is received as a string "16" age ← INT(userAge) // Convert to integer 16 age ← age + 1 // Now we can do arithmetic: age = 17 OUTPUT "Next year you will be " & STR(age) // Convert back to string for output
If age is declared as an integer, the program will reject "hello" as input because it cannot be stored as an integer. The data type acts as a first line of validation.
age ← INT(INPUT("Enter age: "))
// If user enters "hello", this will cause an error
// If user enters "16", age becomes the integer 16
a ← 5 + 3 // Integer addition: a = 8 b ← "5" + "3" // String concatenation: b = "53" c ← 5 / 2 // Real division: c = 2.5 d ← 5 DIV 2 // Integer division: d = 2
The + operator does different things depending on the data type!
| Mistake | Why It's Wrong | How to Fix It |
|---|---|---|
| Using string for numeric data | Cannot perform arithmetic on strings | Cast to integer or real first |
| Using integer for decimal values | Loses the decimal part | Use real/float instead |
| Confusing '5' and 5 | Character vs integer - different operations | Remember quotes mean character/string |
| Not casting input | INPUT usually returns a string | Use INT() or FLOAT() to convert |
| INT(3.9) expecting 4 | INT() truncates, it does not round | Use rounding functions if needed |
Q1: Name the five main data types and give an example value for each.
Q2: What is the difference between the integer 7 and the string "7"?
Q3: What is the result of INT("25") + INT("10")?
Q4: Why should you store a person's age as an integer rather than a string?
Q5: Write pseudo-code that asks the user for a number, converts it to a real, divides it by 2, and outputs the result.
INPUT userInput num ← FLOAT(userInput) result ← num / 2 OUTPUT result
โ Thinking integer division and float division always give the same result โ Integer division truncates the decimal part (7 รท 2 = 3), while float division preserves it (7 รท 2 = 3.5). Using the wrong type causes data loss.
โ Confusing character and string data types โ A character is a single symbol (e.g. 'A'), stored in 1 byte. A string is a sequence of characters (e.g. 'Hello'), and its storage depends on length.
โ Believing Boolean can only be TRUE/FALSE text โ In most languages, Boolean values are stored as 1 (TRUE) or 0 (FALSE) in memory; the keywords TRUE/FALSE are how they are represented in code.
โ Forgetting that real/float numbers cannot represent all decimals exactly โ Floating-point numbers use binary representation and cannot store some decimal fractions exactly (e.g. 0.1), which can cause rounding errors in calculations.
A program stores a student's name, their test score as a whole number, their height in metres, and whether they have passed. State the most appropriate data type for each value and explain your choice. [4 marks]
Name: STRING โ a name consists of multiple characters (e.g. 'Alice'), so a string is needed rather than a single character. Test score: INTEGER โ test scores are whole numbers (e.g. 85), so integer is appropriate as no decimal part is needed. Height: REAL/FLOAT โ height in metres requires decimal places (e.g. 1.65), so a real/float type is needed to store the fractional part. Passed: BOOLEAN โ the student has either passed or not passed, so only two values (TRUE/FALSE) are needed, making Boolean the most efficient choice.
AO1 (Computational Thinking โ 40%): Demonstrate knowledge and understanding of the principles and concepts of computer science, including data types: integer, real, string, character and boolean for AQA 8525, OCR J277 & Edexcel 1CP2.
AO2 (Application โ 40%): Apply knowledge and understanding of computer science, including data types: integer, real, string, character and boolean to analyse problems in computational terms and to design, write and evaluate solutions.
AO3 (Evaluation โ 20%): Evaluate the effectiveness, correctness and efficiency of computational solutions, including data types: integer, real, string, character and boolean, and make reasoned judgements about trade-offs.
Get the best revision books and guides to boost your grades.