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, 10Some remarks.
- The body of a
whilerequest is treated like the body of aderequest:gtrofftemporarily stores it in a macro which is deleted after the loop has been exited. It can considerably slow down a macro if the body of thewhilerequest (within the macro) is large. Each time the macro is executed, thewhilebody 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
troffdoesn't have thewhilerequest) is to use a recursive macro instead which 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).- The closing brace of a
whilebody must end a line..if 1 \{\ . nr a 0 1 . while (\n[a] < 10) \{\ . nop \n+[a] .\}\} ⇒ unbalanced \{ \}
Break out of a
whileloop. Be sure not to confuse this with thebrrequest (causing a line break).
Finish the current iteration of a
whileloop, immediately restarting the next iteration.
See Expressions.