1
0
Fork 0
cl-sites/HyperSpec-7-0/HyperSpec/Body/06_ad.htm
2024-04-01 10:24:07 +02:00

54 lines
9.1 KiB
HTML

<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
<HTML>
<HEAD>
<TITLE>CLHS: Section 6.1.4</TITLE>
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
<LINK REL=TOP HREF="../Front/index.htm">
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
<LINK REL=PREV HREF="06_ace.htm">
<LINK REL=UP HREF="06_a.htm">
<LINK REL=NEXT HREF="06_ada.htm">
</HEAD>
<BODY>
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="06_ace.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="06_a.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="06_ada.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<H2>
6.1.4 Termination Test Clauses</H2> <P>
The <TT>repeat</TT> construct causes iteration to terminate after a specified number of times. The loop body executes <I>n</I> times, where <I>n</I> is the value of the expression <I>form</I>. The <I>form</I> argument is evaluated one time in the loop prologue. If the expression evaluates to 0 or to a negative <A REL=DEFINITION HREF="26_glo_n.htm#number"><I>number</I></A>, the loop body is not evaluated. <P>
The constructs <TT>always</TT>, <TT>never</TT>, <TT>thereis</TT>, <TT>while</TT>, <TT>until</TT>, and the macro <A REL=DEFINITION HREF="m_loop_f.htm#loop-finish"><B>loop-finish</B></A> allow conditional termination of iteration within a <A REL=DEFINITION HREF="m_loop.htm#loop"><B>loop</B></A>. <P>
The constructs <TT>always</TT>, <TT>never</TT>, and <TT>thereis</TT> provide specific values to be returned when a <A REL=DEFINITION HREF="m_loop.htm#loop"><B>loop</B></A> terminates. Using <TT>always</TT>, <TT>never</TT>, or <TT>thereis</TT> in a loop with value accumulation clauses that are not <TT>into</TT> causes an error of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <A REL=DEFINITION HREF="e_progra.htm#program-error"><B>program-error</B></A> to be signaled (at macro expansion time). Since <TT>always</TT>, <TT>never</TT>, and <TT>thereis</TT> use the <A REL=DEFINITION HREF="s_ret_fr.htm#return-from"><B>return-from</B></A> <A REL=DEFINITION HREF="26_glo_s.htm#special_operator"><I>special operator</I></A> to terminate iteration, any <TT>finally</TT> clause that is supplied is not evaluated when exit occurs due to any of these constructs. In all other respects these constructs behave like the <TT>while</TT> and <TT>until</TT> constructs. <P>
The <TT>always</TT> construct takes one <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> and terminates the <A REL=DEFINITION HREF="m_loop.htm#loop"><B>loop</B></A> if the <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> ever evaluates to <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>; in this case, it returns <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>. Otherwise, it provides a default return value of <A REL=DEFINITION HREF="a_t.htm#t"><B>t</B></A>. If the value of the supplied <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> is never <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>, some other construct can terminate the iteration. <P>
The <TT>never</TT> construct terminates iteration the first time that the value of the supplied <I>form</I> is <A REL=DEFINITION HREF="26_glo_n.htm#non-nil"><I>non-nil</I></A>; the <A REL=DEFINITION HREF="m_loop.htm#loop"><B>loop</B></A> returns <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>. If the value of the supplied <I>form</I> is always <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>, some other construct can terminate the iteration. Unless some other clause contributes a return value, the default value returned is <A REL=DEFINITION HREF="a_t.htm#t"><B>t</B></A>. <P>
The <TT>thereis</TT> construct terminates iteration the first time that the value of the supplied <I>form</I> is <A REL=DEFINITION HREF="26_glo_n.htm#non-nil"><I>non-nil</I></A>; the <A REL=DEFINITION HREF="m_loop.htm#loop"><B>loop</B></A> returns the value of the supplied <I>form</I>. If the value of the supplied <I>form</I> is always <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>, some other construct can terminate the iteration. Unless some other clause contributes a return value, the default value returned is <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>. <P>
There are two differences between the <TT>thereis</TT> and <TT>until</TT> constructs: <P>
<P><DL><DT>* The <TT>until</TT> construct does not return a value or <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A> based on the value of the supplied <I>form</I>. <P><DD>
<DT>* The <TT>until</TT> construct executes any <TT>finally</TT> clause. Since <TT>thereis</TT> uses the <A REL=DEFINITION HREF="s_ret_fr.htm#return-from"><B>return-from</B></A> <A REL=DEFINITION HREF="26_glo_s.htm#special_operator"><I>special operator</I></A> to terminate iteration, any <TT>finally</TT> clause that is supplied is not evaluated when exit occurs due to <TT>thereis</TT>. <P><DD>
<P></DL><P>
The <TT>while</TT> construct allows iteration to continue until the supplied <I>form</I> evaluates to <A REL=DEFINITION HREF="26_glo_f.htm#false"><I>false</I></A>. The supplied <I>form</I> is reevaluated at the location of the <TT>while</TT> clause. <P>
The <TT>until</TT> construct is equivalent to <TT>while (not </TT><I>form</I><TT>)...</TT>. If the value of the supplied <I>form</I> is <A REL=DEFINITION HREF="26_glo_n.htm#non-nil"><I>non-nil</I></A>, iteration terminates. <P>
Termination-test control constructs can be used anywhere within the loop body. The termination tests are used in the order in which they appear. If an <TT>until</TT> or <TT>while</TT> clause causes termination, any clauses that precede it in the source are still evaluated. If the <TT>until</TT> and <TT>while</TT> constructs cause termination, control is passed to the loop epilogue, where any <TT>finally</TT> clauses will be executed. <P>
There are two differences between the <TT>never</TT> and <TT>until</TT> constructs: <P>
<P><DL><DT>* The <TT>until</TT> construct does not return <A REL=DEFINITION HREF="a_t.htm#t"><B>t</B></A> or <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A> based on the value of the supplied <I>form</I>. <P><DD>
<DT>* The <TT>until</TT> construct does not bypass any <TT>finally</TT> clauses. Since <TT>never</TT> uses the <A REL=DEFINITION HREF="s_ret_fr.htm#return-from"><B>return-from</B></A> <A REL=DEFINITION HREF="26_glo_s.htm#special_operator"><I>special operator</I></A> to terminate iteration, any <TT>finally</TT> clause that is supplied is not evaluated when exit occurs due to <TT>never</TT>. <P><DD></DL><P>
In most cases it is not necessary to use <A REL=DEFINITION HREF="m_loop_f.htm#loop-finish"><B>loop-finish</B></A> because other loop control clauses terminate the <A REL=DEFINITION HREF="m_loop.htm#loop"><B>loop</B></A>. The macro <A REL=DEFINITION HREF="m_loop_f.htm#loop-finish"><B>loop-finish</B></A> is used to provide a normal exit from a nested conditional inside a <A REL=DEFINITION HREF="m_loop.htm#loop"><B>loop</B></A>. Since <A REL=DEFINITION HREF="m_loop_f.htm#loop-finish"><B>loop-finish</B></A> transfers control to the loop epilogue, using <A REL=DEFINITION HREF="m_loop_f.htm#loop-finish"><B>loop-finish</B></A> within a <TT>finally</TT> expression can cause infinite looping. <P>
<A REL=CHILD HREF="06_ada.htm"><H2>
6.1.4.1 Examples of REPEAT clause</H2></A><P>
<A REL=CHILD HREF="06_adb.htm"><H2>
6.1.4.2 Examples of ALWAYS, NEVER, and THEREIS clauses</H2></A><P>
<A REL=CHILD HREF="06_adc.htm"><H2>
6.1.4.3 Examples of WHILE and UNTIL clauses</H2></A><P><P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issue</A>, <I>not part of the specification</I>, applies to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss224.htm">LOOP-NAMED-BLOCK-NIL:OVERRIDE</A><P></UL><HR>
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
</BODY>
</HTML>