[NTG-context] ! Extra \else error

Aditya Mahajan adityam at umich.edu
Thu Dec 11 16:18:30 CET 2008


On Thu, 11 Dec 2008, Aditya Mahajan wrote:

> On Thu, 11 Dec 2008, Bart C. Wise wrote:
>
>> When I run the code below (via minimals), I get the following error, why?
>> 
>> \newcount\startcnt
>> \newcount\tempcnta
>> 
>> \def\myloop#1{%
>>  \message{enter}
>>  \tempcnta=\the\startcnt%
>>  \loop
>>    \message{loop}
>>    \advance \tempcnta by 1
>>    \ifnum\tempcnta>#1\else \repeat\fi
>>  \message{exit}
>> }
>> 
>> \starttext
>> \startcnt=1800
>> \myloop{2008}
>> \stoptext
>
> Do you know of \dostepwiserecurse{from}{to}{step}{body}. For example,
>
> \starttext
> \dostepwiserecurse{1800}{2008}{1}{We are at \recurselevel \endgraf}
> \stoptext

There is also doloop. Documentations from syst-ext.tex

%D \macros
%D   {doloop,exitloop}
%D
%D Sometimes loops are not determined by counters, but by
%D (a combinations of) conditions. We therefore implement a
%D straightforward loop, which can only be left when we
%D explictly exit it. Nesting is supported. First we present
%D a more extensive alternative.
%D
%D \starttyping
%D \doloop
%D   {Some kind of typesetting punishment \par
%D    \ifnum\pageno>100 \exitloop \fi}
%D \stoptyping
%D
%D When needed, one can call for \type{\looplevel} and
%D \type{\loopdepth}.

[...]

%D We don't have to declare new counters for \type{\looplevel}
%D and \type{\loopdepth} because we can use \type{\recurselevel}
%D and \type{\recursedepth}.

(although I could not get recurselevel or looplevel to work)

[...]

%D The loop is executed at least once, so beware of situations
%D like:
%D
%D \starttyping
%D \doloop {\exitloop some commands}
%D \stoptyping
%D
%D It's just a matter of putting the text into the \type{\if}
%D statement that should be there anyway, like in:
%D
%D \starttyping
%D \doloop {\ifwhatever \exitloop \else some commands\fi}
%D \stoptyping
%D
%D You can also quit a loop immediately, by using \type
%D {\exitloopnow} instead. Beware, this is more sensitive
%D for conditional errors.

So, you can write your example as (changed the stopping condition to add 
\else, just to illustrate that it works)

\newcount\startcnt
\newcount\tempcnta

\def\myloop#1{%
   \message{enter}
   \tempcnta=\the\startcnt%
   \doloop{
     \message{loop \the\tempcnta}
     \advance \tempcnta by 1
     \ifnum\tempcnta<#1 \else \exitloop \fi}
   \message{exit} }

\starttext
\startcnt=1800
\myloop{2009}
\stoptext

Aditya


More information about the ntg-context mailing list