repeat、forが繰り返す条件を指定して手続きを呼ぶのに対し、loopは無限ループを作り実行中に条件を判定して脱出します。
GPL Ghostscript 9.06 (2012-08-08) Copyright (C) 2012 Artifex Software, Inc. All rights reserved. This software comes with NO WARRANTY: see the file PUBLIC for details. GS>/inc-x { x 1 add /x exch def } def % 広域変数 x に1を加算する手続き GS>/x 1 def % x の初期値に1を設定 GS>{x 10 le {x == inc-x} {exit} ifelse }loop % 外側の{}に囲まれた部分をループ。 1 % x が10以下のときはxを表示してxに1加算。 2 % そうでないときはループ脱出。 3 4 5 6 7 8 9 10
1から10までの合計を求めてみます。
GS>/inc-x { x 1 add /x exch def } def % 広域変数 x に1を加算する手続き GS>/x 1 def % x の初期値に1を設定 GS>/calc {total x add /total exch def } def % totalに xを加算し、その結果をtotalに設定 GS>/total 0 def % total を0に初期化 GS>{x 10 le {calc total == inc-x} {exit} ifelse }loop % 外側の{}に囲まれた部分をループ。 1 % x が10以下のときは calc の手続きを実行してtotalを表示。 3 % そうでないときはループ脱出。 6 10 15 21 28 36 45 55