Author |
Topic: FOR NEXT loop (Read 664 times) |
|
Matt
Developer
member is offline


Gender: 
Posts: 210
|
 |
FOR NEXT loop
« Thread started on: Jun 25th, 2013, 05:40am » |
|
Hi,
When using a FOR NEXT loop, I've always assumed that the variable after the TO is checked each time the loop is run to ascertain whether the looping has reached it's end, and if a calculation is required, e.g. t%-1, then is also done each loop. After some testing, this does not seem to be the case. Am I correct. Is this end figure - the result of t%-1, variable, or figure - set at the start and stored somewhere, e.g. on the stack, at the start of the looping?
Matt
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: FOR NEXT loop
« Reply #1 on: Jun 25th, 2013, 9:54pm » |
|
on Jun 25th, 2013, 05:40am, Matt wrote: What follows the TO keyword is a numeric expression (not a variable), which like every expression is evaluated when it is encountered. There are no circumstances whatever in BBC BASIC when the evaluation of an expression is somehow 'deferred' until later (apart from when you do it in your own code using EVAL).
But I'm sure you knew that, so I wonder if you have failed to appreciate that the FOR statement is not part of the loop: it is executed only once at the start. Only the 'body' of the loop and the NEXT statement are executed multiple times. You can easily ascertain that using Trace and single-stepping.
This is totally standard in all dialects of BASIC, so far as I'm aware.
Richard.
|
|
Logged
|
|
|
|
Matt
Developer
member is offline


Gender: 
Posts: 210
|
 |
Re: FOR NEXT loop
« Reply #2 on: Jun 26th, 2013, 04:44am » |
|
on Jun 25th, 2013, 9:54pm, Richard Russell wrote:I wonder if you have failed to appreciate that the FOR statement is not part of the loop: it is executed only once at the start. Only the 'body' of the loop and the NEXT statement are executed multiple times. |
|
You're right. I had failed to appreciate that. I think it's one of those things that I misunderstood years ago, and then carried that misunderstanding through - until now.
As I was quite young when I first started programming with the BBC Micro and was not as interested in learning as I was in 'getting the job done', I guess there are quite a lot of misunderstandings in my reasoning when it comes to programming.
Thanks for explanation.
Matt
|
|
Logged
|
|
|
|
DDRM
Administrator
member is offline


Gender: 
Posts: 321
|
 |
Re: FOR NEXT loop
« Reply #3 on: Jun 26th, 2013, 08:19am » |
|
Thanks, that IS interesting. I'd also always assumed it went back to the FOR... statement, and had wondered, but never tested, whether the end condition was changeable.
I note that changing the control variable WILL alter the looping, though.
Best wishes,
D
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: FOR NEXT loop
« Reply #4 on: Jun 26th, 2013, 08:56am » |
|
on Jun 26th, 2013, 08:19am, DDRM wrote:I note that changing the control variable WILL alter the looping, though. |
|
Since BBC BASIC is interpreted there's no way it can tell that you've changed the value of the control variable 'behind its back'. Before the advent of EXIT FOR the standard way of prematurely exiting a FOR...NEXT loop was to set the loop variable to (or past) the limit value. Setting it past the limit value provided a convenient way of being able to test, on exit, whether it was terminated prematurely or not.
Richard.
|
« Last Edit: Jun 26th, 2013, 08:56am by admin » |
Logged
|
|
|
|
|