2.1.2 Pseudocode PDF
2.1.2 Pseudocode PDF
Introduction to Pseudocode
U
This section covers the use of pseudocode in the production of algorithms. Students should use standard
computing text books to find out information on the features of programming languages (high level and
low level), interpreters, compilers, assemblers, user documentation and technical documentation.
No specific programming language is referred to; development of algorithms using pseudocode uses
generic descriptions of looping, branching, data manipulation, input/output, totaling and counting
techniques.
Assignment
An assignment is an instruction in a program that places a value into a specified variable.
Some typical assignments are:
TheLength 20.5
TheUsersName$ “Charlie”
TheArea TheWidth * TheLength
TotalCost LabelledCost + 15
Counter Counter + 1
Note that the last example is a common method used to increment the value of a variable. It could be read
as:
What is an algorithm?
It is a procedure for solving a problem in terms of the actions to be executed and the order in which those
0T 0T
actions are to be executed. An algorithm is merely the sequence of steps taken to solve a problem. The
steps are normally "sequence," "selection,” "iteration," and a case-type statement.
Page 1 of 13
Computer Science 2210 (Notes)
Chapter: 2.1 Algorithm design and
problem-solving
Relational operators are used in the format: [Expression] [Operator] [Expression] and will always return a
Boolean (True or False) value.
Relational operators are typically used with the IF selection and also within conditional loops
(REPEAT-UNTIL or WHILE-WEND).
In the following table, the variables a and name$ have the following assignments:
a3+5
name$ “JAMES”
Page 2 of 13
Computer Science 2210 (Notes)
Chapter: 2.1 Algorithm design and
problem-solving
AND and OR
The AND and OR operators always return a Boolean result and are used in the format:
The following ‘truth’ table summarises the result of the Boolean operations:
Values Results
NOT
The NOT operator reverses the result of the Boolean expression and is used in the format:
NOT [Boolean]
Page 3 of 13
Computer Science 2210 (Notes)
Chapter: 2.1 Algorithm design and
problem-solving
case of if … then
case number of if number = 1 then x x + 1
1: x x + 1 else if
2: y y + 1 number = 2 then y y + 1
otherwise print “error” else print “error”
endcase endif
endif
SELECT-CASE
This selection method is used if there are MORE THAN TWO possible outcomes to a test:
Creating a Select-Case statement is simple to do. The next program will prompt the user to select the key
A-D and the program will respond by telling the user what key was entered.
CLS
PRINT
PRINT
KeyPressed = UCASE$(KeyPressed)
Page 4 of 13
Computer Science 2210 (Notes)
Chapter: 2.1 Algorithm design and
problem-solving
CLS
PRINT
PRINT
INPUT "Enter The Test Score: ", Score
Page 5 of 13
Computer Science 2210 (Notes)
Chapter: 2.1 Algorithm design and
problem-solving
Example:
| if X > 10 then |
| PRINT X; " is > 10" |
| else |
| PRINT X; " is <= 10" |
| endif |
If count = 0 Then
message = "There are no items."
ElseIf count = 1 Then
message = "There is 1 item."
Else
message = "There are " & count & " items."
End If
Page 6 of 13
Computer Science 2210 (Notes)
Chapter: 2.1 Algorithm design and
problem-solving
Iteration
Iteration is a control structure in which a group of statements is executed repeatedly – either a set
number of times or until a specific condition is True.
There are three common ways of performing a looping function:
1. for … to … next,
2. while … endwhile
3. repeat … until
The following example input 100 numbers and finds the total of the 100 numbers and outputs this total.
All three looping techniques are shown:
FOR-NEXT
This is an unconditional loop in which the number of repetitions is set at the beginning.
FOR X = 1 TO 5
Answer = X*3
OUTPUT X, Answer
NEXT
Sample code:
|10 sum = 0 |
|20 FOR x = 1 to 10 |
|30 print x |
|40 input "enter a number";n |
|50 sum = sum + n |
|60 NEXT x |
|70 print "The sum of the numbers you gave is";sum |
Page 7 of 13
Computer Science 2210 (Notes)
Chapter: 2.1 Algorithm design and
problem-solving
| 10 sum = 0 |
| 20 x = 1 |
| 30 WHILE x < 11 |
| 40 print x |
| 50 input "enter a number";n |
| 60 sum = sum + n |
| 70 x = x + 1 |
| 80 WEND |
| 90 print "The sum of the numbers you gave is";sum |
REPEAT-UNTIL
This is a conditional loop, which has a test at the end and repeats until the condition is true:
X = 0
REPEAT
X = X + 1
Answer = X*3
OUTPUT X, Answer
UNTIL X > 4
1.1) Counting
Counting in 1s is quite simple; use of the statement count count + 1 will enable counting to be done
(e.g. in controlling a repeat loop). The statement literally means: the (new) count =the (old) count + 1.
It is possible to count in any increments just by altering the numerical value in the statement (e.g. count
count – 1) will count backwards.
Page 8 of 13
Computer Science 2210 (Notes)
Chapter: 2.1 Algorithm design and
problem-solving
1.2) Totaling
1.3) Input/output
Input and output indicated by the use of the terms READ number, PRINT total, PRINT “result is” x and so
on.
The following five examples use the above pseudocode terms. These are the same problems discussed in
section 3.1 using flow charts – both methods are acceptable ways of representing an algorithm.
2.1 Example 1
A town contains 5000 houses. Each house owner must pay tax based on the value of the house. Houses
over $200 000 pay 2% of their value in tax, houses over $100 000 pay 1.5% of their value in tax and houses
over $50 000 pay 1% of their value in tax. All others pay no tax.
Write an algorithm to solve the problem using pseudocode.
Notes:
(1) a while loop or a repeat loop would have worked just as well
(2) the use of endif isn’t essential in the pseudocode
Page 9 of 13
Computer Science 2210 (Notes)
Chapter: 2.1 Algorithm design and
problem-solving
EXERCISE: Re-write the above algorithm using a repeat loop and modify the if … then … else statements to
include both parts of the house price range.
(e.g. if house > 50000 and house <= 100000 then tax = house * 0.01)
2.2 Example 2
NOTE: It is much easier in this example to input x first and then loop round doing the calculation until
eventually x = 0. Because of this, it would be necessary to input x twice (i.e. inside the loop and outside the
loop). If input x occurred only once it would lead to a more complicated algorithm.
(Also note in the algorithm that <> is used to represent ≠ “not equals to” ).
A while loop is used here, but a repeat loop would work just as well.
input x
while x <> 0 do
if x = 1 then print “error”
else n (x * x)/(1 – x)
print n, x
endif
input x
endwhile
Page 10 of 13
Computer Science 2210 (Notes)
Chapter: 2.1 Algorithm design and
problem-solving
(NOTE: since the number of inputs is known, a for … to loop can be used. However, a while loop or a repeat
loop would work just as well).
total1 0 : total2 0
for days 1 to 100
input temperature
if temperature < 20 then total1 total1 + 1
else total2 total2 + 1
endif
next
print total1, total2
This is a good example of an algorithm that could be written using the case construct rather than if … then
… else. The following section of code replaces the statements if temperature < 20 then …… endif:
case temperature of
1: total1 = total1 + 1
2: total2 = total2 + 1
endcase
Page 11 of 13
Computer Science 2210 (Notes)
Chapter: 2.1 Algorithm design and
problem-solving
(NOTE: Again since the actual number of data items to be input is known any one of the three loop
structures could be used. It is necessary to set values for the fastest (usually set at zero) and the slowest
(usually set at an unusually high value) so that each input can be compared. Every time a value is input
which > the value stored in fastest then this input value replaces the existing value in fastest; and similarly
for slowest).
fastest 0: count 0
slowest 1000
repeat
input top_speed
total total + top_speed
if top_speed > fastest then fastest top_speed
if top_speed < slowest then slowest top_speed
endif
endif
count count + 1
until count = 5000
average total * 100/5000
print fastest, slowest, average
Page 12 of 13
Computer Science 2210 (Notes)
Chapter: 2.1 Algorithm design and
problem-solving
Write an algorithm using pseudocode which input the codes for all items in stock and outputs the number
of books, maps and magazine in stock. Include any validation checks necessary.
(NOTE: A 4-digit code implies all books have a code lying between 1000 and 1999, all maps have a code
lying between 2000 and 2999 and all magazines a code lying between 3000 and 3999. Anything outside
this range is an error)
(NOTE: A function called INT(X) is useful in questions like this. This returns the integer (whole number)
part of X e.g. if X = 1.657 then INT(X) = 1; if X = 6.014 then INT(X) = 6 etc. Using this function allows us to
use the case statement to answer this question:
(This is probably a more elegant but more complex solution to the problem)
Page 13 of 13