0% found this document useful (0 votes)
25 views9 pages

Session 9 - Pseudocode

Uploaded by

Brian Were
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views9 pages

Session 9 - Pseudocode

Uploaded by

Brian Were
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

SESSION NINE

PSEUDOCODE

9.1 Introduction

9.2 Learning Outcomes

9.3 What is a Pseudocode?

9.4 The main constructs of pseudocode

9.5 Keywords for writing Pseudocode

9.6 Advantages of Pseudocode

9.7 Steps on How to design a Pseudocode

9.8 Summary

9.9 Activity

9.1 Introduction

Welcome to session nine!!! In our previous session, we defined an algorithm


as merely the sequence of steps taken to solve a problem. In programming,
we said that an algorithm is a procedure for solving a problem in terms of the
actions to be executed and the order in which those actions are to be executed.
We also saw that algorithms can be represented in two ways namely:
pseudocode and/or flowchart. In this session, we shall discuss pseudocodes
in depth. Welcome!!!

9.2 Learning Outcomes

At the end of this course, you should be able to:

1. Demonstrate knowledge and understanding of


pseudocode.
2. List and discuss the Keywords for writing Pseudocode
3. Draw a pseudocode
9.3 What is a Pseudocode?

Pseudocode is a term which is often used in programming and algorithm-


based fields. It is an implementation of an algorithm in the form of
annotations and informative text written in plain English. Pseudocode does
not require any strict programming language syntax or underlying technology
considerations. It is used for creating an outline or a rough draft of a program.
Pseudocode summarizes a program's flow, but excludes underlying details.

As a methodology, pseudocode is a design tool that allows the programmer to


represent the implementation of an algorithm. Often at times, algorithms are
represented with the help of pseudocodes as they can be interpreted by
programmers no matter what their programming background or knowledge
is. Pseudo code, as the name suggests, is a false code/fake code or a
representation of code which can be understood by even a layman with some
school level programming knowledge.

9.4 The main constructs of pseudocode

Pseudocode has no syntax unlike programming language and thus can’t be


compiled or interpreted by the computer. The core of pseudocode is the ability
to represent the programming constructs: SEQUENCE, CASE, WHILE,
REPEAT-UNTIL, FOR, and IF-THEN-ELSE. These constructs (also called
keywords) are used to describe the control flow of the algorithm. In C, the
sequence of steps making up an algorithm are normally implemented using
these programming constructs namely: sequence, selection, iteration, and a
case-type statement. sequence statements are imperatives, the selection is
the "if then else" statement, and the iteration is satisfied by a number of
statements, such as the "while”, "do" and the "for," while the case-type
statement is satisfied by the "switch" statement. This shall be discussed in
depth in under Control Structures.
9.5 Keywords for writing Pseudocode

For looping and selection, the keywords that are to be used include Do
While...EndDo; Do Until...Enddo; Case...EndCase; If...Endif; Call ... with
(parameters); Call; Return ....; Return; When; Always use scope terminators
for loops and iteration. As verbs, use the words Generate, Compute, Process,
etc. Words such as set, reset, increment, compute, calculate, add, sum,
multiply, ... print, display, input, output, edit, test, etc. with careful
indentation tend to foster desirable pseudocode. Do not include data
declarations in your pseudocode.

The following examples shows some keywords that can be used while writing
a pseudocode:

a. For If…else statement

If student's grade is greater than or equal to 60

Print "passed"

else

Print "failed"

b. Using while statement

Set total to zero

Set grade counter to one

While grade counter is less than or equal to ten

Input the next grade

Add the grade into the total

Set the class average to the total divided by ten

Print the class average


c. While and if statements

Initialize total to zero

Initialize counter to zero

Input the first grade

while the user has not as yet entered the sentinel

add this grade into the running total

add one to the grade counter

input the next grade (possibly the sentinel)

if the counter is not equal to zero

set the average to the total divided by the counter

print the average

else

print 'no grades were entered'

d. While and if statements

initialize passes to zero

initialize failures to zero

initialize student to one

while student counter is less than or equal to ten

input the next exam result

if the student passed

add one to passes

else

add one to failures


add one to student counter

print the number of passes

print the number of failures

if eight or more students passed

print "raise tuition"

9.6 Advantages of Pseudocode

i. Better readability. Often, programmers work alongside people from


other domains, such as mathematicians, business partners, managers,
and so on. Using pseudocode to explain the mechanics of the code will
make the communication between the different backgrounds easier and
more efficient.

ii. Ease up code construction. When the programmer goes through the
process of developing and generating pseudocode, the process of
converting that into real code written in any programming language will
become much easier and faster as well.

iii. A good middle point between flowchart and code. Moving directly
from the idea to the flowchart to the code is not always a smooth ride.
That’s where pseudocode presents a way to make the transition
between the different stages somewhat smoother.

iv. Act as a start point for documentation. Documentation is an


essential aspect of building a good project. Often, starting
documentation is the most difficult part. However, pseudocode can
represent a good starting point for what the documentation should
include. Sometimes, programmers include the pseudocode as a
docstring at the beginning of the code file.

v. Easier bug detection and fixing. Since pseudocode is written in a


human-readable format, it is easier to edit and discover bugs before
actually writing a single line of code. Editing pseudocode can be done
more efficiently than testing, debugging, and fixing actual code.
9.7 Steps on How to design a Pseudocode

i. Arrange the sequence of tasks and write the pseudocode accordingly.

ii. Start with the statement of a pseudo code which establishes the main
goal or the aim.

Example:

This program will allow the user to check the number whether it's even
or odd.

iii. The way the if-else, for, while loops are indented in a program, indent
the statements likewise, as it helps to comprehend the decision control
and execution mechanism. They also improve the readability to a great
extent.

Example:

if "1"

print response

"I am case 1"

if "2"

print response

"I am case 2"

iv. Use appropriate naming conventions. The human tendency follows the
approach to follow what we see. If a programmer goes through a pseudo
code, his approach will be the same as per it, so the naming must be
simple and distinct.

v. Use appropriate sentence casings, such as CamelCase for methods,


upper case for constants and lower case for variables.

vi. Elaborate everything which is going to happen in the actual code. Don’t
make the pseudo code abstract.
vii. Use standard programming structures such as ‘if-then’, ‘for’, ‘while’,
‘cases’ the way we use it in programming.

viii. Check whether all the sections of a pseudo code are complete, finite and
clear to understand and comprehend.

ix. Don’t write the pseudo code in a complete programmatic manner. It is


necessary to be simple to understand even for a layman or client, hence
don’t incorporate too many technical terms.
Examples of Pseudocode

Let's consider an example of pseudocode to create a program to add 2


numbers together and then display the result.

Solution:Pseudocode

Start Program
Enter two numbers, A, B
Add the numbers together
Print Sum
End Program

The flowchart for above pseudocode is given below:


9.8 Summary

In this session we have learnt about pseudocode and the role they play in
problem solving. Next session, we will discuss about flowcharts.

9.9 Activity

Write a pseudocode for student admission process at Technical University of


Mombasa. The pseudocode should show the process of admission from
application to admission, registration of units up to attending classes.

You might also like