You are on page 1of 74

PSEUDOCODE

ALGORITHM
What is a Pseudocode
Pseudocode is Pseudocode Program Construct
A language similar to a Statements are not executed by the
1. Sequence
programming language but the computer but are used by programmers
as the foundation for developing 2. Selection
rules are not as stringent. Each
statement must have the correct programming codes. 3. Iteration / Repetition/
syntax. It is used as a guide to code the Loop
an imitation of a computer solution to the problem in a high
program written using language.
mathematical notations and Pseudocodes and programs are made
English-like statements to up of four kinds of statements:
describe the logic of solving a Input
problem or carrying out a Processing
procedure. It is used in place of Control
symbols or flowcharts. Output statements.
Allow input for a program
in other words is used to get

INPUT data from outside the


computer into the

STATEMENT computer.
As data is entered into the
computer, it is stored in the
computer’s memory in
variables, or it may be stored
in tables so that it can be
INPUT accessed when needed for
processing.
STATEMENT
Data can also be stored in a
statement in the form of
constants.
The commands that are used
to permit the input of data are

INPUT the words:


READ or INPUT.

STATEMENT
Syntax : READ Variable name
When more than one variable is
used, place the comma between
each variable.
READ Variable name, variable

INPUT name

STATEMENT
Input Statement
Solutions
Example 1
READ Quantity, Price Write an instruction to
input the quantity and
price of an item.

Example 2
READ Score 1, Score 2, Score 3
Write an instruction to read
three scores
Class Work
Write instructions to input the following:
A number
01
The name, age and height of
02 a student

The names of four students


03
Used to get information to the
programmer or computer
user.
can take many forms such as
sound, text, voice and
OUTPUT graphics.

STATEMENT
The command that is used to
produce the output is PRINT.

OUTPUT
STATEMENT
OUTPUTTING THE VALUE
OF A VARIABLE

Syntax PRINT variable PRINT SUM

NB When a variable is printed, the content of the variable is printed and not the
name of the variable. The computer locates the segment of memory with the
name of the variable and print the information that is stored in that segment.
Output Statement
A = 10
PRINT A Example
The Variable is A, The value
NB: The variable used with the PRINT instruction must
not be a new variable that is being introduced in the is 10.
instruction but should be a variable that was executed in The 2nd instruction causes 10
an instruction before the PRINT instruction. Hence, it is
to be printed and not A
necessary to refer to prior instructions to ensure that this
rule is enforced. because 10 is what is in A.
OUTPUTTING A Message

Syntax PRINT STRING PRINT “NAME”

NB NAME IS PRINTED WITHOUT THE QUOTATION MARKS. When


outputting a message, the data should be enclosed in quotation marks.
Outputting a Message and Variable

Syntax PRINT STRING, VARIABLE PRINT “COST IS:”, COST

NB “Cost” is the message. Cost is variable.


The word “cost” is printed as well as the value stored in the variable
COST
Output Statement
Example
In the following algorithm
Price = 20.00
PRINT “Price of Item”, Price 20.00 is stored in the variable
PRICE and “Price of Item”
Constant.
Hence, Price of Item: 20.00 is
printed.
Class Work
Write instructions to output the following:

1. “Today is Monday”
2. Simone Anderson
3. To read and print the name of a student
4. To read and output the license plate number, route
it will be travelling and number of passengers of
a car
A statement that assigns a
value to a variable is called an
assignment statement. It has
two parts:
1. LVALUE
ASSIGNMENT 2. RVALUE

STATEMENT
The LVALUE refers to the
variable as the storage
location where the RVALUE
will be stored.

ASSIGNMENT
STATEMENT
The RVALUE refers to a
value, which may be the result
of an expression or the content
of another variable.

ASSIGNMENT
STATEMENT
Assignment Statement

Syntax Variable name = expression DAY = 28


RATE = 500

NB The value being assigned could be a constant, a variable or a calculation.


Assignment Statement Examples
Sum = 0 OR store 0 to Sum An assignment statement
begins with a variable, followed
A = B OR store B to A by the equal sign, then either a
variable, a constant or a
Grade = “A” OR store “A” to Grade calculation OR the word
STORE, followed by a variable,
Total = A+B+C OR store A+B+C to Total a constant or a calculation of
the word TO and a variable.
Assignment Statement Examples

Total = Deposit + Withdrawal + Interest

Deposit + Withdrawal + Interest = Total


It is incorrect to write a
calculation in this manner
The variable, which begins an
assignment statement, should
not be a variable that was
read as input if the input is to
be printed.
ASSIGNMENT
STATEMENT
Variables used in the formula for
calculating must be variables
that were used before the
calculation instruction and not
new variables.
ASSIGNMENT
STATEMENT
Assignment Statement Examples

A=1 The following instructions are


B=2 executed in the given order.
C=A+B
If the 1st two instructions with A or
C is given the value of the total of A & B B were not executed, then
I.E. C = 1 + 2 = 3 reference to A or B in the 3rd
instruction would be incorrect and
the computer would be unable to
calculate A + B
Class Work

Write a pseudocode algorithm to calculate the salary of a day’s


