| 1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
| 2 |
<html> |
| 3 |
<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ --> |
| 4 |
<head> |
| 5 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 6 |
<title>Termination Test Clauses (ANSI and GNU Common Lisp Document)</title> |
| 7 |
|
| 8 |
<meta name="description" content="Termination Test Clauses (ANSI and GNU Common Lisp Document)"> |
| 9 |
<meta name="keywords" content="Termination Test Clauses (ANSI and GNU Common Lisp Document)"> |
| 10 |
<meta name="resource-type" content="document"> |
| 11 |
<meta name="distribution" content="global"> |
| 12 |
<meta name="Generator" content="makeinfo"> |
| 13 |
<link href="index.html" rel="start" title="Top"> |
| 14 |
<link href="The-LOOP-Facility.html" rel="up" title="The LOOP Facility"> |
| 15 |
<link href="Examples-of-REPEAT-clause.html" rel="next" title="Examples of REPEAT clause"> |
| 16 |
<link href="Examples-of-SUM-clause.html" rel="prev" title="Examples of SUM clause"> |
| 17 |
<style type="text/css"> |
| 18 |
<!-- |
| 19 |
a.summary-letter {text-decoration: none} |
| 20 |
blockquote.indentedblock {margin-right: 0em} |
| 21 |
div.display {margin-left: 3.2em} |
| 22 |
div.example {margin-left: 3.2em} |
| 23 |
div.lisp {margin-left: 3.2em} |
| 24 |
kbd {font-style: oblique} |
| 25 |
pre.display {font-family: inherit} |
| 26 |
pre.format {font-family: inherit} |
| 27 |
pre.menu-comment {font-family: serif} |
| 28 |
pre.menu-preformatted {font-family: serif} |
| 29 |
span.nolinebreak {white-space: nowrap} |
| 30 |
span.roman {font-family: initial; font-weight: normal} |
| 31 |
span.sansserif {font-family: sans-serif; font-weight: normal} |
| 32 |
ul.no-bullet {list-style: none} |
| 33 |
--> |
| 34 |
</style> |
| 35 |
|
| 36 |
|
| 37 |
</head> |
| 38 |
|
| 39 |
<body lang="en"> |
| 40 |
<span id="Termination-Test-Clauses"></span><div class="header"> |
| 41 |
<p> |
| 42 |
Next: <a href="Unconditional-Execution-Clauses.html" accesskey="n" rel="next">Unconditional Execution Clauses</a>, Previous: <a href="Value-Accumulation-Clauses.html" accesskey="p" rel="prev">Value Accumulation Clauses</a>, Up: <a href="The-LOOP-Facility.html" accesskey="u" rel="up">The LOOP Facility</a> </p> |
| 43 |
</div> |
| 44 |
<hr> |
| 45 |
<span id="Termination-Test-Clauses-1"></span><h4 class="subsection">6.1.4 Termination Test Clauses</h4> |
| 46 |
|
| 47 |
<p>The <tt>repeat</tt> construct causes iteration to terminate after a |
| 48 |
specified number of times. |
| 49 |
The loop body executes <i>n</i> times, where <i>n</i> is the value |
| 50 |
of the expression <i>form</i>. The <i>form</i> argument is evaluated one time |
| 51 |
in the loop prologue. If the expression evaluates to 0 or |
| 52 |
to a negative <i>number</i>, the loop body is not evaluated. |
| 53 |
</p> |
| 54 |
<p>The constructs <tt>always</tt>, |
| 55 |
<tt>never</tt>, |
| 56 |
<tt>thereis</tt>, |
| 57 |
<tt>while</tt>, |
| 58 |
<tt>until</tt>, |
| 59 |
and the macro <b>loop-finish</b> |
| 60 |
allow conditional termination of iteration within |
| 61 |
a <b>loop</b>. |
| 62 |
</p> |
| 63 |
<p>The constructs <tt>always</tt>, <tt>never</tt>, and <tt>thereis</tt> provide |
| 64 |
specific values to be returned when a <b>loop</b> terminates. |
| 65 |
Using <tt>always</tt>, <tt>never</tt>, or <tt>thereis</tt> in a loop with |
| 66 |
value accumulation clauses that are not <tt>into</tt> causes |
| 67 |
an error of <i>type</i> <b>program-error</b> to be signaled (at macro expansion time). |
| 68 |
Since <tt>always</tt>, <tt>never</tt>, and <tt>thereis</tt> |
| 69 |
use |
| 70 |
</p> |
| 71 |
<p>the <b>return-from</b> <i>special operator</i> |
| 72 |
</p> |
| 73 |
<p>to terminate iteration, |
| 74 |
any <tt>finally</tt> clause that is supplied is not evaluated |
| 75 |
when exit occurs due to any of these constructs. |
| 76 |
In all other respects these |
| 77 |
constructs behave like the <tt>while</tt> and <tt>until</tt> constructs. |
| 78 |
</p> |
| 79 |
<p>The <tt>always</tt> construct takes one <i>form</i> and terminates the |
| 80 |
<b>loop</b> |
| 81 |
if the <i>form</i> ever evaluates to <b>nil</b>; in this case, it returns |
| 82 |
<b>nil</b>. Otherwise, it provides a default return value of <b>t</b>. |
| 83 |
If the value of the supplied <i>form</i> is never <b>nil</b>, some other construct |
| 84 |
can terminate the iteration. |
| 85 |
</p> |
| 86 |
<p>The <tt>never</tt> construct terminates iteration the first time that |
| 87 |
the value of the supplied <i>form</i> is <i>non-nil</i>; the <b>loop</b> returns |
| 88 |
<b>nil</b>. |
| 89 |
If the value of the supplied <i>form</i> is always <b>nil</b>, some other |
| 90 |
construct can terminate the iteration. |
| 91 |
Unless some other clause contributes |
| 92 |
a return value, the default value returned is <b>t</b>. |
| 93 |
</p> |
| 94 |
<p>The <tt>thereis</tt> construct terminates iteration the first time that the |
| 95 |
value of the supplied <i>form</i> is <i>non-nil</i>; the <b>loop</b> returns the |
| 96 |
value of the supplied <i>form</i>. |
| 97 |
If the value of the supplied <i>form</i> |
| 98 |
is always <b>nil</b>, some other |
| 99 |
construct can terminate the iteration. Unless some other clause contributes a |
| 100 |
return value, the default value returned is <b>nil</b>. |
| 101 |
</p> |
| 102 |
<p>There are two differences between the <tt>thereis</tt> and <tt>until</tt> |
| 103 |
constructs: |
| 104 |
</p> |
| 105 |
<dl compact="compact"> |
| 106 |
<dt><tt>*</tt></dt> |
| 107 |
<dd><p>The <tt>until</tt> construct does not return a value or |
| 108 |
<b>nil</b> based on the value of the supplied <i>form</i>. |
| 109 |
</p> |
| 110 |
</dd> |
| 111 |
<dt><tt>*</tt></dt> |
| 112 |
<dd><p>The <tt>until</tt> construct executes |
| 113 |
any <tt>finally</tt> clause. |
| 114 |
Since <tt>thereis</tt> uses |
| 115 |
</p> |
| 116 |
<p>the <b>return-from</b> <i>special operator</i> |
| 117 |
</p> |
| 118 |
<p>to terminate iteration, |
| 119 |
any <tt>finally</tt> clause that is supplied is not evaluated |
| 120 |
when exit occurs due to <tt>thereis</tt>. |
| 121 |
</p> |
| 122 |
</dd> |
| 123 |
</dl> |
| 124 |
|
| 125 |
<p>The <tt>while</tt> construct allows iteration to continue until the |
| 126 |
supplied <i>form</i> |
| 127 |
evaluates to <i>false</i>. The supplied <i>form</i> |
| 128 |
is reevaluated at the location of the <tt>while</tt> clause. |
| 129 |
</p> |
| 130 |
<p>The <tt>until</tt> construct is equivalent to |
| 131 |
<tt>while (not <i>form</i>)\dots</tt>. If the value of the |
| 132 |
supplied <i>form</i> is <i>non-nil</i>, iteration terminates. |
| 133 |
</p> |
| 134 |
<p>Termination-test control constructs can be used anywhere within the loop body. |
| 135 |
The termination tests are used in the order in which they appear. |
| 136 |
If an <tt>until</tt> or <tt>while</tt> clause causes |
| 137 |
termination, any clauses that precede it in the source |
| 138 |
are still evaluated. |
| 139 |
If the <tt>until</tt> and <tt>while</tt> constructs cause termination, |
| 140 |
control is passed to the loop epilogue, where any <tt>finally</tt> |
| 141 |
clauses will be executed. |
| 142 |
</p> |
| 143 |
<p>There are two differences between the <tt>never</tt> and <tt>until</tt> |
| 144 |
constructs: |
| 145 |
</p> |
| 146 |
<dl compact="compact"> |
| 147 |
<dt><tt>*</tt></dt> |
| 148 |
<dd><p>The <tt>until</tt> construct does not return |
| 149 |
<b>t</b> or <b>nil</b> based on the value of the supplied <i>form</i>. |
| 150 |
</p> |
| 151 |
</dd> |
| 152 |
<dt><tt>*</tt></dt> |
| 153 |
<dd><p>The <tt>until</tt> construct |
| 154 |
does not bypass any <tt>finally</tt> clauses. |
| 155 |
Since <tt>never</tt> uses |
| 156 |
</p> |
| 157 |
<p>the <b>return-from</b> <i>special operator</i> |
| 158 |
</p> |
| 159 |
<p>to terminate iteration, |
| 160 |
any <tt>finally</tt> clause that is supplied is not evaluated |
| 161 |
when exit occurs due to <tt>never</tt>. |
| 162 |
</p></dd> |
| 163 |
</dl> |
| 164 |
|
| 165 |
<p>In most cases it is not necessary to use <b>loop-finish</b> |
| 166 |
because other loop control clauses terminate the <b>loop</b>. |
| 167 |
The macro <b>loop-finish</b> is used to provide a normal exit |
| 168 |
from a nested conditional inside a <b>loop</b>. |
| 169 |
Since <b>loop-finish</b> transfers control to the loop epilogue, |
| 170 |
using <b>loop-finish</b> within a <tt>finally</tt> expression can cause |
| 171 |
infinite looping. |
| 172 |
</p> |
| 173 |
<table class="menu" border="0" cellspacing="0"> |
| 174 |
<tr><td align="left" valign="top">• <a href="Examples-of-REPEAT-clause.html" accesskey="1">Examples of REPEAT clause</a></td><td> </td><td align="left" valign="top"> |
| 175 |
</td></tr> |
| 176 |
<tr><td align="left" valign="top">• <a href="Examples-of-ALWAYS.html" accesskey="2">Examples of ALWAYS</a></td><td> </td><td align="left" valign="top"> |
| 177 |
</td></tr> |
| 178 |
<tr><td align="left" valign="top">• <a href="Examples-of-WHILE-and-UNTIL-clauses.html" accesskey="3">Examples of WHILE and UNTIL clauses</a></td><td> </td><td align="left" valign="top"> |
| 179 |
</td></tr> |
| 180 |
</table> |
| 181 |
|
| 182 |
<hr> |
| 183 |
<div class="header"> |
| 184 |
<p> |
| 185 |
Next: <a href="Unconditional-Execution-Clauses.html" accesskey="n" rel="next">Unconditional Execution Clauses</a>, Previous: <a href="Value-Accumulation-Clauses.html" accesskey="p" rel="prev">Value Accumulation Clauses</a>, Up: <a href="The-LOOP-Facility.html" accesskey="u" rel="up">The LOOP Facility</a> </p> |
| 186 |
</div> |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
</body> |
| 191 |
</html> |