Previous: if-else, Up: Conditionals and Loops [Contents][Index]
gtroff
provides a looping construct using the while
request, which is used much like the if
(and related) requests.
Evaluate the expression expr, and repeatedly execute anything (the remainder of the line) until expr evaluates to 0.
.nr a 0 1 .while (\na < 9) \{\ \n+a, .\} \n+a ⇒ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Some remarks.
while
request is treated like the body of a
de
request: gtroff
temporarily stores it in a macro that
is deleted after the loop has been exited. It can considerably slow
down a macro if the body of the while
request (within the macro)
is large. Each time the macro is executed, the while
body is
parsed and stored again as a temporary macro.
.de xxx . nr num 10 . while (\\n[num] > 0) \{\ . \" many lines of code . nr num -1 . \} ..
The traditional and ofter better solution (UNIX troff
doesn’t have the while
request) is to use a recursive macro
instead that is parsed only once during its definition.
.de yyy . if (\\n[num] > 0) \{\ . \" many lines of code . nr num -1 . yyy . \} .. . .de xxx . nr num 10 . yyy ..
Note that the number of available recursion levels is set to 1000
(this is a compile-time constant value of gtroff
).
while
body must end a line.
.if 1 \{\ . nr a 0 1 . while (\n[a] < 10) \{\ . nop \n+[a] .\}\} ⇒ unbalanced \{ \}
Break out of a while
loop. Be sure not to confuse this with the
br
request (causing a line break).
Finish the current iteration of a while
loop, immediately
restarting the next iteration.
See Expressions.
Previous: if-else, Up: Conditionals and Loops [Contents][Index]