3.17. Pseudocode#
We can write our control structures such as loops in a form using pseudocode which allows us to write out a program without worrying about syntax. Sometimes the symbols and keywords used in pseudocode will vary depending on the convention you are using, but the idea is that pseudocode should be easy to humans to read.
3.17.1. While loops#
We write while
loops using pseudocode as follows
WHILE condition is true
process
ENDWHILE
Here is an example
Python code
i = 0
while i < 3:
print(i)
i = i + 1 # increment i by 1
Pseudocode
i = 0
WHILE i < 3
Display i
i = i + 1
ENDWHILE
Here is another example
Python code
colour = input("Enter a colour: ")
while colour != "red":
print("That is not red!")
colour = input("Enter a colour: ")
Pseudocode
Display 'Enter a colour: '
Get colour
WHILE guess <> 'red'
Display 'That is not red!'
Display 'Enter a colour: '
Get colour
ENDWHILE
Note that <>
can be used to indicate not equal and is equivalent to using
!=
.
3.17.2. For loops#
We write for
loops using pseudocode as follows
FOR variable = start TO finish STEP increment
process
NEXT variable
Here is an example
Python code
for i in range(3):
print(i)
Pseudocode
FOR i = 0 TO 2 STEP 1
Display i
NEXT i
Not that for For
loops in pseudocode, the loop includes the start and end
values. This means that FOR i = 0 TO 2 STEP 1
means that i
takes the
values 0, 1 and 2.
Question 1
Which of the following programs corresponds to the pseudocode shown below?
FOR i = 10 TO 0 STEP -2
Display i
NEXT i
for i in range(10): print(i)
for i in range(10, 0): print(i)
for i in range(10, 0, -2): print(i)
for i in range(10, -1, -2): print(i)
Solution
D.
In pseudocode, the start and end values are inclusive. This means i
will take the values 10, 8, 6, 4, 2 and 0 for FOR i = 10 TO 0 STEP -2
. The corresponding for
loop in Python requires i
starting at 0, going to but not including -1 in steps of 2 (up to and including -2) also works. The other options in the for
loops presented here only go up to but do not include 0.
Question 2
Construct the pseudocode that corresponds to the following Python program.
colours = ['red', 'yellow', 'pink', 'green', 'purple', 'orange', 'blue']
i = 0
while i < len(colours):
print(colours[i])
i = i + 1
Solution
Solution is locked
Question 3
Which of the following best describes the purpose of the pseudocode algorithm given below.
Create list a
Create list b
Create emplty list c
FOR i = 0 TO Length(a) - 1 STEP 1
Append a[i] to c
NEXT i
FOR i = 0 TO Length(b) -1 STEP 1
Append b[i] to c
NEXT i
Display c
It adds all the elements in the list
a
to the corresponding elements in the listb
and stores the result inc
. E.g. if a is the list[7, 1, 3]
andb
is the list[-1, 5, 8]
thenc
will be the list[6, 6, 11]
.It adds all the elements in the lists
a
andb
and stores the result inc
. E.g. ifa
is the list[7, 1, 3]
andb
is the list[-1, 5, 8]
thenc
will be the integer23
.It concatenates (joins) the lists
a
andb
and stores the result inc
. E.g. ifa
is the list[7, 1, 3]
andb
is the list[-1, 5, 8]
thenc
will be the list[7, 1, 3, -1, 5, 8]
.
Solution
Solution is locked