Pseudo Codes
Pseudo Codes
Pseudocode
Chandula Rajapaksa
1
Pseudocode
➢ What is a Pseudocode?
2
Pseudocode
Pseudocode is the plain English representation of a computer
program or algorithm, which specifies the flow and operation of
the program.
• It is not a programming language.
• It is just a learning and reasoning tool, which is used by programmers
and developers to underline how to write the actual code.
• Pseudocode can not be executed or compiled by any
compiler, interpreter, or assembler.
• Unlike programming language code, pseudocode does not follow a
strict structure and syntax. Here, the programmer can write the code
syntax as he pleases.
3
Why pseudocode?
Before building anything, we first need to create a blueprint
that describes all the methods, strategies, flow of the
structure, and the resulting interface of the actual project.
4
How to write a Pseudocode?
No specific rules.
• Use capital words for reserved commands or keywords, for
example, if you are writing IF…ELSE statements then make sure
IF and ELSE be in capital letters.
• Write only one statement per line.
• Use indentation for the block body. It keeps the body of every
component isolated and indenting different pieces of each block will indicate that
those pieces of pseudocode go under a less intended section.
• Be specific while writing a statement, use plain English to
provide a particular description.
5
How to write a Pseudocode?
▪ Variable :
A variable has a name, a data type, and a value.
It is considered good practice to use variable names
that are relevant to the task at hand.
▪ Assignment
▪ Input/Output :
Input / Output both deal with an outside source (can be
a user or another program) receiving or giving
information.
•Output–Write/display/print
• Input – Read / get / input
8
How to write a Pseudocode?
▪ Selection:
Selection construct allows for a choice between
performing an action and skipping it. It is our conditional
statements.
Selection statements are written as such:
if ( conditional statement)
statement list
else
statement list
9
How to write a Pseudocode?
▪ Repetition:
Repetition is a construct that allows instructions to be
executed multiple times (IE repeated).
In a repetition problem
– Count is initialized
– Tested
– incremented
10
Pseudocode for Different Statements
Operators :
1. Assignment Operator =, <- or :=
11
Special Keywords
1. START: To begin the pseudocode.
2. INPUT: Take input from the user.
3. PRINT: To print the output on the screen.
4. READ/GET: Input format while reading data from the file.
5. SET, INIT: Initialize a value.
6. INCREMENT, BUMP: Increase the value of the variable, equivalent
to a++.
7. DECREMENT: Decrease the value of the variable, equivalent to a--.
8. COMPUTE, CALCULATE, DETERMINE: To calculate the expression
result.
12
Example 01
Write the Pseudocode to calculate the area of a rectangle.
Steps :
⚫ Get user inputs for the length and the width of the rectangle
⚫ Using the calculation formula , calculate the area of the rectangle.
• Area of a rectangle = length * width
⚫ Print the area
13
Example 01 : Answer
BEGIN
Number Length , Width , Area
Length = INPUT : “Enter the length of the rectangle”
Width = INPUT : “Enter the width of the rectangle”
Area = Length * Width
OUTPUT Area
END
14
Example 02
Write the Pseudocode to add 2 numbers.
Steps :
⚫ Get 2 user inputs.
⚫ Add the 2 user inputs
⚫ Print the sum
15
Example 03
Write pseudo code that reads two numbers and multiplies them
together and print out their product
16
Example 04
Write the Pseudocode to check whether a person is eligible to
get drivers’ license. If the applicant’s age is greater than 18 , the
applicant will get the license.
Steps :
⚫ Get the age of the applicant
⚫ Check whether the age is greater than 18 or not
⚫ If the age is greater than 18 , can issue the license
⚫ If the age is less than 18 , can’t issue the license
17
Example 05
Write the Pseudocode to check whether a number is positive or
negative.
Steps : ???
18
Example 06
Write the Pseudocode to check whether a number is even or
odd.
Steps : ???
19
Example 07
Write pseudo code that tells a user that the number they entered
is not a 5 or a 6
Steps : ???
20
Example 08
Write pseudo code that performs the following: Ask a user to
enter a number. If the number is between 0
and 10, write the word blue. If the number is between 10 and 20,
write the word red. if the number is between
20 and 30, write the word green. If it is any other number, write
that it is not a correct color option.
Steps : ???
21
Example 08
Write pseudo code that will perform the following.
a) Read in 5 separate numbers.
b) Calculate the average of the five numbers.
c) Find the largest (maximum) of the five entered numbers.
d) Write out the results found from steps b and c with a message
describing what they are
Steps : ???
22
Example 09
Write pseudo code that reads in three numbers and writes them
all in sorted order.
Steps : ???
23
Example 10
Create a quiz that asks “What is the sum of 2 + 2?”.
If the user inputs 4 they get 1 point and a suitable message. If
the user guesses anything else they see a message saying they
are wrong. The user's score is output at the end of the quiz.
Steps : ???
24
Example 11
Write the Buzz Algorithm Using pseudocode
• Write a code that prints each number from 1 to 30 in a new line.
Steps : ???
25
26