https://wiki.contextgarden.net/api.php?action=feedcontributions&user=82.127.213.183&feedformat=atomWiki - User contributions [en]2024-03-29T05:20:02ZUser contributionsMediaWiki 1.34.1https://wiki.contextgarden.net/index.php?title=System_Macros/Loops_and_Recursion&diff=8644System Macros/Loops and Recursion2008-05-31T16:01:43Z<p>82.127.213.183: /* <cmd>doloop</cmd> <cmd>exitloop</cmd> */</p>
<hr />
<div>< '''Prev:''' [[System Macros/Branches and Decisions|Brances & Decisions]] | '''Top:''' [[System Macros]] | '''Next:''' [[System Macros/Action Processing|Action Processing]] ><br />
<br />
TeX does not offer us powerfull for-loop mechanisms. On<br />
the other hand its recursion engine is quite unique. We<br />
therefore identify the for-looping macros by this method.<br />
The most simple alternative is the one that only needs a<br />
number.<br />
<br />
=== <cmd>dorecurse</cmd> ===<br />
<texcode><br />
\dorecurse {n} {whatever we want}<br />
</texcode><br />
<br />
This macro can be nested without problems and therefore be<br />
used in situations where Plain TeX's <code>\loop</code> macro<br />
ungracefully fails. The current value of the counter is<br />
available in <code>\recurselevel</code>, before as well as after<br />
the <code>whatever we want</code> stuff.<br />
<br />
<context source="yes"><br />
\dorecurse % inner loop<br />
{10}<br />
{\recurselevel: % outer value<br />
\dorecurse % inner loop<br />
{\recurselevel} % outer value<br />
{\recurselevel} % inner value<br />
\dorecurse % inner loop<br />
{\recurselevel} % outer value<br />
{\recurselevel} % inner value<br />
\endgraf}<br />
</context><br />
<br />
In this example the first, second and fourth<br />
<code>\recurselevel</code> concern the outer loop, while the third<br />
and fifth one concern the inner loop. The depth of the<br />
nesting is available for inspection in <code>\recursedepth</code>.<br />
<br />
Both <code>\recurselevel</code> and <code>\recursedepth</code> are<br />
macros. The real conters are hidden from the user because<br />
we don't want any interference.<br />
<br />
<br />
=== <cmd>dostepwiserecurse</cmd> ===<br />
The simple command <code>\dorecurse</code> is<br />
a special case of the more general:<br />
<br />
<texcode><br />
\dostepwiserecurse {from} {to} {step} {action}<br />
</texcode><br />
<br />
This commands accepts positive and negative steps. Illegal<br />
values are handled as good as possible and the macro accepts<br />
numbers and counters.<br />
<br />
<texcode><br />
\dostepwiserecurse {1} {10} {2} {...}<br />
\dostepwiserecurse {10} {1} {-2} {...}<br />
</texcode><br />
<br />
=== <cmd>doloop</cmd> <cmd>exitloop</cmd> ===<br />
Sometimes loops are not determined by counters, but by<br />
(a combinations of) conditions. We therefore implement a<br />
straightforward loop, which can only be left when we<br />
explicitly exit it. Nesting is supported. First we present<br />
a more extensive alternative.<br />
<br />
<texcode><br />
\doloop<br />
{Some kind of typesetting punishment \par<br />
\ifnum\pageno>100 \exitloop \fi}<br />
</texcode><br />
<br />
When needed, one can call for <code>\looplevel</code> and<br />
<code>\loopdepth</code>.<br />
<br />
The loop is executed at least once, so beware of situations<br />
like:<br />
<br />
<texcode><br />
\doloop {\exitloop some commands}<br />
</texcode><br />
<br />
It's just a matter of putting the text into the <code>\if</code><br />
statement that should be there anyway, like in:<br />
<br />
<texcode><br />
\doloop {\ifwhatever \exitloop \else some commands\fi}<br />
</texcode><br />
<br />
You can also quit a loop immediately, by using <code><br />
\exitloopnow</code> instead. Beware, this is more sensitive<br />
for conditional errors.<br />
<br />
< '''Prev:''' [[System Macros/Branches and Decisions|Brances & Decisions]] | '''Top:''' [[System Macros]] | '''Next:''' [[System Macros/Action Processing|Action Processing]] ><br />
<br />
[[Category:ConTeXt programming]]<br />
[[Category:Inside ConTeXt]]</div>82.127.213.183