CSE 373, Spring 2004: Pseudocode Manual

This document contains the pseudocode syntax that we are using in the lecture slides and some of the homeworks. This document may change over time as needed. It is perfectly ok for pseudocode to use English expressions or whatever is needed to make the code easier to understand. The basic rules are:
  1. Try to declare all variables. A simple convention is to give the name of the variable followed by a colon followed by its type. Examples:
  2. x : integer
    
    p : node pointer
  3. Functions: Generally, we will assume that functions are call by value. If a parameter is intended to be call by reference then that should be indicated. The arguments should have their types indicated and be separated by commas. The return value if any should be indicated. Examples:
  4. concatenate(p, q : list pointer) : list pointer
    
    find(x : integer,  p reference tree pointer) : tree pointer
    sum(v[] : integer array; num : integer) : integer
  5. Sequences of statements: Separate by with semicolons and group using curly brackets, { }, when needed.
  6. Conditionals: if "condition" then "statement" or if "condition" then "statement1" else "statement2" .
  7. Case statement: the case statement is quite handy. The case can follow the following syntax:
  8. case 
    
      condition1 : statement1 ; 
    
      condition2 : statement2 ; 
    
      . 
    
      . 
    
    endcase  
    
    
  9. While loop: while "condition" do "statement".
  10. Repeat loop: repeat "statement" until "condition".
  11. for loop: for i = "start index" to "end index" do "statement". Occationally, there may be a need for stepping by more than 1, in which case, we have for i = "start index" to "end index" by "step" do "statement".
  12. Records and pointers: If p is a pointer to a record with field x, the field can be accessed using the notation p.x. Generally it will be clear from context what are pointers and what are not.
  13. Memory management: We assume a simple memory management scheme where an object of any type can be obtained by using the term "new", usually a pointer is returned. Don't bother to destroy objects unless it is required. We'll just assume an infinite amount of new memory.
  14. Arrays: to obtain the i-th item from array A, simply write A[i]. An array used as function parameter can be assumed to be by reference. Declaring an array can be done by specifying its range. For example:
  15. A[1..N] : integer array
  16. Assignment: use := (this is nicer than = but we won't penalize if you use = and the context is evident )
  17. Boolean operators: use and, or, not, implies (or their mathematical symbols)
  18. Equals operator: use = for any simple objects such as pointers and integers (you can use = = but then be sure to be consistent with the Assignment operator).
  19. Arithmetic operators: use +, -, *, ^, /, mod for integer addition, subtraction, multiplication, exponentiation, division (yields the quotient), and division (yields the remainder).
tanimoto@cs.washington.edu (last update 1/6/2003 by J.-L. Baer)