Module 1: Variables & Data Types
What are Variables?
A variable is a named location in memory used to store data that can change while the program is running. In OCR ERL, you create a variable simply by assigning a value to it using the = sign.
score = 0
name = "Alice"
isGameOver = False
Exercise: Variables
Declare a variable called lives and set it to 3. Print its value.
ex1.erl
Constants
A constant is like a variable, but its value cannot change once the program is running. We use the const keyword.
const PI = 3.14
const MAX_PLAYERS = 4
Exercise: Constants
Declare a constant called VAT and set it to 0.2. Print its value.
ex2.erl
Data Types & Casting
Variables automatically take the type of the value you assign, but sometimes you need to change the type manually. This is called casting.
int("3")converts the string "3" to the integer 3.str(42)converts the integer 42 to the string "42".float("3.14")converts to a real/float number.bool("True")converts to a boolean.
Exercise: Casting
Convert the string "100" to an integer and store it in score. Add 50 to it and print the result.
ex3.erl