Q2-W1-ICT9
Q2-W1-ICT9
SELF-LEARNING PACKAGE IN
Variables in Programming
Learning Competency:
Cite Examples of variables in programming
SSP_TLE-CT8AP-IId-m-2.1
Prepared by: Maricar R. Pornel
Ready to Launch!
Sometimes we need computers to remember the information we give it and that it calculates during
programs. A variable can be thought of as a box that the computer can use to store a value. The value held in
that box can change or ‘vary’. A program can use as many variables as it needs it to.
Variables are extremely useful when programming. For example, if a variable is used for the length of a
side in a simple program to calculate the perimeter of a square, it is easy to calculate the perimeter of a
different sized square, simply by changing the value of the variable.
In this lesson, we will explore the use of a variable in programming.
Try This!
Solve the crosswords using the list of words and the clues.
FLOAT
STRING
VARIABLE
BOOLEAN
INTEGER
DATA TYPE
2
ACROSS DOWN
1. In software programming, they are names used 4. It is the format in which a variable holds data.
to hold one or more values. 5. A data type that has one of two possible values
2. A data type used for a combination of any charac- (usually denoted true and false)
ters that appear on a keyboard, such as letters, 6. A type of data used to hold whole numbers
numbers and symbols.
3. Represents real numbers and is written with a
decimal point dividing the integer and fractional.
red
Direction. Demonstrate what will be the value each of the variable will hold. The first one is done for you.
Analysis.
Answer the following questions below based on the result of the demonstration.
3
Abstraction and Generalization
What is a Variable?
Variables are data values that can change when the user is asked a question, for example, their age. Vari-
ables may change during program execution.
A variable is a memory location. It has a name that is associated with that location. The memory location
is used to hold data. The value associated with a variable name may change during program execution. For ex-
ample 'highScore' would need to be variable to change throughout a game.
The content and organisation of a computer's memory is not fixed - so neither is the value that is pointed at by
a variable.
When data is read from a variable, the content of the memory location is copied and used in calculations.
Naming variables
Each variable is named so it is clear which variable is being used at any time. It is important to use mean-
ingful names for variables:
For example, pocketMoney = 20 means that the variable ‘pocketMoney’ is being used to store how much pock-
et money you have. Right now you have 20.
The name given to each variable is up to the programmer, but ideally a variable name should have
meaning, for example, it should reflect the value that it is holding.
(NOTE: A camelCase convention where there are no spaces, and the first letter of the first word is in lowercase
with the first letter subsequent words are in uppercase.)
4
• Digits: variable names should not start with a digit
Consider these example variable names, all of which could be variable names to store the length of a side of a
square:
Variable name Comment
side_length Good
sideLength Good
Example:
This Java program uses two meaningful names when calculating the perimeter of a square:
side_length = 5
perimeter = side_length * 4
System.out.println (perimeter)
20
Because meaningful names have been used in this code, it is easy to know what each variable is used for.
Data types
Variables come in all shapes and sizes. Some are used to store numbers, some are used to store text
and some are used for much more complicated types of data. A data type is the format in which a variable
holds data.
The data types to know are:
• String (or str or text). Used for a combination of any characters that appear on a keyboard, such as
letters, numbers and symbols. (examples: abc, hello world)
• Character (or char). Used for single letters.
• Integer (or int). Used for whole numbers. (example: 1, 37, 565566)
• Float (or Real). Used for numbers that contain decimal points, or for fractions.
(example: 4.5, 23.345, 456.34)
• Boolean (or bool). Used where data is restricted to True or False options.
5
In many programming languages variables must be declared before they can be used, for example:
• Visual Basic - dim score as int
• Java – int score;
The variable ‘pocket_money’ is used to store how much pocket money you have. Right now you have 20.
As well as using fixed numbers in calculations and storing the answer in a variable, we can also use variables
within the calculations themselves. In this example, the instruction uses the variable ‘money_in_bank’ to calcu-
late the answer and then stores the answer in a variable called ‘total_money’.
total_money = money_in_bank + 10
money_in_bank = 20
total_money = money_in_bank + 10
System.out.println(total_money)
30
The first line stores the value 20 to the variable ‘money_in_bank’. Since the computer knows that
‘money_in_bank’ has the value 20, it can calculate the value of ‘total_money’ and store it. Once calculated, the
value of ‘total_money’ can be printed to the screen.
Once a value is stored, all sorts of mathematical operations can be performed on the variables:
money_in_bank = 20
total_money = money_in_bank + 10
System.out.println(total_money)
30
cost_of_holiday = 150
left_to_pay = cost_of_holiday - total_money
System.out.println(left_to_pay)
120
REMEMBER: Using variables means the exact amounts that are being used don’t have to be remembered -
the computer stores the numbers for us.
6
A text variable works in the same way as a number variable, with a couple of differences:
• text variables hold characters (letters, digits, punctuation)
• the data in text variables is placed in quotes
• arithmetic calculations cannot be performed on text variables
REMEMBER: Data in character or string has quotes placed around it. This tells the computer that we are using
text and not a number.
Application.
Direction. Answer the question given below.
Base on the table above, identify what are the variables, its data type and the value it may hold.
Variable name:
Data type:
Value:
Reflect
Complete the statements below.
I understand _____________________________________________________________________________________
I don’t understand ________________________________________________________________________________
I need more information about ______________________________________________________________________
Scenario: Assuming you are the owner of a coffee shop. You want to organize some data in the daily
transaction of your shop such as identifying the product names and its prices, daily total sales, best -
selling product, and others.
1. Base on the data that you organize , create at least five variable names and the possible data type it
may hold.
7
Assess Your Learning
https://www.bbc.co.uk/bitesize/guides/zc6s4wx/revision/5
https://www.kidscodecs.com/variables/
https://teachinglondoncomputing.org/resources/inspiring-unplugged-classroom-activities/the-box-variable-
activity/
COFFEE SHOP. https://www.shopkeep.com/blog/what-to-consider-when-choosing-a-coffee-shop-location#step-1