1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.0.3, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!-- This manual documents GNU troff version 1.23.0.
Copyright 1994-2023 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with no
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
copy of the license is included in the section entitled "GNU Free
Documentation License". -->
<title>while (The GNU Troff Manual)</title>
<meta name="description" content="while (The GNU Troff Manual)">
<meta name="keywords" content="while (The GNU Troff Manual)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Request-Index.html" rel="index" title="Request Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Conditionals-and-Loops.html" rel="up" title="Conditionals and Loops">
<link href="if_002delse.html" rel="prev" title="if-else">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
-->
</style>
</head>
<body lang="en">
<div class="subsection-level-extent" id="while">
<div class="nav-panel">
<p>
Previous: <a href="if_002delse.html" accesskey="p" rel="prev">if-else</a>, Up: <a href="Conditionals-and-Loops.html" accesskey="u" rel="up">Conditionals and Loops</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Request-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsection" id="while-1">5.23.5 while</h4>
<a class="index-entry-id" id="index-while"></a>
<p><code class="code">groff</code> provides a looping construct: the <code class="code">while</code> request.
Its syntax matches the <code class="code">if</code> request.
</p>
<a class="index-entry-id" id="index-body_002c-of-a-while-request"></a>
<dl class="first-deffn">
<dt class="deffn" id="index-_002ewhile"><span class="category-def">Request: </span><span><strong class="def-name"><code class="t">.while</code></strong> <var class="def-var-arguments">cond-expr anything</var><a class="copiable-link" href='#index-_002ewhile'> ¶</a></span></dt>
<dd><a class="index-entry-id" id="index-while-1"></a>
<p>Evaluate the conditional expression <var class="var">cond-expr</var>, and repeatedly
execute <var class="var">anything</var> unless and until <var class="var">cond-expr</var> evaluates false.
<var class="var">anything</var>, which is often a conditional block, is referred to as
the <code class="code">while</code> request’s <em class="dfn">body</em>.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">.nr a 0 1
.while (\na < 9) \{\
\n+a,
.\}
\n+a
⇒ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</pre></div></div>
<a class="index-entry-id" id="index-de-request_002c-and-while"></a>
<p>GNU <code class="code">troff</code> treats the body of a <code class="code">while</code> request similarly to
that of a <code class="code">de</code> request (albeit one not read in copy
mode<a class="footnote" id="DOCF94" href="groff.html_fot.html#FOOT94"><sup>94</sup></a>), but stores it under an internal name
and deletes it when the loop finishes. The operation of a macro
containing a <code class="code">while</code> request can slow significantly if the
<code class="code">while</code> body is large. Each time the macro is executed, the
<code class="code">while</code> body is parsed and stored again.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">.de xxx
. nr num 10
. while (\\n[num] > 0) \{\
. \" many lines of code
. nr num -1
. \}
..
</pre></div></div>
<a class="index-entry-id" id="index-recursive-macros"></a>
<a class="index-entry-id" id="index-macros_002c-recursive"></a>
<p>An often better solution—and one that is more portable, since
<abbr class="acronym">AT&T</abbr> <code class="code">troff</code> lacked the <code class="code">while</code> request—is to
instead write a recursive macro. It will be parsed only
once.<a class="footnote" id="DOCF95" href="groff.html_fot.html#FOOT95"><sup>95</sup></a>
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">.de yyy
. if (\\n[num] > 0) \{\
. \" many lines of code
. nr num -1
. yyy
. \}
..
.
.de xxx
. nr num 10
. yyy
..
</pre></div></div>
<p>To prevent infinite loops, the default number of available recursion
levels is 1,000 or somewhat less.<a class="footnote" id="DOCF96" href="groff.html_fot.html#FOOT96"><sup>96</sup></a> You can
disable this protective measure, or raise the limit, by setting the
<code class="code">slimit</code> register. See <a class="xref" href="Debugging.html">Debugging</a>.
</p>
<p>As noted above, if a <code class="code">while</code> body begins with a conditional block,
its closing brace must end an input line.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">.if 1 \{\
. nr a 0 1
. while (\n[a] < 10) \{\
. nop \n+[a]
.\}\}
error→ unbalanced brace escape sequences
</pre></div></div>
</dd></dl>
<dl class="first-deffn">
<dt class="deffn" id="index-_002ebreak"><span class="category-def">Request: </span><span><strong class="def-name"><code class="t">.break</code></strong><a class="copiable-link" href='#index-_002ebreak'> ¶</a></span></dt>
<dd><a class="index-entry-id" id="index-break-2"></a>
<a class="index-entry-id" id="index-while-request_002c-confusing-with-br"></a>
<a class="index-entry-id" id="index-break-request_002c-in-a-while-loop"></a>
<a class="index-entry-id" id="index-continue-request_002c-in-a-while-loop"></a>
<p>Exit a <code class="code">while</code> loop. Do not confuse this request with a
typographical break or the <code class="code">br</code> request.
</p></dd></dl>
<dl class="first-deffn">
<dt class="deffn" id="index-_002econtinue"><span class="category-def">Request: </span><span><strong class="def-name"><code class="t">.continue</code></strong><a class="copiable-link" href='#index-_002econtinue'> ¶</a></span></dt>
<dd><a class="index-entry-id" id="index-continue"></a>
<p>Skip the remainder of a <code class="code">while</code> loop’s body, immediately starting
the next iteration.
</p></dd></dl>
</div>
<hr>
<div class="nav-panel">
<p>
Previous: <a href="if_002delse.html">if-else</a>, Up: <a href="Conditionals-and-Loops.html">Conditionals and Loops</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Request-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|