Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Reid G.C.Thinking in PostScript.1990.pdf
Скачиваний:
17
Добавлен:
23.08.2013
Размер:
846.44 Кб
Скачать

you’ll see later in this chapter). For example, a routine to draw a polygon with hundreds of sides could benefit from being put into a loop.

LOOP BASICS

There are several different looping operators in the PostScript language (see Table 8.1). Each of them has a particular purpose and a style of use that is worth considering.

Table 8.1: Looping Operators

Arguments Operator

Action

exit –

jump out of loop body immediately

pattern proc buffer

filenameforall

execute proc for each file that

 

 

matches pattern string

start index end proc

for –

execute proc as many times as

 

 

(end-start) / index dictates.

composite_obj proc forall –

execute proc for each element of

 

 

composite_obj

proc loop –

execute proc indefinitely

count proc repeat

execute proc exactly count times

If you want to execute a sequence of instructions a precise number of times, you should use either the repeat operator or the for operator. Use repeat if you don’t need to use the loop index within the body of the loop (as discussed in the next section). If you want to keep executing a loop until some condition is met, you should use the loop operator, and use exit to pop out of the loop when the condition is satisfied. If you want to execute some instructions on each element of an array, a string, or a dictionary, you can use the forall operator, which supplies each element of a composite object to the loop as it is executing. This is useful for searching through arrays and sometimes for processing each character in a string, although it is a fairly labor-intensive operation compared to other string operators.

To construct a simple program loop, first start with a template for the loop body and make sure to supply the correct arguments for the looping operator you have chosen. Example 8.1 shows a simple repeat loop that draws an octagon; its result is displayed in Figure 8.1.

94

Chapter 8: USING LOOPING CONSTRUCTS