CSE 326, Spring 1998: Pseudocode Manual

This document contain the pseudocode syntax that we will be using in homeworks. This document will change over time as needed. 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:
    x : integer
    
    p : node pointer
    
  2. 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:
    concatenate(p : list pointer, q : list pointer) : list pointer
    
    find(x : integer,  p reference tree pointer) : tree pointer
    
  3. Sequences of statements: Separate by with semicolons and group using curly brackets, { }, when needed.
  4. Conditionals: if "condition" then "statement" or if "condition" then "statement1" else "statement2" .
  5. Case statement: the case statement is quite handy. The case can follow the following syntax:

    case 
    condition1 : statement1 ;
    condition2 : statement2 ;
    .
    .
    endcase

  6. While loop: while "condition" do "statement".
  7. Repeat loop: repeat "statement" until "condition".
  8. 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".
  9. 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.
  10. 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.
  11. 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.
  12. Assignment: use :=
  13. Boolean operators: use and, or, not, implies
  14. Equals operator: use = for any simple objects such as pointers and integers.
  15. Arithmetic operators: use +, -, *, ^, /, mod for integer addition, subtraction, multiplication, exponetiation, division (yields the quotient), and division (yields the remainder).
  16. Feel free to use a basic case control structure. I recommend "case" followed by a sequence of "condition": "statement". The semantics is that the statement following the first true condition is executed.
ladner@cs.washington.edu (last update 3/30/98)