Data types, variables and operators
Every value in a program has a data type, which decides what you can do with it. Operators then do the work: arithmetic on numbers, comparisons that give true or false, and Boolean logic that combines conditions.
Part 1 of 3: Learn it
In short
- The main data types are integer, real (float), Boolean, character and string.
- DIV gives the whole-number part of a division; MOD gives the remainder.
- Casting converts a value from one type to another, such as a string "42" to the integer 42.
Where this is in your specification
Spec points: AQA 3.2.1 to 3.2.5, OCR 2.2.1 and 2.2.2, Edexcel Topic 6
| Board | Topic: Programming in exam style |
|---|---|
| AQA 8525 | 3.2.1 to 3.2.11, 3.7.1 |
| Edexcel 1CP2 | Topic 6 (and Topic 1 for trace tables) |
| OCR J277 | 2.2.1 to 2.2.3, 2.3.1, 2.3.2 |
| Eduqas C500 | 7 Principles of programming; 10 Program construction |
| Cambridge IGCSE 0478 | 8 Programming |
| National 5 C816 75 | Software design and development: implementation, testing |
Data types
| Type | Holds | Example |
|---|---|---|
| Integer | whole numbers | 17, −3 |
| Real (float) | numbers with a decimal part | 3.75 |
| Boolean | one of two values | True, False |
| Character | a single letter, digit or symbol | 'G' |
| String | a sequence of characters | "Year 11" |
Variables and constants
A variable is a named store whose value can change while the program runs. A constant is given a value once and cannot be changed later, such as VAT_RATE = 0.2. Constants make code easier to read and update in one place. Assignment uses = (or ← in AQA pseudocode) to put a value in a variable.
Operators
| Kind | Operators | Example |
|---|---|---|
| Arithmetic | + − * / | 7 / 2 gives 3.5 |
| Integer division | DIV | 17 DIV 5 gives 3 |
| Remainder | MOD | 17 MOD 5 gives 2 |
| Power | ^ | 2 ^ 5 gives 32 |
| Comparison | == != < > <= >= | 4 != 5 gives True |
| Boolean | AND OR NOT | True AND False gives False |
MOD is handy for testing whether a number is even: n MOD 2 is 0 for every even number.
Casting
Input from the keyboard usually arrives as a string. To do arithmetic with it, convert it: int("42") + 1 gives 43, while "42" + "1" joins the strings to make "421". str(), int(), float() and bool() are the usual casting functions.
What is 29 MOD 4?
Show the answer
1 (29 = 7 × 4 + 1).
Part 2 of 3: See it worked
Worked examples
Example 1
A school has 230 students to put on minibuses of 16. Use DIV and MOD to find how many full buses there are and how many students are left over.
- 230 DIV 16 = 14 (14 × 16 = 224)
- 230 MOD 16 = 6
Answer: 14 full minibuses, with 6 students left over (so 15 buses are needed).
Example 2
x = 6 and y = 10. Evaluate (x > 5) AND NOT (y == 10).
- x > 5 is True
- y == 10 is True, so NOT (y == 10) is False
- True AND False
Answer: False.
Common mistakes
- Mixing up DIV and MOD.
- Storing a phone number or a postcode as an integer.
- Using = to compare in languages that need ==.
- Forgetting to cast input before doing arithmetic with it.
Which data type would you use for whether a user is logged in?
Show the answer
Boolean.
Part 3 of 3: Test yourself
Check yourself
Answer each one in your head or on paper first, then open it to check.
What is 29 MOD 4?
1 (29 = 7 × 4 + 1).
Which data type would you use for whether a user is logged in?
Boolean.
What is the difference between a variable and a constant?
A variable's value can change while the program runs; a constant's value is fixed.
Jobs that use this
Each link opens the job profile on the National Careers Service (England). In the rest of the UK: My World of Work (Scotland), Careers Wales, nidirect careers (Northern Ireland).
Ces fiches sont en anglais car elles suivent les programmes d'examen britanniques.
Full lessons and marked practice for this course are coming soon to Brainlag Learn. See courses