Professional Documents
Culture Documents
Flowline
Connector
Start / stop
Input/ output
Decision
Process
Preparation
Predefined Process
1
Page
ontech25
Stored Data
Annotation
Pseudocode is another way of representing the logic of a program. It can be defined as English-
like statements used to express the steps of an algorithm. The prefix ‘pseudo’ means in this
context - resembling or imitating. Pseudocode therefore is intended to resemble actual program
code as best as possible. The idea is that the pseudocode will, line by line, easily be converted
into program source code for easier and smoother program implementation.
All languages have two major component rules – syntax and semantics. Pseudocode must also
then conform to a set of rules, even more as it mimics artificial language.
Syntax – rules governing correct use of language, word and sentence structures, sentence pattern,
organization, the structure of commands, statements, or instructions that are given to a computer.
Elements of pseudocode
Words:
Terminal points (i.e., begin, end, start, stop, enter, return)
Reference points (i.e., Repeat, Until Do While, End Do, For, End For)
Statements:
Instructions in a sentence form
Always begin with a verb
Must be semantically correct in natural language (i.e. English)
Must be properly structured and indented to illustrate flow of logic and control
ontech25
EXAMPLE
Using pseudocode and flowchart, design a program that accepts two numbers from the user,
multiplies them and outputs the results to the screen.
PSEUDOCODE FLOWCHART
START START
Declare num1, num2, prod as integer
Read num1, num2
(Let) prod num1 * num2 Read num1, num2
Write prod
STOP
Write prod
STOP
Assignment
The line prod num1 * num2 uses an assignment statement to compute the value for
sum. For our purposes, the assignment symbol is the . This may differ in actual programming
languages. In an assignment statement:
Only a single variable name should appear to the left of the (sum)
Everything to the right of the must be known (we would know the values of num1 and
num2 because we would have accepted them from the user in the previous statement)
The assignment operator works from right to left therefore what is on the right hand side
of the statement will be evaluated first and its value placed in the identifier on the left
Pi 3.142
word “Hello”
Note that there is a key difference between assignment and equality. Assignment changes the
value of a variable whereas equality tests whether two values are equal.
3
Page
ontech25
Desk-checking an Algorithm
After an algorithm has been developed, it should be tested for correctness. Desk-checking
involves tracing through the logic of the algorithm with some test data in order to detect any
errors that may occur. The steps involve the following:
1. Choose at least two simple input cases that are valid
2. Determine what the expected result should be for those cases
3. Create a table of the relevant variable names within the algorithm
4. Walk through each test case line by line, keeping a check of the contents of the
variables as the data passes through the logic
5. Check that the expected results match the actual results obtained. If not, the algorithm
probably contains a logic error and should be checked.
1. Always review your Input, Processing and Output requirements to ensure that you’ve
covered what the problem is asking for
2. Use meaningful variable names
3. Use indentation so that your code logic is clearly illustrated and neat!
4. Use appropriate comments
5. In pseudocode, the words START & STOP start in the left margin but the actual program
statements are indented. This makes for good, neat documentation
Structured Programming
Structured programming is the ability to express a problem solution using only three basic
patterns of logic or control structures. These structures govern the order in which instructions are
carried out and specify the flow of control. They are:
Sequence
Selection
Iteration
These three components form global building blocks for the design of programs.
SEQUENCE
4
This represents the ability of the computer to execute instructions in a step-by-step sequential or
Page
consecutive manner.
ontech25
Example: Using pseudocode and flowchart, design a program that will accept a number in
meters and convert and output to the screen its corresponding values in centimeters and
millimeters.
START
START
DECLARE meter, centimeter as numeric read meter
STOP
Note that the execution is done in order of how the statements are presented. The implication
here: your design MUST adhere to this rule.
Also, for the pseudocode, note the indentation of the statements within the START & STOP
commands. Indentation enhances program readability and should be used when writing your
programs.
SELECTION
This represents the ability of the computer to make decisions and alter the course of action it
takes. The relational, logical and equality operators are used with the selection structure.
Conditions are evaluated by comparing the left operand with the right operand and the result of
the comparison done is always either 1 or 0 (true or false) respectively.
GENERAL FORM
If (some condition/comparison is true)
Statement set A
(Else) ‘{done if condition is false’}
5
Endif
Page
ontech25
Example
The mark of a student is to be inputted. A message is then outputted showing “Mark accepted”.
An additional message “Excellent” is to be outputted if the mark is greater than 89. Design the
program.
START START
DECLARE mark as integer
READ mark
WRITE “Mark accepted” Read mark
IF mark > 89 THEN
WRITE “Excellent”
(ELSE) Write “Mark Accepted”
ENDIF
STOP
Mark > 89 Y
? Write “Excellent”
STOP
* There is an error on the flowchart. Identify the error and correct it.
Format
if <condition>
<statements>.
Example
if (mark > 89)
printf(“Excellent\n”);
6 Page
ontech25
(b) Double Selection
GENERAL FORM
If (some condition/comparison is true)
Statement set A
Else ‘{done if condition is false’}
Statement set B
Endif
Example
A company pays a car allowance to its employees based on the size of the car’s engine.
Those employees whose car engines are less than or equal to 1500cc receive $80 per
mile, and those employees whose car engines are greater than 1500cc receive an
additional $20 per mile. Design a program to input the mileage and engine size, calculate
the total mileage allowance and output this figure.
START
DECLARE mileage, engine size as integer
DECLARE allowance as numeric
READ mileage, engine size
IF engine size > 1500 THEN
allowance mileage * 100
ELSE
allowance mileage * 80
ENDIF
WRITE allowance
STOP
7
Page
ontech25
START
N engine size Y
allowance mileage * 80 > 1500 allowance mileage * 100
?
Write allowance
STOP
8
Page
ontech25
C uses the if/else structure for this selection.
Format
if <condition>
<statement1>;
else
<statement2>;
Example
if(engine_size > 1500)
allowance = mileage * 100;
else
allowance = mileage * 80;
Format
<condition> ? <statement1> : <statement2>;
Using the same example above, the conditional statement would be:
One form of the IF-THEN-ELSE structure is the sequential IF-THEN-ELSE pattern where the
tests are made directly after each other, for example, a problem which requires the user to enter 3
integers and find and output the largest.
START
DECLARE num1, num2, num3, largest as integer START
READ num1, num2, num3
largest num1
IF num2 > largest THEN Read num1, num2, num3
largest num2
(ELSE)
ENDIF largest num1
IF num3 > largest THEN
largest num3 N Y
(ELSE) num2 > largest?
ENDIF
WRITE largest
largest num2
STOP
9 Page
STOP
Example
A program is to be designed to use the computer as a calculator. The user inputs a code; 1 for
addition, 2 for subtraction, 3 for multiplication and 4 for division; and then inputs the two
numbers that are to be computed. The computer calculates the answer than outputs the result.
Assume all inputs are valid.
We could use the nested IF-THEN-ELSE pattern as a possible solution to this problem:
START
DECLARE num1, num2, result as numeric (1)
DECLARE code as integer (2)
READ code, num1, num2 (3)
IF code = 1 THEN (4)
result num1 + num2 (5)
ELSE (6)
IF code = 2 THEN (7)
result num1 – num2 (8)
ELSE (9)
IF code = 3 THEN (10)
result num1 * num2 (11)
ELSE (12)
IF code = 4 THEN (13)
result num1 / num2 (14)
(ELSE) (15)
ENDIF (16)
ENDIF (17)
ENDIF (18)
ENDIF (19)
10
ontech25
START
Y N
code = 1?
Y N
result num1 + num2
code = 2?
Y N
result num1 - num2
code = 3?
Y N
result num1 * num2
code = 4?
Write result
STOP
Note that if one of the tests yields a true outcome, the statement corresponding to that test is
executed and no additional tests are made. E.g. if the code that the user entered was actually 1,
the statement result num1 + num2 (line 5) would be executed, then the next statement to be
executed would be the statement WRITE result (line 20) since all statements in the ELSE portion
(from line 6 to 18) would be ignored.
If the user entered 2, the program would carry out the test at line 4, determine it to be false, then
move on to line 6. It would then carry out the test at line 7, determine it to be true, execute line 8,
then move directly to line 18.
Ensure that you understand the difference between the nested IF-THEN-ELSE pattern and
a sequence of separate IF-THEN-ELSEs (also called the sequential IF-THEN-ELSE
pattern), especially as it relates to the flowchart.
11
ontech25