Pseudocode Design
An Algorithm may be looked at as a verbal or written description of a logical sequence of actions
that is to be carried out (by a computer).
Pseudocode uses keywords and control structures similar to those used in programming
languages but without the strict rules of Programming Language (Syntax). Pseudocodes are
written in a form, which looks like a combination of English and a Programming Language.
These are a technique for structured program design that uses English and Mathematical
statements to outline the logic of a program. After an Algorithm is properly structured, the next
step is to code it into a Programming Language such as Pascal, Visual Basic, C++, Basic, Java,
etc.
Parts of a Pseudocode Algorithm
1. Input Statements
2. Output Statements
3. Assignment Statements
4. Control Statements (Sequence, Repetition, Selection)
Definition of Important Terms
VARIABLE A variable is a named quantity in Pseudocode whose value can be
changed.
NB The value of a variable can be changed by an Input Statement.
CONSTANT A Constant is a value (OR VARIABLE) in a Pseudocode that represents a
value that does not change.
IDENTIFIER An Identifier is the name invented by the Programmer for a data item. An
identifier can be the name of a variable or constant.
A pseudo-code is an imitation computer program written using mathematical notations and
English-like statements to describe the logics to solve a problem or carry out a procedure. The
pseudo-code is used as a guide to code the solution to the problem in a high-level language.
Parts of a pseudo-code algorithm
A pseudo-code algorithm contains one or more of the following statements:
Input Statements and Prompting Statement
Assignment Statements
Output Statements
Control Statements (Sequence, Selection, Repetition)
The PROMPTING STATEMENT is used to tell the user to enter data into the
computer. It tells the person using the program what to do.
E.g., Print ‘Please enter your age’
NB. EVERY INPUT STATEMENT WILL HAVE A PROMPTING STATEMENT
ABOVE IT. Whatever is in quotes will be displayed exactly.
The INPUT STATEMENT is used to accept whatever data or value that was entered by
the user. It will store the value in a variable.
E.g. Read age
PROCESSING STATEMENT is used to perform calculations or assignments. The
assignment includes placing a value into a variable, while calculations include any
mathematical operators such as +, -, /, x. The equal sign or arrow (<-----) can be used to
store text or numbers in variables.
Syntax for assignment : Variable name= value
E.g. of Assignment: year = 2009 OR year <---- 2009
Syntax for Calculation
Please note the syntax for calculation depends on what you are asked to do. Thus each
calculation might be different.
Variable name= variable name1- variable name2
Variable name= variable name1 + variable name2
Variable name= variable name/ variable name
If you are counting or incrementing (increase):
Variable name= variable name +
E.g. birthyear = year - age OR birthyear <---- year – age
OUTPUT STATEMENT is used to display the results of calculations.
Syntax(format)- Print ‘ statement’ variable name
E.g. Print ‘you were born in ‘, birthyear
NB. If you have more than one variable in a Read or more than one text or variable in a Write
line, separate them with a comma.
E.g. Read Num1, Num2, Age1, Age2
OR
E.g. A program is required to read three numbers, calculate, and print their total.
START
Print 'Enter three numbers'
Read Num1, Num2, Num3
Total = Num1+ Num2 + Num3
Print 'The total is', Total
STOP
KEYWORDS
PSEUDOCODE KEYWORDS
1) The word Start is used to begin an algorithm
2) The words Read, input, store is used to input or accept values.
Do NOT use quotes(“ ”) with Read.
3)Multiplication (* ) and Division (/)
4) Assignment Symbol = or
5) To display results/messages use the keywords: Print, Display, Output
Text is enclosed in single quotes(‘ ’ )and are displayed exactly as how it appears in the
single quotes
6) To end an algorithm the word Stop is used
Do NOT put quotes around a variable if you want to print the value stored in it.
SAMPLE PSEUDOCODE ALGORITHM
Write a program to read 2 numbers. The program should calculate and print the sum, average
and product.
START // START OF THE PROGRAM//
Print 'Enter two numbers' // Prompting the user to enter data//
Read Num1 // accepting the value entered by the user//
Read Num2 // accepting the value entered by the user//
Sum = Num1 + Num2 // Calculating the total of the two numbers entered by the user//
Average = Sum/2 // Calculating the average of the two numbers entered by the user//
Product = Num1 * Num2 // Calculating the product of the two numbers entered by the user//
Print 'The Sum, Average and Product are', Sum, Average, Product // Displaying the result//
STOP //END OF THE PROGRAM//
THE PASCAL LANGUAGE
PSEUDOCODE KEYWORDS PASCAL KEYWORDS
1) The word Start is used to begin an The word Begin is used to start a Pascal
algorithm program
2) The word Read is used to input values The words Read and Readln is used to input
values
3)Multiplication * and Division / Same
4) Assignment Symbol = or Assignment symbol :=
5) To display results/messages: Print, To display results: Write or Writeln
Display, Output
6) To end an algorithm the word Stop is used To end a Pascal program the word End
followed by a full stop is used. (End.)
A control structure is a structure that decides the order in which statements will appear or be
performed.
Example: A vehicle is arriving at an intersection. Thus, the precondition is the vehicle is in
motion. Suppose the traffic light at the intersection is red. The control structure must determine
the proper course of action to assign to the vehicle.
Precondition: The vehicle is in motion.
Control Structure
Is the traffic light green? If so, then the vehicle may stay in motion.
Is the traffic light red? If so, then the vehicle must stop.
End of Control Structure
Postcondition: The vehicle comes to a stop.
Thus, upon exiting the control structure, the vehicle is stopped.
There are three main types of control structures:
Sequential: Sequential Control structure executes one line of code after the other.
Selection: This is used for decisions, dividing, or choosing between 2 or more options. In
programming, the main types of selections statements are:
o if
o if/else
o switch
Repetition: This is used for looping or repeating a condition until a criterion is met, that
is repeating a piece of code multiple times in a row. There are FOUR types of loops:
o while
o do/while
o for
o Repeat Until
Please note that algorithms can be represented using:
Narratives
Pseudocode - helps "think" out a problem or algorithm before trying to code it
Flowcharting - graphical way to formulate an algorithm or a program's flow
Reinforcement: PROGRAMMING CONSTRUCTS
* Sequential – all statements are executed in the order they are written.
* Selection – a statement/set of instructions that is/are executed if a condition is
satisfied
* Repetition/Iteration – a statement/set of instructions that is/are executed until a
condition is satisfied a specific number of times.
SELECTION STRUCTURES
Selection (decision)- This is a construct that is used when no action needs to be performed if the
expression returns false.
Syntax(format ):
IF (expression) THEN
{Statement (s)} Execute statement(s) if logical expression is TRUE
Example.1) Write an algorithm to read the score of a student in an examination and determine
whether the student has passed. If the score is greater than or equal to 50, the student has
passed. Print whether the student has passed.
Answer
Start
Print (‘Please enter the score of a student’)
Read Score
IF (Score >= 50 )THEN
Print ‘PASS’
endif
Stop
Example 2
Write an algorithm that read TWO numbers. If the first number is greater than the second
number, output the first number.
Variable Names: Num1 and Num2
START
Print “ Enter the first Number”
Read Num1
Print “ Enter the second number”
Read Num2
IF (Num1>Num2) THEN
Print “ Num1”
Endif
STOP
Trial
Write an algorithm to read the Quantity and price of an item. If the Price is greater than 5000
output on the screen too expensive.
START
Print “ Enter the quantity of item”
Read quantity
Print “ Enter the price of item”
Read Price
IF (Price>5000) THEN
Print “ Too Expensive”
Endif
STOP
IF-THEN-ELSE CONSTRUCT
This selection control structure is a conditional statement used to execute statements depending
on the truth value of a certain condition. If the condition is TRUE, then the statements after
“THEN” are executed. If the condition is FALSE, then the statements after “ELSE” are executed.
Syntax:
IF (expression) THEN
{Statements} executed only if condition is TRUE
ELSE
{Statements} executed only if condition is FALSE
ENDIF
Only one group of statements could be executed each time the program is executed
Eg.2) Write an algorithm to read the score of a student in an examination and determine whether
the student has passed or failed. If the score is greater than or equal to 50, the student has
passed, otherwise the student has failed. Print whether the student has passed or failed.
Answer
Start
Print (‘Please enter the score’)
Read Score
IF Score >= 50 THEN
Print ‘PASS’
ELSE
Print ‘FAIL’
Endif
Stop
REPETITION STRUCTURES
Repetition or Loop or Iteration structures allow statements to be repeated a fixed number of
times or until some condition evaluates to false.
There are three repetition constructs:
1. FOR Loop - counted loop
2. REPEAT Loop - conditional loop
3. WHILE Loop - conditional loop
4.
In repetition, you need to keep track of how many times the instruction must be repeated. Thus a
COUNTER is established. The counting or iteration involves increasing the value of the counter
by a fixed number every time instruction is repeated.
Counter-Controlled Repetition Requires
1. the name of a control variable (or loop counter)
2. the initial value of the control variable
3. the loop-continuation condition that tests for the final value of the control variable to
determine when to exit
4. the control variable to be incremented (or decremented) each time through the loop
***ALL LOOPS: if loop body contains more than one statement, statements must be
entered as a statement block--that is, in a set of braces {}
Counting
Syntax: <counter variable>= Counter Variable + 1
Before After
5 Counter Variable Result value 6
count Counter Variable count
Following the syntax above we get:
Count= Count + 1
5= 5+1= 6
Loops can execute a block of code as long as a specified condition is true. There are several
types of loops, but two main ones are:
For Loop
While Loop
For Loops allow a user to run through the loop when you know how many times he or she
would like it to run through the problem .
Syntax: For (conditional statement) do
While Loops gives a user more flexibility in what you put in it, and when it will stop.
Syntax: While (conditional statement) do
‘FOR’ LOOP CONSTRUCT
The FOR-loop construct syntax:
FOR <counter> = <start value> TO <end value> DO
{Statements}
ENDFOR
e.g. FOR X = 1 to 10 DO
{Statements to be executed}
ENDFOR
NOTE: The FOR loop is used when the required number of iterations (loops) is known
beforehand.
Some other simple examples include:
FOR x = 1 to 100 DO FOR weekday = 1 to 5 DO
Print (‘I must not sleep in class’) Print(‘Go to school’)
ENDFOR ENDFOR
FOR age= 13 to 19 DO FOR x = 1 to 12 DO
Print(‘You are a teenager’) y=x*2
ENDFOR Print(x,’ times 2 = ‘,y)
ENDFOR
FOR num = 1 to 20 DO Saturday = 2
Print(‘The number is: ‘,num) Sunday = 3
ENDFOR FOR day = Saturday to Sunday DO
Print(‘It is the weekend’)
ENDFOR
‘WHILE’ LOOP CONSTRUCT vs ‘REPEAT…UNTIL’ LOOP CONSTRUCT
COMPARISON OF ‘WHILE’ LOOP AND ‘REPEAT...UNTIL’ LOOP
SIMPLE EXAMPLES OF ‘WHILE’ vs ‘REPEAT...
Eg.) Write an algorithm to accept an unspecified number of integers. Calculate and print the
sum of the numbers entered. The program should be terminated when the number 999 is
entered.
Answer: ‘WHILE’ LOOP Answer: ‘REPEAT...UNTIL’ LOOP
Start Start
Print(‘Please enter a number’) Print(‘Please enter a number’)
Read(num) Read(num)
WHILE (num <> 999) DO REPEAT
sum = sum + num sum = sum + num
Print(‘Please enter a number’) Print(‘Please enter a number’)
Read (num) Read(num)
UNTIL (num = 999)
ENDWHILE Print(‘The sum is: ‘, sum)
Print(‘The sum is: ‘, sum) Stop
Stop