worker. Input to the pseudocode are hours worked for the day
and the hourly rate.
Class Work Basic Structure

01 02 03

Declare all variables with START Initialize salary to zero


the appropriate data type.
Class Work Basic Structure

04 05 06

Write prompt and input Write assignment Write output statements


statements statements
Class Work Basic Structure

07

STOP
Class Work Solution
Declare hworKed as Float For this pseudocode, we need
Declare Hrate as Float three (3) variables because we
Declare DaysPay as Float
need to store three (3) pieces of
START
DaysPay = 0 information hours worked, hourly
PRINT “Enter the hours worked and rate and days’ salary.
hourly rate” Therefore:
READ hworKed, Hrate  hworKed
DaysPay = hworKed * Hrate
 Hrate
PRINT “Day’s Salary is: $”, DaysPay
STOP  DaysPay
An instruction containing the
command IF allows deviation
and selection to take place.
Given criteria are used to form
conditions.
SELECTION
CONSTRUCTS
A condition is made up of three parts:

01 02 03

Variable that was carried Relational operator A variable that was carried
out before out before, a constant or
value.
Condition Examples
Conditions may involve data of different
Colour = “blue” data types. When the data of the condition
Style = “straps” is the character data type, the data should
Heel = “4-inch”
be enclosed in quotation marks.
Cost = “$75”

A condition is evaluated when the instruction


containing it is carried out. When the
condition is met, the condition is said to be
true, if the condition is not met, then the
condition is said to be false.
When using the command IF,
the instructions forming the
condition and selection are
called a construct. Each
construct will perform
SELECTION differently;

CONSTRUCTS
IF-THEN Construct
This is used when one
option is available, and a
IF W_Condition = “rain is falling” THEN selection may or may not
PRINT “take umbrella” be made.
ENDIF
IF-THEN-ELSE
IF W_Condition = “rain is falling” THEN
PRINT “take an umbrella” This is used when two
ELSE options are available, and a
PRINT “Don’t take an umbrella” selection must be made.
ENDIF
IF-THEN-ELSE-IF
IF Choice= 1 THEN
PRINT “You are ordering Fried This is used when two or
Chicken”
ELSEIF Choice= 2 THEN more options are available,
PRINT “You are ordering Chicken and a selection may or may
Soup”
not be made.
ELSEIF Choice= 3 THEN
PRINT “You are ordering Patty”
ENDIF
ENDIF
ENDIF
Making Selection
Based on Two or The selection to be chosen could depend on
two or more conditions. When this is
More Conditions required, logical operators AND and OR are
used.
Making Selection Based on Two or More Conditions

AND is used when all the conditions in the


instruction work together as a unit.
AND
AND is also used to limit values to a particular
Range.
Logical
Operator

When OR is used the conditions work independently.


OR If either condition is true, the THEN path is selected.
Logical Operator AND

Declare STATUS as STRING


PACKAGE as integer Read the status of a guest
START
PRINT “Enter your status and package” and package. If the status is
REA STATUS, PACKAGE “Resident” and the package
IF STATUS = “Resident” AND PACKAGE = 4 THEN
is “4”, print discount allowed
PRINT “Discount Allowed”
ELSE otherwise print no discount.
PRINT “No Discount”
ENDIF
STOP
Logical Operator OR

Declare Day as STRING


START Read the name of a day. IF
PRINT “Enter the Day”
REA DAY the name is “Monday” or
IF DAY = “Monday” OR DAY = “Tuesday” THEN “Tuesday” print “correct
PRINT “Correct Day”
day”.
ENDIF
STOP
Class Work
Employees are given a salary increase as follows:
6% of the salary if the salary is more than $3000. 8% of the salary if the salary is
more than $2000 but less than or equal to $3000. 10% of the salary if the salary is
less than or equal to $2000.
Read a salary. Output the increased amount and new salary.
Class Work Solution
Declare salary, increase and newSalary as REAL
START
increase = 0
newSalary = 0
PRINT “Enter your salary”
READ salary
IF salary > 3000 THEN
increase = salary * 0.06
ELSEIF salary > 2000 AND salary <= 3000 THEN
increase = salary * 0.08
ELSEIF salary < =2000 THEN
increase = salary * 0.1
ENDIF
Class Work Solution
ENDIF
ENDIF
newSalary = salary + increase
PRINT “Your increase amount is”, increase
PRINT “Your new salary is: $” newSal
STOP
Class Work
A student is given a 10% discount if the cost of a subject exceeds $100. 5% of the
cost is refunded if the student receives grade ‘A’ in the examination. Read the cost
and a grade. Output the discount amount and the amount refunded.
Loops are useful for repeating
parts of a program. That is,
they will repeatedly execute a
section of a program until the
end condition is satisfied.
ITERATION
CONSTRUCTS
To exit a loop, you must have a
method for checking to see if you
have completed the task.

ITERATION
CONSTRUCTS
Once a loop terminates, control
is returned to the first sentence
after the block of sentences in
the loop.

