On Samedi 16 Juin 2001 17:51, Christopher Cave wrote:
> I have just come across a feature in the behaviour of NEXT in FOR-loops
> which is a bit puzzling. If NEXT is invoked from the body of the loop with
> the counter NOT on its final value, control ends up in the loop with the
> counter reset at its next value - just what one would hope for. However,
> if the counter IS on its final value, the same happens except that the
> counter is not reset. Try entering even and odd integers in the program
> below (I am using QPC2v2.00 and SMSQ/e v2.98) :
>
> 100 CLS : INPUT '>';n
> 110 FOR i=0 TO n
> 120 IF i MOD 2=0 THEN NEXT i
> 130 PRINT i
> 140 NEXT i
> 150 PRINT 'done'
> 160 END FOR i
>
> To get round this in a piece of code I'm currently engaged on, I have had
> to test for i=n when the exception arises and use EXIT i when on the final
> loop. Comments anyone?
This is normal: although NEXT is _NOT_ the equivalent of END FOR (it is used
to reenter the loop, either FOR ... END FOR or REPeat ... END REPeat, short
circuiting the instructions after the NEXT), it behaves in much the same way:
when the counter reaches its maximum value, the interpreter aborts the loop,
continuing just after the END FOR or NEXT (whichever was reached first).
In a FOR ... END FOR loop, the NEXT directive must be followed by EXIT if you
want to ensure that S*BASIC branches after the END FOR when the NEXT is
executed while the FOR counter is already at its maximal value.
NEXT always worked that way in all QDOS/SMS versions, so it is surpising that
you discover this today.
QDOS/SMS forever !
Thierry.