0% found this document useful (0 votes)
7 views10 pages

Learning Outome B1.1 Variables and Constants

This document introduces the fundamental concepts of programming, focusing on variables, constants, and data types, along with their naming conventions and rules. It outlines the importance of descriptive names for variables and constants, as well as the scope of these data items in programming. Additionally, it highlights common pitfalls such as undeclared and uninitialized variables, providing examples and solutions.

Uploaded by

organnkuna11
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)
7 views10 pages

Learning Outome B1.1 Variables and Constants

This document introduces the fundamental concepts of programming, focusing on variables, constants, and data types, along with their naming conventions and rules. It outlines the importance of descriptive names for variables and constants, as well as the scope of these data items in programming. Additionally, it highlights common pitfalls such as undeclared and uninitialized variables, providing examples and solutions.

Uploaded by

organnkuna11
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/ 10

Principles of Programming IA

(PPA115D/TRO115D)

Using variables and constants

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:

Variable Name Valid / Invalid? Reason


covid 19
1stGrade
xyz
subjectCode
theAdmissionPointScore
5%discount
#address
7654

2. Supply appropriate names for the following variables:

Description Variable name


Area code
School fees
Day of the week
Shoe size
Doctors address
Member of society( Yor y –for yes/ Nor n
–for no)
Total sales amount
Number of books
1.2 Constants

A constant is a container that stores unchanging data of a specific type. A constant


is a memory location that stores unchanging data of a specific type.

Figure 2.4

So, if we have fixed data in our program, we must store it in a constant.

Constants can be one of two types:


Either a literal value, such as 5 or 0.6, or it can be a data item with a name and a value
that never changes. In the latter case the same rules that apply to the naming of a
variable also apply to the naming of a constant.

Examples of constants as a data item are the following:


 There are always 7 days in a week e.g. noDaysWeek should contain 7.
 A day has 24 hours e.g. dayHours should contain 24.
 The percentage used for VAT rate is 15% (at the moment) e.g. the value of
salesTax is 0.15.

These constants are usually assigned a value at the beginning of the


algorithm/program and it never changes like the value of a variable. We also have
rules that govern the naming of constants. Let us have a look at some of the rules.
1.2.1 Naming convention of constants

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:

Constant Name Valid / Invalid? Reason


DaysInAYear =365
TEST_TOTAL = 100
DISCOUNT = 15%

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 this chapter we managed to introduce the student to the basic concepts of


programming. The concepts introduced are variables, constants, and data types.
These are the fundamental concepts of programming.

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

The solution here is to assign a value to the variable before it is used.


Solution

int yearOfBirth=2014 ;
int age = 2023 – yearOfBirth;
ommon Error 2.1

You might also like