Control Statements & Conditionals With Postscript
In this tutorial, we will calculate a harmonic series. A text editor will be used to write postscript (notepad is sufficient). GSview will also be used to view our files.

Redefining variables

Take a look at this simple program

/foo 10 def
/foo foo 1 add def

What does this code do?

The code increments foo by one.  This is similar to saying foo = foo +1; in a c-style language.  Here are some other ways of changing a variable:

            5 6 add

     /foo exch def %foo now contains 11

How does the above code work?  Make a drawing of the stack and it is quick clear what is going on.

     /foo foo 10 mul def % foo will now contain 110

That is simple enough but we did not touch on that in the first tutorials. 

Defining Procedures

Procedures are defined in much the same way as variables are defined.  The only difference is that the commands are enclosed in braces { }.  Here is the first function that we saw:

            /inch {72 mul} def

This code is quite unique.  The items enclosed in braces make up what is called a executable array.  By calling inch we execute the array that is associated with it.  Executable arrays are used in other places as well.  They are used to define the body of an if statement.  We will see examples of this next.

Here is an example of another procedure:

            /LM 72 def         %Left Margin

     /ypos 720 def     %current y position

     /lineheight 14 def     %height of a line

     /newline

     { ypos linheight sub

       /ypos exch def

       LM ypos moveto } def

Because of the descriptive names it is quite easy to say what this code does but without those names it could be very difficult to say what exactly it does.    The first few lines are some variables.  General naming conventions are used so values that are meant to be constants are in upper case while values that are meant to be changed are in lowercase. 

Conditional Statements - Remember that items enclosed in braces are executable arrays.

First thing is to cover the comparison operators.  Here they are:

Command Function

eq

Equal (==)

gt

Greater than (>)

ge

Greater than or equal (>=)

lt

Less than (<)

le

Less than or equal (<=)

ne

Not equal (!=)

These operators work just like most others.  Try a few in Ghostscript and youíll see that they consume the two numbers being compared and then push a true or false on the stack.

            5 6 eq        % stack will have a false on the top

Now that we can compare some things lets learn how to make an if statement.  An if statement has this form:

            bool {op1} if

The bool is a true or a false that can be put there by a comparison operator.  Next is an executable array which is the body of the if statement.  Next is the if keyword which will execute the statement if the bool is true.

Ex:

            /max {

          /num1 exch def

          /num2 exch def

          num1 num2 gt

          {num1} if

          num1 num2 lt

          {num2} if

     } def

This code will compute the max and needs two numbers put on the stack before called.  It will put nothing on the stack if they are equal.

The ifelse operator is similar to the if statement and has this form:

            bool {op1} {op2} ifelse

It will execute op1 if the bool is true and op2 otherwise.

Loops

There are three loops in postscript.  The first is the repeat loop.  The form of this loop is:

            num {op} repeat

This will do op num times.

Then there is the for loop with the form:

            Start inc end {op} for

The final loop is the loop with the form:

            {op} loop

This loop will need to have an exit statement inside of it to terminate if not then you have an infinite loop (the best kind).

Here is an example of a repeat loop used in a procedure called harmonic:

/harmonic

{    /num 0 def

           /current 1 def

          {/num 1 current div num add def

          /current current 1 add def

      } repeat

      num

} def

The requirements for this function the number to go to on the top of the stack.  Donít put too large of a number on the stack because after about 10 million it could take a minute or two (not tested past 10 million).

Homework:

1 - Rewrite the harmonic procedure and use a for loop instead of a repeat loop.
2 Ė Write a procedure that produces the first n Fibonacci numbers where n is the top number on the stack.

The PostScript Tutorial Concieved and Created by Dann Ormond & Will Munn. Inspired by Mike Grady PhD. Website Design by NiftySites.com.