Table of Contents
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
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
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
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
-
Dictionary Operations - For scoping with begin/end
-
Array Operations - For forall iteration
-
Error Handling (to be documented) - For comprehensive error management