Algorithms
The following is the syntax used for writing algorithms using pseudocode. Syntax means rules
used in a programming language.
Input Instruction
Data to be processed must be entered into the computer and stored. Such data may be entered from
the keyboard. The command READ is used to accept input.
Syntax – Read <variable> [for one variable]
Or
Read <variable>, <variable2> [for more than one variable]
Example of an input instruction – read name [accept data from the user and store into the variable
called name]
In order to accept input from the user, it is good programming to inform the user what is required.
A short message can be printed to the screen to notify the user. These are called prompts.
Output Instruction
Output is needed in most programs, Output can be sent to the screen or the printer. The command
PRINT is used to produce output.
There are different ways to output data to the screen.
Outputting a value from a variable
Syntax - print <variable>
Print sum [explanation – print the value stored in the variable sum]
Output a message – this is also called a Prompt Statement
Syntax - Print ‘message’
Print ‘today is a beautiful day’ [explanation – the statement in the quotation
marks will be printed]
Output a message and a value
Syntax – print ‘the answer is ‘, sum [explanation the message ‘the answer is’ will
be printed followed by the values stored in the sum]
Processing statements
Data that is to be processed could involve performing calculations. Calculations can be done using
the mathematical operators.
Operator Name and description
Example
+
Addition
2+2=4
–
Subtraction
4–2=2
/
Division
8/4=2
*
Multiplication
4 * 8 = 32
^
Exponentiation: When one number increases
2 ^ 4 = 16 (2 * 2 * 2 * 2 = 16,
exponentially (the number of times) to another. The
or 24)
repeated multiplication of a number by itself.
MOD
Modulus: The remainder that is left over when a
16 MOD 3 = 1 (16 / 3 = 5,
number is divided by another. Some programming
with 1 left over)
languages will use the % symbol for MOD.
DIV
Integer division: Used to find the quotient (integer
100 DIV 3 = 33 (actual value
number before the decimal point) after division.
33.333333333 repeating)
Python uses // for this.
When calculations are to be performed, numeric variables or numeric constants must be used. The
result of the calculation must be stored in a variable for future use, such as printing or use in a
subsequent calculation.
The syntax for a calculation instruction:<variable1> = <variable2> mathematical operator <numeric constant>
or <variable3>
The result of the calculation is stored in the variable to the left of ‘=’. This variable is therefore
assigned the result of the calculation. Any statement that assigns a value is called an assignment
statement.
Each assignment statement must begin with:
1. A variable, followed by an equal sign, then another variable or a constant
2. A mathematical operator, followed by another variable or constant.
Some calculation examples are as follows:
Sum = num1 + num2 [explanation – add the value of the variable num1 to the value of
the variable num2 and store the result in the variable called sum]
Ans = num + 5 [explanation – add the value of the variable num to the number 5 and
store the result in the variable called ans]
The variable, which begins an assignment statement, should not be variable that was read as input
if the input is to be printed.
Variable used in the formula for calculating must be variables that were used in executed
statements prior to the calculation instruction and not new variables.