ITERATION
CONSTRUCTS
Types of Loop
Indefinite Definite
Also known as a "for loop". Executes
Also known as a conditional loop. its body of code as for a fixed number
Executes its body of code as long as a of times. The term "definite" signifies
specified condition remains true. Will that the number of iterations is known
execute until certain conditions are before the loop starts executing.
met. FOR Loop
(WHILE or REPEAT Loops)
While Loop
With the while loop you have

A User Input

B Check Condition

C Perform Statements
Repeatedly executes a
statement or a block of
statements as long as the
condition is true.

WHILE
LOOP
Condition in a while loop is
tested at the beginning of the
loop, so it is possible for the
statement not to be executed at

WHILE all.

LOOP
WHILE Loop General Form

WHILE <CONDITION>
<STATEMENT>
ENDWHILE

OR

WHILE <CONDITION IS TRUE> DO


<STATEMENT>
ENDWHILE
WHILE Loop General Form

WHILE <CONDITION IS TRUE> DO


BEGIN If you have a block of
STATEMENT 1
STATEMENT 2 statements, then they are
STATEMENT 3 placed between BEGIN and
END
END statements
ENDWHILE
EXAMPLES

DECLARE Day_Type as STRING


START The output is to attend classes
PRINT “Enter type of day: School Day | while it is a school day. If it is not
Weekend” a school day (for example, a
READ Day_Type weekend or holiday) then the
WHILE (Day_Type = “SCHOOL DAY”) DO statement “Attend classes” will
PRINT “ATTEND CLASSES”
not be executed.
PRINT “Enter type of day: School Day |
Weekend”
READ Day_Type
ENDWHILE
STOP
EXAMPLES

DECLARE Number as Integer


START
Number = 1
WHILE (Number <= 3) DO
BEGIN These two
PRINT “The number is”, Number statements are
Number = Number + 1 executed only in the
END loop.
ENDWHILE
STOP
Repeat- Until Loop

With the repeat loop you have

A User Input

B Perform statements

C Check Condition
Executes a statement or a
block of statements as long as
the specified condition in the
UNTIL statement is false.

REPEAT- UNTIL
LOOP
Note that this condition is
tested at the end of the loop, so
the statement will always be
executed at least once.

REPEAT- UNTIL
LOOP
REPEAT-UNTIL Loop General Form

REPEAT
<STATEMENT>
UNTIL (CONDITION)

OR

REPEAT
<STATEMENT>
UNTIL <CONDITION IS TRUE>
REPEAT-UNTIL Loop General Form

REPEAT
STATEMENT 1
STATEMENT 2 If you have a block of
STATEMENT 3
statements, then they are
………
UNTIL (CONDITION is True) placed between REPEAT and
UNTIL statements
EXAMPLES

DECLARE Number as Integer


START
Number = 1
REPEAT
PRINT “The number is”, Number
number = Number + 1
UNTIL (Number <3)

STOP
EXAMPLES

DECLARE Day_Type as STRING


START
REPEAT
PRINT “Enter the type of day: School Day | Weekend”
READ Day_Type
IF (Day_Type = “Weekend” THEN
PRINT “DO NOT GO TO SCHOOL”
ENDIF
UNTIL (Day_Type = “SCHOOL DAY”)
STOP
For Loop

A Variable

B Equal

C Start Value

C To End Value
FOR Loop General Form

FOR <VARIABLE = < Start Value> To <Final Value> DO


BEGIN There MUST be a variable
STATEMENT 1
that will keep track of your
STATEMENT 2
STATEMENT 3 count.
END
ENDFOR
FOR Loop General Form

FOR <VARIABLE = < Start Value> To <Final Value> DO


BEGIN There MUST be an equal sign
STATEMENT 1
(=) after the variable
STATEMENT 2
STATEMENT 3
END
ENDFOR
FOR Loop General Form

FOR <VARIABLE = < Start Value> To <Final Value> DO


BEGIN The variable begins with the
STATEMENT 1
<start value>
STATEMENT 2
STATEMENT 3
END
The loop variable is increased
ENDFOR
every time you go round the
loop
FOR Loop General Form

FOR <VARIABLE = < Start Value> To <Final Value> DO


BEGIN The loop terminates when the
STATEMENT 1
counter reaches the value of
STATEMENT 2
STATEMENT 3 the final expression.
END
ENDFOR
FOR Loop Example

Declare Age as Integer.


START The loop terminates when the
FOR AGE = 13 TO 19 DO
counter reaches the value of
PRINT “YOU ARE A TEENAGER”
ENDFOR the final expression.
STOP
FOR Loop Example

Declare A , B, C, i as Integer.
START
A=5
B = 10
FOR i = A TO B DO
C=i+B
PRINT “The value of C is:”, C
ENDFOR
STOP
Class Work
Write a program that will prompt the user to enter a number until the user enters a
greater than 100. Once a number greater than 100 is entered, the program should
print "Number exceeds 100" and stop.
Class Work
Write pseudocode for a program that continues to print "Hello, World!" until a user
enters the word "stop".
Class Work
Write pseudocode for a program that prints out the first ten multiples of a number
provided by the user.
“Problem-solving is the adventure of the
mind. Each challenge, a puzzle; every
solution, a triumph. Dive in, think deeply,
emerge wiser. Your journey shapes the world,
one solution at a time."

You might also like