Introduction To Pseudocode Algorithm Handout
Introduction To Pseudocode Algorithm Handout
When you ask the computer to read two numbers, find the sum of the two numbers then output
the sum. When the computer reads the two values it has to store the values somewhere in
memory. Similarly, it will have to store the value of sum somewhere in order to provide an
output to you the user. These values are normally stored in what we call variables. (Note:
variables or constants are also referred to as identifiers).
1. A variable is a named location in memory that stores a particular value. This value may
be changed during the execution of a program. Examples of variable names include:
‘number’, ‘age’, ‘name’, ‘length’ and so on. You can have several different names,
numbers, ages etc.
20 literal value being
assigned to the
variable
When you write your algorithms and programs you will write instructions to:
give the variable a name
allow a value to be assigned to the variable
The name of the variable will be the same each time you run the program, but the value
stored in the variable can be different each time.
Naming Variables
● A variable must begin with a letter, and then followed by any digit, letter or the
underscore character.
● No character space is allowed when naming your variables. See the diagrams below
which illustrate good variable names versus bad variable names.
GOOD VARIABLE NAMES BAD VARIABLE NAMES TO USE
num1 6Num
A1 _Grade
Product_Calculation Average Calculation
It is sensible that you use meaningful names for your variables that will remind you of the
purpose of the variable.
2
2. A constant provides locations for storing data which do not change value during
execution of a program. For example, if you were calculating the total price of an item
which included General Consumption Tax (GCT), which is for example 17.1 percent.
The 17.1 would be treated as a constant value in the calculation.
Data Types
Variables typically store values of a given type. There are several data types but we will be
focusing primarily on the following basic elementary data types: Integer, Real, character (Char),
Boolean and string.
o Integers – Used to store whole numbers (whether negative or positive) for example, 5, -10,
etc.
o Floating point/Real – Used to store real or fractional numbers for example, 3.1, 22/7 etc.
o Characters – Any single character in the ASCII set (a single character such as a letter of the
alphabet or punctuation) for example, ‘A’, “@”, “$”, etc.
o String – a collection of characters such as a word, phrase or sentence for example, Natalee.
o literals are a special class of data type, and they cover those values that are fixed (even
if only temporarily). For example, consider the following output statement:
Print “Please enter your name”. The words shown in quotation would be considered
an example of a literal string value.
o Boolean – this is a data type that has one of two possible values (usually denoted true and false),
intended to represent the two truth values of logic.
Algorithms
An algorithm is a sequence of precise instructions for solving a problem in a finite amount of
time.
The properties of an algorithm are:
It must be precise (exact)
It must be unambiguous (in other words it must be clear)
It must give the correct solution in all cases
An algorithm must terminate or eventually end
Every algorithm should have the following sections, in the stated order:
Header: Algorithm’s name or title
3
Declaration: A brief description of the algorithm and variables used. That is, a
statement of the purpose as well as initialization of the variables.
Flow chart- this is a graphical representation of an algorithm with the use of certain specified
symbols.
Narrative format
Example: Write an algorithm to accept two numbers, find the product of the two numbers and
output the product of the two numbers.
Step 1: Start
Step 2: Get the two numbers.
Step 3: Multiply the two numbers and store the result.
Step 4: Display the result.
Step 5: Stop
Pseudocode Format
4
Start
Print “Enter two numbers”
Read num1, num2
Product num1 * num2
Print Product
Stop
Flow Chart
Start
Print Product
Stop