Learning Outome B1.1 Variables and Constants
Learning Outome B1.1 Variables and Constants
(PPA115D/TRO115D)
Compiled by
V. Booi
And
V. Memani
1 Learning outcome: B.1. Using identifiers and implementing arithmetic
operators in programs.
In this chapter we are going to discuss the basic concepts of programming, and these
are variables, constants and datatypes as well as arithmetic operators.
1.1 Variables
There are many definitions associated with variables. A variable can be seen as a
container that can store data of a specific type. The data can change, but the container
stores one value at a time of a specific type.
Another definition for variables is that a variable is a memory location that can store
data of specific type. Again this data can change at any time. There are rules to be
followed when naming variables. Let’s look at some of the rules below.
1.1.1 Naming convention of variables
Rule #1
A variable name must be highly descriptive of the value it stores or can store. This
means a variable name must not be meaningless or ambiguous (have double
meaning).
Example
Create two variables, one to store the number of hours worked and the other to store
the price of an item. Let the number of hours worked be 18 and the item’s price be
R20.50.
Valid Invalid
hourWorked = 18 h = 18
price = 20.50 p = 20.50
Rule #2
A variable name must consist of lowercase letters.
Example
Create two variables, one to store the name of a person and the other to store the
price of an item.
Valid Invalid
name Name
price Price
Rule #3
It may not be more than one word (must form a unit), e.g. hourly rate is incorrect,
rather use hourlyRate.
In the case of a variable name being made of multiple words, the words must be joined
together (concatenated) with the first letter of the next word being the only capitalized
letter.
Example
Create a variable to represent the name of a subject.
Valid Invalid
subjectName subjectname
nameOfSubject namaeofsubject
Rule #4
A variable name cannot start with a number. It may contain letter and numbers, but
not only numbers e.g.
Example
Create two variable that will store the first item and number of modules respectively.
Valid Invalid
item1 1item
module11 11Modules
123
Rule #5
A variable name cannot contain special characters e.g #, $, % etc.
Example
Create a variable name that will store the percentage mark obtained by a student.
Valid Invalid
percMark %percMark
percentageMark percentage%
1. Do the the following variable names comply to the rules of variable names? If not,
supply a reason:
Figure 2.4
Rule #1
A constant name must be a highly descriptive noun with capitalized letters.
Example
Create a constant to store the value of pi as 3.14
Valid Invalid
PI = 3.14 Pi= 3.14
pi= 3.14
Rule #2
In the case of a constant name being made of multiple words, the words must be
concatenated (joined together) through an underscore.
Example
Create a constant that will store the tax rate of 15%.
Valid Invalid
TAX_RATE = 0.15; TAXRATE =0.15
TAX_RATE_VALUE = 0.15; Taxrate =0.15
Rule #3
A constant name must not start with a number.
Example
Create a constant to store the name of country 1 as South Africa.
Valid Invalid
COUNTRY_1 = “South Africa”; 1_COUNTRY = “South Africa”;
Rule #4
A constant name must not contain special characters except for an underscore (_).
Example
Create a constant to store 10% as weight formative assessment.
Valid Invalid
WEIGHT_FA = 0.10; %WEIGHT_FA = 0.10;
FA_WEIGHT = 0.10; FA_WEIGHT = 0.10%;
1.2.2 Scope
The area in which a data item is visible to a program, and in which you can refer to
it using its simple identifier. A variable or constant is in scope from the point it is
declared .Until the end of the block of code where the declaration lies
Task #1
Do the the following constant names comply to the rules of constant names? If not,
supply a reason:
Task #2
Supply appropriate names for the following constants:
Description Constant name
Registration fee R2500
Chairs in a classroom 50
Percentage increase 10%
Task #3
Declare and initialise the following variables:
Description Declaration
Count = 10
gender = Male
isAStudent = true
price = R29.50
Category = C
Task #4
Declare and initialise the following constants:
Description Declaration
Discount = 25%
Registration amount =
R3500.00
Voting age = 18
Name of our institution =
TUT
1.3 Conclusion
In the next chapter we are going to introduce the student to mathematical operators.
These operators are highly used in a programming environment. Thank you very much
for having taken time to go through this chapter. Enjoy the rest of the day and God
bless you.
1.4 Common Pitfalls
Undeclared variable
Basic Rule:
A variable must be declared before you use it for the first time in a program.
Example:
double amountDue = 12 * price; // Pitfall/error: price is not declared
double price = 350.55;
In a program, the statements are executed in sequence. When the compiler reaches
the first statement, it does not know the existence of price and therefore it reports an
error. The solution here is to declare the variable before the calculations so that each
variable is declared before it is used (see solution below).
Solution
double price = 350.55;
double amountDue = 12 * price;
Uninitialized variables
Example:
int yearOfBirth;
int age = 2023 – yearOfBirth; // Pitfall/error: yearOfBirth is not initialised
int yearOfBirth=2014 ;
int age = 2023 – yearOfBirth;
ommon Error 2.1