Week 2
Week 2
VARIABLES
Used to store values in our computer’s memory.
Our computer has a memory.
To store values in this memory we need to reserve some space.
o Use a variable
Each variable has a specific type.
It is called a variable because the value inside it can change.
DECLARATION
Allocating space inside our memory.
To allocate space in our memory we declared a variable.
o TYPE NAME;
The type of the variable should be compatible with the data inside it.
o To store a string inside a variable, the variables type should be string.
String myName;
o Declared a variable called myName and can store a String.
To declare multiple variables of the same type.
o TYPE NAME1, NAME2, NAME3;
String myName, myJob;
o myName and my job are two variables that can store a String.
A variable must be declared before it can be used.
DATATYPES
A type that should be compatible with the data inside it.
Integers – Numbers without a decimal part.
Ex: 1, 2, 100, -4, -9, 0
Real numbers / floating point numbers – Numbers with decimal part.
Ex: 1.5, 2.25, -4.7, -9.9, 1.0, 0.0
Characters – All characters on the keyboard surrounded with ‘single quotes’.
Ex: ‘a’, ‘5’, ‘-‘, ‘*’, ‘?’, ‘$’, ‘;’, ‘,’
Strings – Group of characters.
Ex: “a”, “abc123”, “534”, “hello”
Booleans – Represents true or False.
Ex: true, false
ASSIGNMENT
Used to store/put a value inside a variable.
We can assign a value to a variable by using the assignment operator (=).
VariableName = expression
o myJob=”Prgrammer”;
o “Programmer” will be stored inside my job.
An expression is anything that produces/gives a value
o Examples: (1+3), (4*2).
INITIALIZATION
Assigning a value to a variable when declaring it.
o Examples: String myJob = “Programmer”;
o This equivalent to
- String myJob;
myJob=”Programmer”;