Tutorial 2
📦
Variables & Constants
9618 A-Level
If wanting to store data in our program, we will need to declare either:
- Variables: to hold data that can change
- Constants: to hold data that can't change
1
Person Details
Let's declare the 6 primitive variable types for A-Level and assign some values to them
- INTEGER: a whole number
- REAL: a number that can have a decimal component - 123.456
- CHAR: a single ASCII character (Unicode also supported by the site)
- STRING: zero or more characters in order - "Hello"
- BOOLEAN: a logical TRUE or FALSE
- DATE: in the form dd/mm/yyyy
2
Circle Area
Assume we want to calculate the area of a circle - the value of PI will never change, so we can define it as a constant:
Note: while it's not technically required, constants are usually written in UPPERCASE in real life and the exam - this is to make them easy to spot when looking at code
Also note how we assign to constants with the equals symbol (=)
Note: if we try to reassign a value to a constant, we won't be allowed to - this is useful for ensuring either us or another programmer doesn't accidentally modify a value that shouldn't be changed
Often we want to assign to values directly, however, sometimes we will want to ask the user to input some data - see tutorial 1 on data input if you haven't already
3
City Facts
Ask the user to enter 6 bits of information about the city they live in. You should choose at least one STRING, INTEGER, CHAR, BOOLEAN, REAL and DATE variable
Then output a sentence summarising the data they have input
4
e = mc^2
Use Einstein's famous e = mc^2 to work out the amount of energy contained within your body - where:
- e: energy in Joules
- m: mass in kg
- c: speed of light in meters per second = 300,000,000
Hint: think about what should be a constant and what should be a variable