Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Reid G.C.Thinking in PostScript.1990.pdf
Скачиваний:
17
Добавлен:
23.08.2013
Размер:
846.44 Кб
Скачать
% bool proc1 proc2 ifelse % look at the boolean
% show us which proc will execute % show the “true” procedure
% show the “false” procedure % now execute “ifelse” normally

off_bottom_of_page

% boolean

/regular_proc load

% “false” proc

(a random string)

 

/off_bottom_proc load

% “true” proc

exch pop exch

% get rid of random string

ifelse

 

Another interesting example that illustrates the fact that ifelse is just an ordinary operator is Example 7.11, which redefines the ifelse operator to get a little extra information while it is executing, for debugging purposes.

Example 7.11: Redefining ifelse for Debugging

% redefine “ifelse” to trace which clause is being executed: /orig_ifelse /ifelse load def

/ifelse { %def

2 index dup (condition: ) print == { %ifelse

1 index == }{ %else

dup == } orig_ifelse orig_ifelse

} bind def

NESTED CONDITIONALS AND ELSE CLAUSES

Fairly often, in a complicated program, you need to have conditionals that have more conditionals on each of the two result clauses. When these nest more than one level deep, it can become tricky to maintain them without leaving things on the operand stack or introducing bugs of some sort.

The first issue is simply balancing the curly braces correctly, so that there are no basic syntactic errors in the program. Using the template technique for this is a good approach, and one that can be expanded as necessary later (see Example 7.12).

Chapter 7: BUILDING CONDITIONAL STATEMENTS

87