PseudocodeBasics
PseudocodeBasics
PseudocodeBasics
Flowcharts were the first design tool to be widely used, but unfortunately they do not
very well reflect some of the concepts of structured programming. Pseudocode, on the
other hand, is a newer tool and has features that make it more reflective of the
structured concepts. Unfortunately, the narrative presentation is not as easy to
understand and follow.
PSEUDOCODE:
READ name, hourlyRate, hoursWorked, deductionRate
grossPay = hourlyRate * hoursWorked
deduction = grossPay * deductionRate
netPay = grossPay – deduction
WRITE name, grossPay, deduction, netPay
SEQUENCE: keep statements that are “stacked” in sequence all starting in the
same column.
SELECTION: indent the statements that fall inside the selection structure, but not
the keywords that form the selection
LOOPING: indent the statements that fall inside the loop, but not the keywords that
form the loop
EX: In the example above, employees whose grossPay is less than 100 do not
have any deduction.
TASK LIST:
Read name, hourly rate, hours worked, deduction rate
Compute gross, deduction, net pay
Is gross >= 100?
YES: calculate deduction
NO: no deduction
Write name, gross, deduction, net pay
PSEUDOCODE:
READ name, hourlyRate, hoursWorked
grossPay = hourlyRate * hoursWorked
IF grossPay >= 100
deduction = grossPay * deductionRate
ELSE
deduction = 0
ENDIF
netPay = grossPay – deduction
WRITE name, grossPay, deduction, netPay
yes no
amount
< 1000
interestRate = interestRate =
.06 .10
Some selections are of the “do it or don’t” (one sided) variety. For example:
IF average < 60
NULL
ELSE
DO GivePassingGrade
ENDIF
This could (and should) be rewritten to eliminate the NULL “yes” part. To do that, we
change the < to its opposite: >= as follows:
IF average >= 60
DO GivePassingGrade
ENDIF
NESTING IF STATEMENTS
What if we wanted to put a little menu up on the screen:
1. Solitaire
2. Doom
3. Monopoly
and have the user select which game to play. How would we activate the correct
game?
DO Monopoly gameNumber
=2?
ENDIF DO
ENDIF Solitaire
DO Doom DO
Monopoly
We must pay particular attention to where the IFs end. The nested IF must be
completely contained in either the IF or the ELSE part of the containing IF. Watch for
and line up the matching ENDIF.
LOOPING STRUCTURES
One of the most confusing things for students first seeing a flowchart is telling the loops
apart from the selections. This is because both use the diamond shape as their control
symbol. In pseudocode this confusion is eliminated. To mark our loops we will use
these pairs: WHILE / ENDWHILE REPEAT / UNTIL
Count = 0 count = 0
WHILE count < 10
ADD 1 to count
WRITE count
ENDWHILE
WRITE “The end”
Count no
< 10
yes
Notice that the connector and test at the
top of the loop in the flowchart become
Sometimes it is desirable to put the steps that are inside the loop into a separate
module. Then the pseudocode might be this:
Count = 0 count = 0
REPEAT
ADD 1 to count
WRITE count
UNTIL count >= 10
WRITE “The end”
Add 1 to count
Notice how the connector at the top of the loop
corresponds to the REPEAT keyword, while
the test at the bottom corresponds the the
Write UNTIL statement. When the loop is over,
count control goes to the statement following the
UNTIL.
no Count
>= 10
yes
Write
“The end”
STOP
ADVANTAGES AND DISADVANTAGES
Pseudocode Disadvantages
It’s not visual
There is no accepted standard, so it varies widely from company to company
Pseudocode Advantages
Can be done easily on a word processor
Easily modified
Implements structured concepts well
Flowchart Disadvantages
Hard to modify
Need special software (not so much now!)
Structured design elements not all implemented
Flowchart Advantages
Standardized: all pretty much agree on the symbols and their meaning
Visual (but this does bring some limitations)
The computer stores all program data into memory locations. It knows these location by
their addresses. It is perfectly able to understand code like:
READ 19087, 80976, 10943, 80764
but we would have a hard time with it. So we name our storage locations using words
that are descriptive to us. Every language has its own (different) set of rules about how
these names are formed. We will use a simple style:
The READ statement tells the computer to get a value from the input device (keyboard,
file, …) and store it in the names memory location.
When we need to compute a value in a program (like grossPay) we will use what is
called an assignment statement.
variable = expression
CALCULATION SYMBOLS
We will often have to represent an expression like the one that computes grossPay. To
symbolize the arithmetic operators we use these symbols
grouping ( )
exponent ** or ^
multiply *
divide /
add +
subtract -
ORDER OF EXECUTION
( ) equations in parenthesis
Note: when operators of equal value are to be executed, the order of execution
is left to right.
Examples:
AREA = R2
SUM = A2 + B2
PERIM = 2(L + W)
A D D2
A A A C
BC BC BC
2
BC BC B
SYMBOL IS OPPOSITE TO
> <=
< >=
= <>
OR: if any of the conditions are true, the whole expression is true
NOT: reverses the outcome of the expression; true becomes false, false becomes true.