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
Value of Variable with For Next loop
-
- Posts: 64
- Joined: Tue 03 Apr 2018, 12:07
- Location: Belgium
Re: Value of Variable with For Next loop
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.should Num not finish up holding the value 5000 or whatever the upper range is set to
You can leave the loop with other values if you use the EXIT FOR instruction. Check the manual for this.
Re: Value of Variable with For Next loop
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
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
Re: Value of Variable with For Next loop
As it should. Think about what the NEXT statement does:hinckleyj wrote: ↑Thu 17 Nov 2022, 07:35After this has run, Num holds the value of 5001, not 5000Code: Select all
FOR Num = 0 TO 5000 <code> NEXT Num
- Add the STEP value (+1 by default) to the loop variable.
- Test the new value of the loop variable against the TO value.
- 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.
-
- Posts: 25
- Joined: Sat 02 Jun 2018, 08:02
Re: Value of Variable with For Next loop
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
Now I am aware of how this works, I can issue a num = num-1 to work around this.
Thanks everyone
John
- hellomike
- Posts: 192
- Joined: Sat 09 Jun 2018, 09:47
- Location: Amsterdam
Re: Value of Variable with For Next loop
John,
Sometimes I even use it to my advantage.
E.g.
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."
Re: Value of Variable with For Next loop
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?
-
- Posts: 25
- Joined: Sat 02 Jun 2018, 08:02
Re: Value of Variable with For Next loop
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
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