1. if

Conditionally executes a procedure based on a boolean value.

1.1. Syntax

bool proc if → –

1.2. Stack Effects

Table 1. Before
Level Object

1

bool (boolean value)

0

proc (procedure to execute)

Table 2. After
Level Object

(varies)

Results from procedure (if executed)

1.3. Description

id removes both operands from the stack, then executes proc if bool is true. If bool is false, proc is not executed.

The if operator itself pushes no results, but proc may push results onto the stack.

1.4. PostScript Level

Level 1 and later

1.5. Examples

Simple conditional
3 4 lt { (3 is less than 4) } if
% Prints the string (condition is true)
Conditional definition
/DEBUG true def
DEBUG { (Debug mode enabled) print } if
No execution when false
false { (This won't print) } if
% Nothing happens

1.6. Common Use Cases

1.6.1. Validation

x 0 lt {
  /rangecheck cvx exec
} if

1.6.2. Optional Operations

/verbose where {
  pop verbose {
    (Processing...) print
  } if
} if

1.6.3. Conditional Setup

/isColor currentpagedevice /ProcessColorModel get
         /DeviceRGB eq def
isColor {
  % Setup for color output
  setupColorRendering
} if

1.7. Common Pitfalls

Requires Boolean - Operand must be true or false, not 0/1 or other values.
1 { code } if        % Error: typecheck
true { code } if     % Correct
Requires Procedure - Second operand must be executable array (procedure).
true [code] if       % Error: literal array
true { code } if     % Correct: executable array
Use ifelse for Two-Way - For true/false cases, ifelse is more convenient than nested if statements.

1.8. Error Conditions

Error Condition

[stackunderflow]

Fewer than 2 operands on stack

[typecheck]

First operand not boolean, or second not procedure

1.9. Implementation Notes

  • Very fast conditional evaluation

  • Procedure not executed if condition false (no overhead)

  • Procedure executed in current context (same dict stack, graphics state)

1.10. See Also

  • ifelse - Two-way conditional

  • loop - Indefinite repetition

  • stopped - Error catching

  • Comparison operators: eq, ne, lt, le, gt, ge

  • Boolean operators: and, or, not


Back to top

Copyright © 2025 Ribose. PostScript is a trademark of Adobe. Distributed under the MIT License.