Value of Variable with For Next loop

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
hinckleyj
Posts: 25
Joined: Sat 02 Jun 2018, 08:02

Value of Variable with For Next loop

Post by hinckleyj »

Hi everyone,

In BBC4W I have a loop with:

FOR Num = 0 TO 5000
<code>
NEXT Num

After this has run, Num holds the value of 5001, not 5000

I have tried this with 0 to 5000 and 1 to 5000 and it always ends up adding 1 to any value you set the Loop to.

Is this by design, or should Num not finish up holding the value 5000 or whatever the upper range is set to on the Loop?

Many thanks.
John
Edja
Posts: 64
Joined: Tue 03 Apr 2018, 12:07
Location: Belgium

Re: Value of Variable with For Next loop

Post by Edja »

should Num not finish up holding the value 5000 or whatever the upper range is set to
When the loopvariable reaches the upper value, the loop is left with the value "upper value+1". This is standard for all basics as far as I know.
You can leave the loop with other values if you use the EXIT FOR instruction. Check the manual for this.
DDRM

Re: Value of Variable with For Next loop

Post by DDRM »

Hi John,

In BBC BASIC (and I think most if not all BASICs) FOR loops run from the first control value to the second **inclusively**: that is, in your example, the loop will execute while num takes values of 0 up to 5000. The value of num gets incremented at the end of each iteration. When num gets incremented the next time (taking it to 5001) the test will fail, and it will drop out of the FOR loop.

[Arguably irrelevant in a BBC BASIC forum, but may be useful to those coming from other languages]
This is in contrast to some other languages like Python, where
for x in range(0,5000):
would execute 5000 times, with the last x value executed being 4999. In the implementation of Python I have here, this DOES execute with x having the value 4999.

Hope that's helpful.

D
Hated Moron

Re: Value of Variable with For Next loop

Post by Hated Moron »

hinckleyj wrote: Thu 17 Nov 2022, 07:35

Code: Select all

FOR Num = 0 TO 5000
<code>
NEXT Num
After this has run, Num holds the value of 5001, not 5000
As it should. Think about what the NEXT statement does:
  1. Add the STEP value (+1 by default) to the loop variable.
  2. Test the new value of the loop variable against the TO value.
  3. If the loop variable is less than or equal to the TO value (more than or equal to if STEP is negative) jump back to the first statement in the loop.
So if Num has the final value 5000 within the loop it will have the value 5001 outside the loop once the final NEXT statement has been executed. It is precisely because the loop variable is now outside the specified range that the loop was exited! Most (but not all) BASICs work this way.
hinckleyj
Posts: 25
Joined: Sat 02 Jun 2018, 08:02

Re: Value of Variable with For Next loop

Post by hinckleyj »

Thank you everyone for your comprehensive responses. Much clearer now.

Now I am aware of how this works, I can issue a num = num-1 to work around this.

Thanks everyone
John
User avatar
hellomike
Posts: 192
Joined: Sat 09 Jun 2018, 09:47
Location: Amsterdam

Re: Value of Variable with For Next loop

Post by hellomike »

John,

Sometimes I even use it to my advantage.
E.g.

Code: Select all

      FOR I%=0 TO N%-1
        ..... code .....
        IF ...... EXIT FOR
        ..... code .....
      NEXT

      IF I% = N% PRINT "EXIT FOR did not fire."
Hated Moron

Re: Value of Variable with For Next loop

Post by Hated Moron »

hinckleyj wrote: Thu 17 Nov 2022, 18:07 Now I am aware of how this works, I can issue a num = num-1 to work around this.
I'm puzzled as to why your program needs to know the value of the loop variable outside the loop. In many languages this would be 'out of scope' and even in BASIC it is strictly speaking undefined (not least because BASICs differ in how they behave). Unless you are leaving the loop early using EXIT FOR, isn't the value you are wanting the same as the expression after TO, and therefore known even without the loop executing?
hinckleyj
Posts: 25
Joined: Sat 02 Jun 2018, 08:02

Re: Value of Variable with For Next loop

Post by hinckleyj »

Hi HM,

In most cases, it doesn't. You're right in that most loops are used to loop your code, and then you're done with the loop, and the loop variable isn't used elsewhere.

I was playing around with a program in which variables were used to set the FROM and TO values, but then used the TO value afterwards, and I didn't know it ended up 1 higher. Using Richard Russell's BBC for Windows has a 'List Variables' monitor and I noticed it always ended up 1 higher (something that after years of coding I didn't realise).

I really appreciate all your help. Its great that there is an active forum for this great language still.

Thank you
John