1. ifelse

Conditionally executes one of two procedures based on a boolean value.

1.1. Syntax

bool proc1 proc2 ifelse → –

1.2. Stack Effects

Table 1. Before
Level Object

2

bool (boolean condition)

1

proc1 (procedure if true)

0

proc2 (procedure if false)

Table 2. After
Level Object

(varies)

Results from executed procedure

1.3. Description

ifelse removes all three operands from the stack, then executes proc1 if bool is true, or proc2 if bool is false.

The ifelse operator itself pushes no results, but the executed procedure may push results.

1.4. PostScript Level

Level 1 and later

1.5. Examples

Basic two-way branch
4 3 lt
  { (4 < 3: true) }
  { (4 < 3: false) }
ifelse
% Prints: 4 < 3: false
Returning different values
x 0 ge
  { x }
  { x neg }
ifelse
% Returns absolute value
Nested conditionals
x 0 gt {
  (positive)
} {
  x 0 lt {
    (negative)
  } {
    (zero)
  } ifelse
} ifelse

1.6. Common Use Cases

1.6.1. Value Selection

/max {  % num1 num2 => max
  2 copy gt { exch } if pop
} def

/min {  % num1 num2 => min
  2 copy lt { exch } if pop
} def

1.6.2. Conditional Computation

isColor {
  currentrgbcolor  % Get RGB
} {
  currentgray dup dup  % Convert gray to RGB
} ifelse

1.6.3. Default Values

/getValue {  % dict key defaultValue => value
  3 1 roll
  2 copy known {
    get exch pop
  } {
    pop pop
  } ifelse
} def

1.7. Common Pitfalls

Both Procedures Required - Unlike some languages, you cannot omit the false branch.
condition { true-code } ifelse  % Error: stackunderflow
condition { true-code } { } ifelse  % Correct (empty false branch)
Stack Balance - Both procedures should leave the stack in the same state.
% Bad: different stack effects
x 0 gt { x x mul } { } ifelse  % Unbalanced

% Good: both push one value
x 0 gt { x x mul } { 0 } ifelse
Use for Single Branch - If you only need one branch, if is simpler.

1.8. Error Conditions

Error Condition

[stackunderflow]

Fewer than 3 operands on stack

[typecheck]

First operand not boolean, or second/third not procedures

1.9. Implementation Notes

  • Only the selected procedure is executed

  • No overhead for the non-executed branch

  • Procedures execute in current context

  • Very fast conditional evaluation

1.10. Pattern: Multi-Way Branch

For multiple conditions, nest ifelse or use dictionary dispatch:

% Nested ifelse
grade 90 ge {
  (A)
} {
  grade 80 ge {
    (B)
  } {
    grade 70 ge {
      (C)
    } {
      (F)
    } ifelse
  } ifelse
} ifelse

% Dictionary dispatch (often cleaner)
5 dict begin
  /red { 1 0 0 setrgbcolor } def
  /green { 0 1 0 setrgbcolor } def
  /blue { 0 0 1 setrgbcolor } def
  colorName load exec
end

1.11. See Also

  • if - Single-branch conditional

  • loop - Indefinite repetition

  • repeat - Fixed repetition

  • 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.