1. repeat

Executes a procedure a specified number of times.

1.1. Syntax

int proc repeat → –

1.2. Stack Effects

Table 1. Before
Level Object

1

int (non-negative integer)

0

proc (procedure to execute)

Table 2. After
Level Object

(varies)

Results from procedure executions

1.3. Description

repeat executes proc exactly int times, where int must be a non-negative integer.

The operator removes both operands before executing proc for the first time. If int is 0, proc is not executed at all.

If proc executes exit, the loop terminates prematurely.

1.4. PostScript Level

Level 1 and later

1.5. Examples

Simple repetition
4 { (abc) } repeat
% Stack: (abc) (abc) (abc) (abc)
With side effects
10 { (.) print } repeat
% Prints: ..........
Zero repetitions
mark 0 { (won't happen) } repeat
% Stack: mark (procedure never executed)
Removing values
1 2 3 4
3 { pop } repeat
% Stack: 1 (removed top 3 values)

1.6. Common Use Cases

1.6.1. Simple Drawing

5 {
  0 0 moveto
  72 0 rlineto
  stroke
  0 10 translate
} repeat

1.6.2. Buffer Initialization

100 { 0 } repeat
100 array astore
% Array of 100 zeros

1.6.3. Pattern Generation

10 {
  gsave
    % Draw pattern element
    currentpoint translate
    drawElement
  grestore
  spacing 0 translate
} repeat

1.7. Common Pitfalls

Stack Accumulation - If proc pushes values without consuming them, they accumulate.
10 { 42 } repeat
% Stack has 10 copies of 42!
No Control Variable - Unlike for, repeat doesn’t provide iteration index.
5 { % No way to know which iteration } repeat
Use for if Index Needed - If you need to know the iteration number, use for instead:
0 1 4 { % Iteration index on stack } for

1.8. Error Conditions

Error Condition

[rangecheck]

int is negative

[stackunderflow]

Fewer than 2 operands on stack

[typecheck]

int not integer, or proc not procedure

1.9. Implementation Notes

  • Efficient for simple fixed repetition

  • No control variable overhead

  • Very fast for small counts

  • Procedure executed in current context

1.10. Comparison

Use Case Operator Reason

Need iteration index

for

Provides control variable

Fixed count, no index

repeat

Simpler, faster

Conditional termination

loop

Exit when condition met

Iterate collection

forall

Works with arrays/strings

1.11. See Also

  • for - Loop with control variable

  • loop - Indefinite loop

  • exit - Exit loop early

  • forall - Iterate collections


Back to top

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