Lecture 05-PseudoCode

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 18

Today We will cover

• To describe pseudocode
• To show examples of pseudocode
• To practice using pseudocode
• To compare pseudocode with flowcharts
Pseudocode

• Flowcharts were the first design tool to be widely used,


but unfortunately they do not reflect some of the
concepts of structured programming very well.

• Pseudocode, on the other hand, is a newer tool and has


features that make it more reflective of the structured
concepts.

• The drawback is that the narrative presentation is not as


easy to understand and/or follow.
What is it?

• Pseudo code is a kind of structured English for describing algorithms.


• It allows the designer to focus on the logic of the algorithm without
being distracted by details of language syntax.
• At the same time, the pseudo code needs to be complete.
It describes:
• the entire logic of the algorithm so that implementation becomes a
route
• mechanical task of translating line by line into source code.
• It omits detailed subroutines, variable declarations, and language-
specific syntax.
Pseudocode

• Artificial, informal language that helps us develop algorithms


• Similar to everyday English
• Not actually executed on computers
• Helps us “think out” a program before writing it
• Easy to convert into a corresponding program language
• Consists only of executable statements
Rules for Pseudocode

• Write only one statement per line


• Capitalize initial keyword
• Indent to show hierarchy
• End multi-line structures
• Keep statements language independent
One Statement Per Line
• Each statement in pseudocode should express
just one action for the computer. If the task list is
properly drawn, then in most cases each task will
correspond to one line of pseudocode.

Task List Pseudocode


Read name, hours worked, READ name, hoursWorked,
rate of pay payRate
Perform calculations gross = gross = hoursWorked * payRate
hours worked * rate of pay
WRITE name, hoursWorked,
Write name, hours worked, gross
gross
Capitalize Initial Keyword

In the example below note the words: READ and WRITE.


These are just a few of the keywords to use, others include:

READ, WRITE, IF, ELSE, ENDIF, WHILE, ENDWHILE

Pseudocode
READ name, hoursWorked, payRate
gross = hoursWorked * payRate
WRITE name, hoursWorked, gross
Indent to Show Hierarchy
Each design structure uses a particular indentation pattern

• Sequence: Keep statements in sequence all starting in the same column

• Selection: Indent statements that fall inside selection


structure, but not the keywords that form the selection
• Loop: Indent statements that fall inside the loop but not
keywords that form the loop
READ name, grossPay, taxes
IF taxes > 0
net = grossPay – taxes
ELSE
net = grossPay
ENDIF
WRITE name, net
End Multiline Structures
READ name, grossPay, taxes
IF taxes > 0
net = grossPay – taxes
ELSE
net = grossPay
ENDIF
WRITE name, net

In the IF/ELSE/ENDIF as constructed above, the


ENDIF is in line with the IF.

The same applies for WHILE/ENDWHILE etc…


Language Independence

• Resist the urge to write in whatever language you are


most comfortable with, in the long run you will save time.
• Remember you are describing a logic plan to develop a
program, you are not programming!
The Selection Structure
yes no
amount < 100

interestRate = .06 interestRate = .10

IF amount < 100


interestRate = .06
ELSE
Pseudocode 
InterestRate = .10
ENDIF
The Looping Structure
• In flowcharting one of the more confusing things
is to separate selection from looping. This is
because each structure uses the diamond as
their control symbol.
• In pseudocode we avoid this by using specific
keywords to designate looping

WHILE/ENDWHILE
REPEAT/UNTIL
WHILE / ENDWHILE
Start
count = 0
WHILE count < 10
ADD 1 to count
count = 0
WRITE count
ENDWHILE
WRITE “The End”
count
Mainline
<10  Modular
count = 0
Write WHILE count < 10
add 1 to “The End”
count DO Process
ENDWHILE
Stop WRITE “The End”
write count

Process
ADD 1 to count
WRITE count
REPEAT / UNTIL
Start count = 0
REPEAT
count = 0
ADD 1 to count
WRITE count
UNTIL count < 10
add 1 to WRITE “The End”
count

Mainline  Modular
write count count = 0
REPEAT
DO Process
count
<10 UNTIL count < 10
WRITE “The End”
Write Process
“The End”
ADD 1 to count
Stop WRITE count
Advantages & Disadvantages
Flowchart Advantages: Pseudocode Advantages
 Standardized  Easily modified
 Visual  Implements structured concepts
 Done easily on Word Processor

Flowchart Pseudocode
Disadvantages: Disadvantages:
 Hard to modify
 Not visual
 Structured design elements not
 No accepted standard, varies from
implemented
company to company
 Special software required
Quiz 1
Time Allowed: 15
min
Q1. Draw a flow chart to find the largest
of three numbers and print it? [6]

Q2. Write Pseudo code for problem in


question 1. [4]
Assignment 1
Deadline: Tuesday, 03.00 pm

Q1. Draw a flowchart to read a number N and print all its


divisors.
Q2. Draw a flowchart to compute the sum of squares of
integers from 1 to 50.
Q3. Draw a flowchart to arrange the given data in an ascending
order.
Q4. Design an algorithm that outputs the ith largest number in
a group of n numbers.
Q5. Draw a flow chart to print the shown figure.
Q6. Write Pseudo code for Q1 to Q5.
Thank You!

You might also like