1. Control Flow

Control flow operations enable conditional execution, loops, and program flow control in PostScript. These commands provide the foundation for decision-making and repetitive operations.

2. Overview

PostScript control flow operates through:

  • Conditional execution: Execute code based on boolean conditions

  • Iteration: Repeat operations multiple times or over collections

  • Flow control: Exit loops early, handle errors, terminate execution

  • Dynamic execution: Execute objects programmatically

3. Command Categories

3.1. Conditional Execution

Command Description

if

Execute procedure if condition is true

ifelse

Execute one of two procedures based on condition

3.2. Iteration

Command Description

for

Loop with numeric control variable

repeat

Execute procedure a fixed number of times

loop

Execute procedure indefinitely until exit

forall

Iterate over array, string, or dictionary elements

3.3. Flow Control

Command Description

exit

Terminate innermost loop early

stop

Terminate stopped context

stopped

Execute object with error catching

quit

Terminate interpreter

3.4. Dynamic Execution

Command Description

exec

Execute an object

cvx

Convert to executable attribute

4. Common Patterns

4.1. Conditional Execution

% Simple conditional
x 0 gt {
  (positive) print
} if

% Two-way conditional
x 0 gt {
  (positive) print
} {
  (non-positive) print
} ifelse

4.2. Numeric Iteration

% Count from 1 to 10
1 1 10 {
  % Loop body, current value on stack
  =
} for

% Count down
10 -1 1 {
  % 10, 9, 8, ..., 1
} for

4.3. Collection Iteration

% Process array elements
[1 2 3 4 5] {
  10 mul  % Multiply each by 10
} forall

% Process string characters
(hello) {
  % Each character code pushed
  =
} forall

4.4. Error Handling

{
  % Code that might fail
  riskyOperation
} stopped {
  % Handle error
  (Operation failed) print
} if

5. Loop Types Comparison

Loop Type Use When Control Variable

for

Numeric sequence needed

Yes

repeat

Simple fixed repetition

No

loop

Indefinite looping

No

forall

Iterating collections

Yes (element)

6. Important Considerations

6.1. Boolean Context

Conditionals require boolean values:

true { } if      % Correct
0 { } if         % Error: typecheck (0 is not boolean)

6.2. Procedure Objects

Control flow operators require procedures (executable arrays):

{ code } if      % Correct: { } creates procedure
[code] if        % Wrong: [ ] creates literal array

6.3. Loop Termination

  • exit exits innermost loop only

  • stop exits stopped context

  • Infinite loops possible with loop

6.4. Stack Effects

Procedures may leave values on stack:

5 { 10 } repeat  % Leaves 10 10 10 10 10 on stack
5 { } repeat     % Leaves nothing

7. Common Pitfalls

Not C-Style Booleans - PostScript uses true/false, not 0/1 or nil/non-nil.
Procedures Not Blocks - Use { } not [ ] for code blocks.
Stack Accumulation - Loop bodies that don’t consume values will accumulate on stack.
Use exit Carefully - Only works within loop contexts (for, repeat, loop, forall).

8. See Also