for next

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
Ivan
Posts: 127
Joined: Tue 07 May 2019, 16:47

for next

Post by Ivan »

Code: Select all

      for t% = 1 to 0
        print t%
      next

      print t%
      end
The first print produces 1 and the second produces 2
Behaves exactly as:

like for t% = 1 to 1

I did not expect that, becauce I thought for-next always increments with one as default.

But it seems for - next always do one iteration...
BBC Model B - 1984-1989. 6502 assembler, Unicomal 1988-1994, Some C and C++, Pascal 1990-1994. Bought a copy of BBC-BASIC 2007, but started to program at a daily basis 2019. C++ in 2021.
RichardRussell

Re: for next

Post by RichardRussell »

Ivan wrote: Fri 02 Oct 2020, 20:11 I thought for-next always increments with one as default.
It does, and that's exactly what your result is showing! Breaking it down to what each statement does:

Code: Select all

      FOR t% = 1 TO 0 : REM Set t% = 1, set loop terminating value to 0
        PRINT t%      : REM Print the value of t%, which is 1
      NEXT            : REM Add the default STEP to t%, then test if it is > the terminating value
      PRINT t%        : REM Print value of t%, which is 1 + 1 = 2
But it seems for - next always do one iteration...
Yes, it must, since the test for loop termination is performed in the NEXT statement. It's the same with a REPEAT ... UNTIL loop, because the test for loop termination is in the UNTIL statement it must execute at least once. Only in a WHILE ... ENDWHILE loop does the test for termination happen at the start of the loop, so it may not be executed at all.

There are some BASICs which perform the test for loop termination in the FOR statement but they tend to run more slowly (if interpreted) because both the FOR and the NEXT statements must be executed on each loop iteration. In BBC BASIC the FOR statement is executed only once, which speeds things up.
Ivan
Posts: 127
Joined: Tue 07 May 2019, 16:47

Re: for next

Post by Ivan »

I was rewriting some old code from UniComal and normally I can do that without to many quirks.

UniComal apperently at least test the FOR statement and skips the hole FOR - ENDFOR structure if not true.

UniComal also treat the FOR - ENDFOR counter variable as a local variable.
BBC Model B - 1984-1989. 6502 assembler, Unicomal 1988-1994, Some C and C++, Pascal 1990-1994. Bought a copy of BBC-BASIC 2007, but started to program at a daily basis 2019. C++ in 2021.
RichardRussell

Re: for next

Post by RichardRussell »

When I need to emulate the 'test for termination at the start of the loop' behaviour (which I do in LB Booster for example) I generally use EXIT FOR like this:

Code: Select all

      FOR var = start TO finish : IF start > finish THEN EXIT FOR
        ... REM Body of loop goes here
      NEXT
One could of course enclose the entire loop in an IF ... ENDIF but that's more difficult for an automated translator because it doesn't know where the end of the loop is.