8 Programming
8.1. A. Programming Concepts
Identifier
•Is a name or label chosen by the
programmer, to represent an object
within a program.
•The object could be a variable, a function,
a procedure, a data type or any other
element defined within a program.
Variable
•A variable is a uniquely named location
in memory (RAM), in which a single
data item can be held while a program
is running.
•By definition the program can change
the value held in the variable during
the time of the program execution.
• Any variable consists of;
A name
Data type
Size- The number of characters that can be stored
A memory location
Rules to declare a variable
1. Reserve words or Keywords can not
be taken as variable names.
(eg: IF, INPUT, ENDIF…)
2. Every variable name should start
with alphabets/ underscore. Variable
mustn’t start with numeric or special
characters.
3. Except ‘Underscore’, no other special
symbols are allowed in the middle of
the variable declaration.
eg. Item-code - not allowed
Item_code - allowed
4. There should not be any spaces kept
in the variable name.
- Underscore (_) or Title capitals
(camelCase / camel caps) should be
used.
e.g. Item Code - not allowed
ItemCode, Item_code - allowed
5. Variable names should be meaningful
e.g. ItC, FN, NIC - Not allowed
ItemCode, First_name
Allowed
National_identity_number
6. Variable names can not be repeated
within the same level of scope.
(ie within the same program segment)
7. Maximum length of characters in a
variable should be 255.
(depends on the programming
language and database program)
Constants
•Constants are data values which can be
introduced at the start of a program and remains
unchanged during the time of program
execution.
Eg. Pi 3.142
VAT 12.5
Arithmetic Operators and Functions used in Algorithms
Operator Description Example
+ Addition A+B
- Subtraction A-B
* Multiplication A*B
/ Division A/B
Assignment A B
^ Raise to the power A^2
() Group Used for grouping, when
operator precedence is
required.
Built in Functions
MOD Modular function – 5 MOD 2 returns 1
returns the remainder
INT Integer function – returns INT(89.57) returns 89
the integer value INT (21.215) returns 21
ROUND Returns a value rounded ROUND(6.9756, 2) returns
to a given number of 6.98
decimal places
RANDOM Returns a random number RANDOM()
Returns any number
(decimal)between 0 to <1.
DIV Returns the quotient of a DIV(10,3)
division. Returns 3
The assignment operator (←)
<identifier> ← <value>
• The identifier must refer to a variable
• The value may be any expression that
evaluates to a value of the same data type
as the variable.
Eg.
Counter ← 0
Counter ← Counter + 1
TotalToPay ← NumberOfHours * HourlyRate
Functions
MODULAR Function (MOD function)
Eg. X 12 MOD 5; where X = 2
y 9 MOD 4; where y = 1
Z 8 MOD 2; where z = 0
Functions
INTEGER Function (INT function)
Eg. 12 INT (12.7)
8 INT(8.125)
201 INT(201.999)
Logical Operators (relational operators) used in Algorithms
All programming languages make use of logical operators to decide which path to take through a program
Operator Description Example
= equal to A=B
< less than A<B
> greater than A>B
<= less than OR equal to A <= B
>= greater than OR equal to A >= B
<> not equal to A<>B
Boolean Operators
All programming languages make use of Boolean operators to decide whether expression is
true or false.
Operator Description
AND Both True
OR Either True
NOT Not True
Indentation
•Lines should be indented by 4 spaces to
indicate that they are contained within a
statement in the previous line, where it is not
possible to fit a statement on one line, any
continuation lines are indicated by two spaces.
Comments
•Comments are preceded by two
forward slashes “//”.
•The comment continues until the end
of the line…
Class work – Complete the table by writing suitable variable names,
selecting the data type and providing examples.
Data Variable Name Data Type Example
Name of School SchoolName STRING
Name of Student
Date of Birth
Age
Gender
Form Tutor Group
Fees per Term
Active or Not