3. Variables and Constants
• Results of processing must be stored somewhere in order to
access it later.
• Let’s make lemon juice:
– Squeeze the lemon in a juicer
– Add lemon juice to jug
– Make syrup in a pan
– add water and syrup to jug
– Pour juice in a glass
• Just as we need containers to hold or store ingredients,
likewise we need something to store and hold the values we
manipulate.
4. Variable
• Variable is a container that stores data. You can refer and
you can modify data in variables at any time during
processing.
• Variable is usually located on RAM.
• You can name variables as you like. Name of variables is called
identifier. Identifier should be clarified so that they are always
meaningful.
5. Variables
• Variable names:
– Should disclose what it does
– Contain letters, numbers, underscores
– Should start with a letter
– Should not contain any spaces
Data
Assignment
Variable
Reference
6. Variables
• Example of variable names
• If you want to find the sum to two numbers 5
and 8.
sum
num2
num1
store
Read num1,
num2
7. Variables
• Example of variable names
• If you want to find the sum to two numbers 5
and 8.
sum
store
reference
store
Variables
num1
num2
Sum ← num1 + num2
8. Variables
• Example of variable names
• If you want to find the sum to two numbers 5
and 8.
store
store
Variables
num1
num2
sumPrint sum
9. Constants
• Constant is container with fixed data. Data that does not
change but remains the same during the execution of the
program.
• You can refer the data but you CAN’T modify data in
constant.
• Constant is located on ROM.
• You can name constants as you like. Name of variables is
called identifier. Identifier should be clarified so that they are
always meaningful.
constant
Reference
11. Data Types
• Determines the type of data that a variable can store.
• Data has any of following types :
• Integers: positive and negative whole numbers. e.g. 15, -60, 3
• Floating point or real: positive and negative numbers with decimal
values. e.g. 0.36, 4.6, -9.4
• Characters: anything that you can key in from a key board. This
includes letter, numbers and special characters. e.g. @, k, j, *
• Boolean: data that is either true or false. e.g. whether the light is on or
off
• String: hold groups of characters, like a word or a phrase.