1. loop

Repeatedly executes a procedure indefinitely until exit is called.

1.1. Syntax

proc loop → –

1.2. Stack Effects

Table 1. Before
Level Object

0

proc (procedure to execute)

Table 2. After
Level Object

(varies)

Results from procedure executions

1.3. Description

loop repeatedly executes proc until proc executes exit, at which point interpretation resumes after the loop.

Control also leaves proc if stop is executed. If proc never executes exit or stop, an infinite loop results.

1.4. PostScript Level

Level 1 and later

1.5. Examples

Loop with exit condition
0 {
  dup 10 ge { exit } if
  dup =
  1 add
} loop
pop
% Prints: 0 1 2 3 4 5 6 7 8 9
Reading until condition
file {
  read not { exit } if
  % Process character
} loop
Infinite server loop
{
  % Wait for request
  % Process request
  % Send response
} loop

1.6. Common Use Cases

1.6.1. Event Loop

{
  waitForEvent
  dup /quit eq { exit } if
  processEvent
} loop

1.6.2. File Processing

{
  file readline not { exit } if
  processLine
} loop

1.6.3. Search with Early Exit

/found false def
0 {
  array exch get
  dup target eq {
    /found true def
    exit
  } if
  pop
} loop

1.7. Common Pitfalls

Infinite Loops - Without exit or stop, loop runs forever.
{ (infinite) print } loop  % Never stops!
Stack Growth - Ensure loop body balances stack or it will overflow.
{ 1 } loop  % Stack overflow eventually
Use External Interrupt - Infinite loops can be broken via implementation-specific external interrupts (e.g., Ctrl-C).

1.8. Error Conditions

Error Condition

[stackunderflow]

No operand on stack

[typecheck]

Operand is not a procedure

1.9. Implementation Notes

  • No iteration limit checked

  • Very lightweight (minimal overhead per iteration)

  • Procedure must contain exit logic

  • Common pattern for server loops

1.10. Typical Pattern

{
  % 1. Get/wait for input
  getData

  % 2. Check termination
  dup shouldExit { pop exit } if

  % 3. Process
  process

  % 4. Loop continues
} loop

1.11. See Also

  • exit - Exit loop

  • for - Numeric loop

  • repeat - Fixed repetition

  • stop - Exit stopped context

  • forall - Iterate collections


Back to top

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