if and ifelse
PostScript uses if and ifelse for conditional logic. These operators expect a boolean value and one or two procedures on the stack.
if Example:
% Check if a number is greater than 50
/num 65 def
num 50 gt { (Number is greater than 50) show } ifIn this example, if num is greater than 50, the message is shown.
ifelse Example:
% Check if a number is even or odd
/num 42 def
num 2 mod 0 eq
{ (Number is even) show }
{ (Number is odd) show }
ifelseThis script checks whether num is even or odd, displaying an appropriate message.
PostScript provides several looping constructs: loop, repeat, for, and forall. Each serves different purposes and is used based on the context.
loop
This is an infinite loop, only exiting upon a conditional break.
% Print numbers from 1 to 10
/counter 1 def
{
counter 10 lt
{
counter pstack
/counter counter 1 add def
}{
exit
} ifelse
} loopThe exit operator is used to break out of the loop.
repeat
Executes a block of code a specific number of times.
% Print "Hello" 5 times
5 { (Hello\n) print } repeatfor
Iterates over a range of numbers, commonly used for index-based operations.
% Print numbers 0 to 9
0 1 9 {
pstack
} forThis script iterates from 0 to 9, printing each number.
forall
Iterates over elements in an array or dictionary.
% Print all elements in an array
[10 20 30 40 50] {
pstack
} forallIf used with a dictionary, forall passes each key-value pair to the block:
<< /a 1 /b 2 >> {
exch pstack % First pop value, then key
pstack
} forallPostScript handles runtime errors via the stopped operator, which can catch exceptions thrown by operations inside a procedure.
Example:
{
/num 0 def
10 num div
} stopped {
(An error occurred.) print
} ifIn this example, division by zero would normally cause an error, but stopped catches it and prints an error message instead.