0% found this document useful (0 votes)
24 views8 pages

Q2-W1-ICT9

This document is a self-learning package focused on variables in programming, outlining their definition, usage, and importance. It includes learning objectives, examples of variable types, naming conventions, and exercises to reinforce understanding. The document also provides activities and assessments to evaluate the learner's grasp of the concepts presented.

Uploaded by

Rey Ponsaran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views8 pages

Q2-W1-ICT9

This document is a self-learning package focused on variables in programming, outlining their definition, usage, and importance. It includes learning objectives, examples of variable types, naming conventions, and exercises to reinforce understanding. The document also provides activities and assessments to evaluate the learner's grasp of the concepts presented.

Uploaded by

Rey Ponsaran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

9

SELF-LEARNING PACKAGE IN

ICT 9 Quarter 2 | Week 1

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.

Aim at the Target!

At the end of this module you are expected to:

1. Explain what is a variable.


2. Identify the rules in naming a variable.
2. Identify the different data types that a variable may hold.
3. Create a variable.

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.

Keep This in Mind!


As stated earlier, we can think of a variable as a box that the computer can use to store val-
ues. In Figure 1 below, there are three variables namely colour1, temp, and colour2. Sup-
posed each variable holds a value:
colour1 = “red”
colour2 = “green”
temp = colour1
colour1 = colour2
colour2 = temp

colour1 temp colour2

red

Figure 1. Illustration of a variable

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.

1. What is the value hold by colour1?


2. What is the value hold by temp?
3. What is the value hold by colour2?

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.

All variables are made up of three parts:


• a name
• a type
• a value

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.

Variable naming rules:


There are some rules about variable names:

• Consistency: ‘name’ is not the same as ‘Name’ or ‘NAME’.


Spacing: variable names should not have a space in them. Use underscores or camelCase instead, for
example, total_money or totalMoney.

(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

l A poor choice– it has no meaning


length Okay but a bit vague

side_length Good

sideLength Good

side length Wrong-don't have spaces

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.

In the table, one example of a


variable is Title with a data type
String and one of the value it may
hold is Zombie Attack.

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;

Working with variables

• Working with numbers


You can use variables to store numbers.
pocket_money = 20

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

The variables represent any value we choose to assign to them.

Consider this Java program:

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.

• Working with text


A variable can hold a number, but it can also hold a piece of text. Just one letter is called a character. More
than one character is called a string.

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

Consider the following Java program that uses strings:


message = "Hooray! It's my birthday!"
System.out.println(message)

Hooray! It's my birthday!”

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.

Activity. Naming a variable and its data type

name section average grade promoted


Maria Makiling A 85.83 86 True
Juan Salvador Q 70.89 71 False
Salvacion Manalo D 90.1 91 True

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 ______________________________________________________________________

Reinforcement & Enrichment

Direction. Read the scenario and answer the question below.

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

I. Multiple Choice. Choose the letter of your choice.


1. Which of the following is NOT a correct variable name?
a. 3three b. gender c. yourFirstname d. age
2. Which data type is use to store “D”?
a. float b. character c. boolean d. string
3. which of the following is NOT part of a variable?
a. name b. number c. type d. Value
4. In Java program, which one of the following is a correct variable declaration?
a. int name b. string num1 c. float = 2.22 d. none of the above
5. Which data type is use to store “HELLO WORLD!”?
a. character b. integer c. boolean d. string
6. Which of the following is NOT correct about variable?
a. variables are use for storing values.
b. Once a variable is declared, it cannot be changed.
c. a variable is essential in programming
d. a variable in Java should be declared first before it can be used.
7. What type of data that returns a value either true or false.
a. float b. integer c. string d. Boolean
8. What is the name of this variable?
sum = (“input an integer”)
a. sum b. integer
9. What is the name of this variable?
response = (“input a string”)
a. String b. response
10. What is a variable?
a. something that changes
b. A storage location with a unique name and a data type.
c. something that can store numbers only.
d. a name that can be used in programming.

References & Photo Credits

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

You might also like