Tutorial 2

📦

Variables & Constants

2210 O-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 5 primitive variable types for IGCSE/O-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
DECLARE name : STRING DECLARE yearBorn : INTEGER DECLARE significance : REAL DECLARE alive : BOOLEAN DECLARE gender : CHAR name <-- "Alan Turing" yearBorn <-- 1912 significance <-- 90.5 alive <-- FALSE gender <-- 'M'

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

CONSTANT PI <-- 3.14 DECLARE radius, area : REAL radius <-- 10 area <-- PI * radius ^ 2 OUTPUT "Area for a circle of radius ", radius, " is ", area

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

CONSTANT PI <-- 3.14 PI <-- 999 //will raise an error

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 5 bits of information about the city they live in. You should choose at least one STRING, INTEGER, CHAR, BOOLEAN and REAL variable

Then output a sentence summarising the data they have input

DECLARE name : STRING DECLARE population : INTEGER DECLARE gdp : REAL DECLARE isCapital : BOOLEAN DECLARE settlementType : CHAR OUTPUT "Do you live in a city (c), town (t) or village (v) - enter a single letter, c/t/v:" INPUT settlementType OUTPUT "What is the name?" INPUT name OUTPUT "How many people live there?" INPUT population OUTPUT "What is the gdp per capita?" INPUT gdp OUTPUT "Is is the capital of your country - enter TRUE or FALSE" INPUT isCapital OUTPUT "You live in ", name, " with a population of ", population, " a GDP per capita of ", gdp, " - the type is a ", settlementType, " and it's capital status is ", isCapital

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

CONSTANT C <-- 300000000 DECLARE mass, energy : REAL OUTPUT "Enter your mass in kg:" INPUT mass energy <-- mass * (C ^ 2) OUTPUT "You contain ", energy, " Joules of energy!"