- The idea of having "standard" pseudocode is an oxymoron, so I'm giving you what the textbook seems to prefer
- There are other attempts at standards. For instance this LaTeX package.
JOES_VARIABLE <- 35
- Declare constants at start
- Note we use '<-' rather than '='
- Standard style seems to be to use capitals and '_' not CamelCase
Python:
joes_variable = 35- Use '=' for assignment and '==' for comparison
- Python style is to use lower case for Variables
- Objects get an upper case first letter in the manner of a proper noun
- CamelCase is a Java thing
OUTPUT "Something to display on screen"
- Use double quotes
- "PRINT" is a BASIC command, so is probably acceptable too
Python:
print( 'Something to display on screen' )- Python style is to use 'single quotes' but "double quotes work fine too"
- Old-style Python didn't treat print as a function so brackets weren't necessary
- You can format output nicely using "{0.%5f}" style notation. Take a look at this.
// First a prompt //
OUTPUT "Say Something"
// Then the actual input //
INPUT CHARLIES_VARIABLE
- Probably best separate the prompt (which is output) from the input itself
- There isn't a standard way to write a comment in pseudocode
Python:
charlies_variable = input( 'Say something: ' )- Note that the
inputfunction takes a prompt as input. - It returns a string as output.
FOR ... NEXT
FOR INDEX = 1 TO 20
OUTPUT INDEX
NEXT INDEX
- You can have a
STEPclause at the end of theFOR ... TOline to have jumps of 5 at a time or whatever. - This is a standard "BASIC" loop.
Python:
for i in range( 1 , 21 ):
print( i )- No NEXT because of indentation.
range( 1 , 20 , 5 )will give you jumps of size 5.
WHILE ... END WHILE
i <- 1
WHILE i < 21
OUTPUT i
i <- i + 1
END WHILE
Python:
i = 1
while i < 20 :
print ( i )
i = i + 1- Indentation tells us the end of the loop so no
ENDWHILE. - You can leave the loop at any time using a
BREAK. See below...
REPEAT ... UNTIL
i <- 1
REPEAT
OUTPUT i
i <- i + 1
UNTIL i = 20
Python:
- Doesn't exist.
- Use
whileinstead and consider using abreakif it helps with structure:
i = 1
## Standard trick for an infinite loop in Python:
while True:
print( i )
i = i + 1
if i == 20:
## Next line will leave the loop
break- Using too many
breaks can be messy so use sparingly.
INPUT SOMETHING
IF SOMETHING = 20 THEN OUTPUT "You typed 20"
- Since "<-" is used for assignment, "=" is not ambiguous in pseudocode.
- If the
THENblock is long then use multiple lines and finish with anEND IF. ELSEblocks can be added as required (and finish with anEND IF).
Python:
something = input()
if something == 20:
print( 'You typed 20' )- No
THENbecause of the colon. - No
END IFbecause of indentation. - Additional
elif ...:andelse:clauses can be added.
In Python we use lists most of the time.
- A list's size does not need to be declared but can be intialized as empty.
- Lists are specifically 1d, if you want more then either a list of lists or use something like
import numpy.
# Empty list
my_list = []
# Adding to end
my_list.append( 'a thing' )
# Defining list with entries
another_list = [ 'Apple' , 'Orange' ]
# Combining lists
my_list = my_list + another_list
# First item in list
print( my_list[ 0 ] )
# Last item in list
print( my_list[ -1 ] )
# Delete item from list
del my_list[ 0 ]In Pseudocode we tend to talk about arrays rather than lists
// DECLARE ARRAYS
DECLARE days : ARRAY [1:20] OF STRING
DECLARE bus_names: ARRAY [1:6] OF STRING
// DECLARE AN ARRAY OF ARRAYS
DECLARE bus_times: ARRAY[1:6] OF ARRAYS [1:20] OF INTEGER
days <- [“Mon1”, “Tues1”, … ]
bus_names <- [“Bus A”, “Bus B”, …]
bus_times[i][j] = late_val