1. for

Executes a procedure repeatedly with a numeric control variable.

1.1. Syntax

initial increment limit proc for → –

1.2. Stack Effects

Table 1. Before
Level Object

3

initial (starting value)

2

increment (step value)

1

limit (ending value)

0

proc (procedure to execute)

Table 2. After
Level Object

(varies)

Results from procedure executions

1.3. Description

for executes proc repeatedly, passing it a sequence of values from initial by steps of increment to limit.

The operator maintains an internal control variable: 1. Sets control variable to initial 2. Before each iteration: compares with limit 3. If not exceeded: pushes control variable, executes proc, adds increment 4. Repeats until termination condition met

Termination: * If increment > 0: stops when control variable > limit * If increment < 0: stops when control variable < limit * If initial meets condition: proc never executes

1.4. PostScript Level

Level 1 and later

1.5. Examples

Basic counting
1 1 5 { = } for
% Prints: 1 2 3 4 5
Summing values
0  % Accumulator
1 1 4 { add } for
% Result: 10 (sum of 1+2+3+4)
Counting down
3 -.5 1 { = } for
% Prints: 3.0 2.5 2.0 1.5 1.0
Building arrays
5 array
0 1 4 {
  2 copy exch dup 10 mul put
} for
% Result: [0 10 20 30 40]

1.6. Common Use Cases

1.6.1. Array Population

/squares 10 array def
0 1 9 {
  dup dup mul
  squares 3 1 roll put
} for

1.6.2. Grid Generation

0 10 100 {  % x from 0 to 100 by 10
  0 10 100 {  % y from 0 to 100 by 10
    % Draw grid point at (x, y)
    2 copy moveto
    2 0 rlineto stroke
  } for
  pop
} for

1.6.3. Factorial Calculation

/factorial {  % n => n!
  1 exch 1 exch { mul } for
} def

5 factorial  % Returns 120

1.7. Common Pitfalls

Real Number Precision - Using reals for control values can cause precision errors.
0 0.1 1 { } for  % May not reach exactly 1.0
% Use integers when possible
Stack Accumulation - If proc doesn’t consume the control variable, values accumulate.
1 1 5 { } for
% Stack: 1 2 3 4 5 (all values left!)
Increment Sign Matters - Positive increment counts up, negative counts down.
5 1 1 { } for  % Never executes (5 > 1 already)
5 -1 1 { } for % Executes: 5, 4, 3, 2, 1
Early Exit - Use exit to terminate loop early.

1.8. Error Conditions

Error Condition

[stackoverflow]

Too many iterations or proc pushes too much

[stackunderflow]

Fewer than 4 operands on stack

[typecheck]

Wrong operand types

1.9. Implementation Notes

  • Control variable is internal (not on stack)

  • Very efficient for numeric sequences

  • Works with both integers and reals

  • Termination tested before each iteration

1.10. Comparison with Other Loops

Loop Best For Control Variable

for

Numeric sequences

Yes

repeat

Fixed count, no variable needed

No

loop

Indefinite/conditional termination

No

forall

Collection iteration

Yes (element)

1.11. Advanced Example

Matrix operations
/multiplyMatrices {  % matrix1 matrix2 => result
  [
    0 1 5 {  % For each result element
      /i exch def
      matrix1 i get
      matrix2 i get mul
    } for
  ]
} def

1.12. See Also


Back to top

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