Pseudocode Design
Pseudocode Design
Pseudocode Design
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.
1. Input Statements
2. Output Statements
3. Assignment Statements
4. Control Statements (Sequence, Repetition, Selection)
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.
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.
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.
Please note the syntax for calculation depends on what you are asked to do. Thus each
calculation might be different.
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.
OR
E.g. A program is required to read three numbers, calculate, and print their total.
START
Print 'Enter three numbers'
STOP
KEYWORDS
PSEUDOCODE KEYWORDS
1) The word Start is used to begin an algorithm
Write a program to read 2 numbers. The program should calculate and print the sum, average
and product.
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//
A control structure is a structure that decides the order in which statements will appear or be
performed.
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
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
* 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.
START
Read Num1
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
Read quantity
Read Price
IF (Price>5000) THEN
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
***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
Before After
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 .
While Loops gives a user more flexibility in what you put in it, and when it will stop.
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:
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